Python 使用 RSA
安装rsa模块
pip install rsa
RSA加解密过程:
python
Python 3.5.3 (default, Nov 4 2021, 15:29:10)
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
import rsa
(pubkey, privkey) = rsa.newkeys(512)
print(pubkey, privkey)
PublicKey(7923383863263798057086493131602855244106043226226580784778365931183912547588793153219251578468413935121113886104438479941506950504197502414270136297859107, 65537) PrivateKey(7923383863263798057086493131602855244106043226226580784778365931183912547588793153219251578468413935121113886104438479941506950504197502414270136297859107, 65537, 4082772678981620464589634451595715726894137353703581688236651319042384096914189930827359104990339125600702388770355631338209042409912997066322913565755233, 5111290471875396921428642977019854300275057344938560249265410597941457399078028889, 1550172878427042048581175711004305712704687713604245356094500484144351963)
rsa.encrypt("hello".encode('utf-8'), pubkey)
b'x16rx17xbbOxdcQxa0xffxdfjxad]x1axc7x96xbcx94LxcfBx83GOxa9x18Syx94x13xcaxafN_xd2xd25xa9Etxa1xb6lxc9xb1~xc8xc1+x10x9bx90x06xc6xddxb4xeax86x00x13xf8x0bN~'
crypto = rsa.encrypt('hello'.encode('utf-8'), pubkey)
print(crypto)
b'x16b~x81x0bxd9xb8>x1ex0fKxd9KY5xf1nx80x:xc2w/QFxa6!&xd0+q!xf5x14xe7xe9=Nx1dx0cxdd"6x80xa4xabxb8xf5=xcbxb05x07xf4xb0xa6xe9xe4DbXx99x17x8d'
rsa.decrypt(crypto, privkey)
b'hello'