RSA Tool


Python gmpy2

import gmpy2

p = 168870409632549765296862502254899759857248288652407554850383477768823119986297924033151555409082356346297282197467254809081931208549224155851315637344747298357415893525472097154103238042019866682938382139834279447488738548244853184293595933654527554670457923545588565351501521501815389298997833861578226633099
q = 136505636991931352215759862754723380107419404176934963805672183755717752956589135238497377957828097441347957566489629211243389022288160648357324629288547621047463026266886900467398707294913322326802718447765393605735412855505214780439471729354761056917299208670362196028628829497574575055117449119342100056517
e = 65537

n = p * q
fn = (p - 1) * (q - 1)
d = gmpy2.invert(e, fn)

# encode
plain = "hello world"
cipher = gmpy2.powmod(int(plain.encode('hex'),16), e, n)
print cipher

# decode
cipher = 17123126358168532314364171789745947147158203528255189528376034042576955961464108007241396193221317579386199055678187296416631757350036173529280264692509105557539380445658352098757386832691606291403716717255406122398828996166165137869000756490419628668837727171987655840934249710275220041791912411702429566338522067039808551028470065040909781794268938955092150084715883613062506445552253043511850366325327543440113474870896494812927181373067664361593089869517445577240126156141735331979758927134194252186254770372018574046630061371952143328299376196926784339615150604268752279279335534713614030849861471450015448322452
plaint = gmpy2.powmod(cipher, d, n)
s = '%x' % plaint 
if len(s) % 2 != 0:
    s = '0' + s
    

Python pem

import rsa
import base64

public_key_str = """-----BEGIN RSA PUBLIC KEY-----
MIGJAoGBAJ6m+aWR00650XWRNzAcy2ywCS5UAyPC4RVhlAP0BOtjegv5rHFgs/Wg
C+lHUCwQ6/vnS9uebcnlSCjldMQRqGLoCyHdvuN0USWBRsJkCsYXWNEuoiwZ3RUQ
EJuhGBjhy7bAN80SqTIUoPgMSfHCe1ymi3ppuskAOTfOR3KjhvlFAgMBAAE=
-----END RSA PUBLIC KEY-----"""

private_key_str = """-----BEGIN RSA PRIVATE KEY-----
MIICYQIBAAKBgQCepvmlkdNOudF1kTcwHMtssAkuVAMjwuEVYZQD9ATrY3oL+axx
YLP1oAvpR1AsEOv750vbnm3J5Ugo5XTEEahi6Ash3b7jdFElgUbCZArGF1jRLqIs
Gd0VEBCboRgY4cu2wDfNEqkyFKD4DEnxwntcpot6abrJADk3zkdyo4b5RQIDAQAB
AoGAHQciEEgxKGtZRrCOL3BlS/qdg2t9s5JZiobzBRIlwEfQMda51XjDFIL3CvSw
V4+1Db8RIxrGrbUU0d7Bsj2r9l31jCcZvf3ohqRklcWZ/OF7ndL1pHq1yOR3jqVY
JcVie7OZmAt6dqT7FqzbapdxoU5tMmILI9hQBhwhC+puXPUCRQCrD6APLQvgioTK
kkh3iaMBgH/sU2aRk50q7kQhS8XIs1G3tJihBxveXKkflllW/Nxr6EDKBDgmvzEr
SVeNOZGdXZ13lwI9AO1uAp+QopQ6nmsYCApCihnVCJ8hcvERokm9Wgcadfr/fW9d
HwDdCB/YKDwNHTObExhyERBeo1TMo0RRgwJEP5PxI3LUpUIWlMvFz1gCk75UzVs6
FgVNNvWTsORewHeVebfPupnPy9eYrDrPPbuBmUGbQvpKfGw3NCVwOvcYnep7akUC
PQCSb1sm1qmvCkhSfMvYqBlMvVtH6fVeQSX6nNI9t1A0sgbG/IP2oFw2Z7bI8r2j
6mzoktF7ayMJVf0MUckCRQCJPNzi+INd9rXghnDIpx49gMML719k8fkLlpwOD2Se
8PKB5W18W0u8ECrHS906JZFESKLqVYWqmM2jgBVTOp7XzdjWfA==
-----END RSA PRIVATE KEY-----"""

private_key = rsa.PrivateKey.load_pkcs1(private_key_str)
public_key = rsa.PublicKey.load_pkcs1(public_key_str)
msg = "hello world"
print("[*] plaintext: " + msg + '\n')

encrypt_msg = base64.encodestring(rsa.encrypt(msg, public_key))
print("[*] encodetext:")
print(encrypt_msg)

decrypt_msg = rsa.decrypt(base64.decodestring(encrypt_msg), private_key)
print("[*] decodetext: " + decrypt_msg)
    

Python 生成证书

下载rsatool
python rsatool.py -p xxx -q xxx -e 65537 -o private.pem
    

Openssl

使用openssl解析私钥
openssl rsa -text -modulus -in private.pem

使用openssl解析公钥
openssl rsa -text -modulus -pubin -in public.pem

使用openssl加密
openssl rsautl -encrypt -in flag.txt -inkey public.pem -pubin -out cipher.txt

使用openssl解密
openssl rsautl -decrypt -in cipher.txt -inkey private.pem -out plaint.txt