スーパーシンプルなのpythonは、RSAの暗号化と復号化を達成します

何:

RSAの暗号化アルゴリズムは、非対称暗号化アルゴリズムです。公開鍵暗号化と電子商取引ではRSAが広く用いられています。いわゆる公開鍵暗号方式は、異なる暗号化鍵と復号鍵の使用である暗号システム「既知の暗号化キーの復号鍵から導出される計算上実行不可能です」。

IMG

環境

Windows10、p​​ython3.8、pycharm

ステップ

RSAの暗号化と復号化のプロセス、および概念に精通

(RSAをインストールPIPインストールインポートする前に)RSAインポートライブラリ

身近RSAライブラリー

__all__ = ["newkeys", "encrypt", "decrypt", "sign", "verify", 'PublicKey',
           'PrivateKey', 'DecryptionError', 'VerificationError',
           'compute_hash', 'sign_hash']

入力平文

暗号化/復号化(一般的に1024を推奨)

暗号文/平文

テスト

コード

import rsa

def rsa_encrypt(plaintext):
    '''
    输入明文、生成公钥、私钥
    公钥对明文进行加密、字符串加密
    :return:加密结果及私钥
    '''
    pub_key, priv_key = rsa.newkeys(1024)
    print(pub_key)
    print(priv_key)
    plaintext = plaintext.encode()     #the message to encrypt. Must be a byte string no longer than``k-11`` bytes
    ciphertext = rsa.encrypt(plaintext, pub_key)
    print('加密后:', ciphertext)
    return ciphertext, priv_key

def rsa_decrypt(ciphertext, priv_key):
    '''
    传参私钥和加密的明文
    :param ciphertext:
    :param priv_key:
    :return:解密结果
    '''
    plaintext = rsa.decrypt(ciphertext, priv_key)
    plaintext = plaintext.decode()
    print('解密后:', plaintext)

if __name__ == '__main__':
    plaintext = input("输入明文:\n").strip()
    ciphertext, priv_key = rsa_encrypt(plaintext)
    rsa_decrypt(ciphertext, priv_key)

公開された40元の記事 ウォンの賞賛2 ビュー2385

おすすめ

転載: blog.csdn.net/qq_42404383/article/details/105136866