Python RSA 정보 암호 화 및 신분 서명 검증 실현

3768 단어 네트워크 보안
초기 에 python RSA 라 이브 러 리 를 사용 하려 고 시 도 했 지만 rsa.newkeys 방법 은 사용 할 수 없 기 때문에 이 프로그램 은 Python Crypto 라 이브 러 리 를 사용 하여 RSA 정보 암호 화 기능 을 실현 하고 신분 서명 검 사 를 실시 했다. 
# -*- coding: utf-8 -*-
# coding: utf-8
"""
          :
1.  RSA           ,      Alice     Bob        ,Bob             
2.   Bob     ,Bob          ,Alice     Bob        
"""

from Crypto import Random
from Crypto.Hash import SHA
from Crypto.Cipher import PKCS1_v1_5 as Cipher_pkcs1_v1_5
from Crypto.Signature import PKCS1_v1_5 as Signature_pkcs1_v1_5
from Crypto.PublicKey import RSA
import base64

"""  RSA         ,          ,         """
random_generator = Random.new().read
rsa = RSA.generate(1024, random_generator)
#   Alice      
private_pem = rsa.exportKey()
public_pem = rsa.publickey().exportKey()
with open('key/alice_private.pem', 'wb') as f:
    f.write(private_pem)
with open('key/alice_public.pem', 'wb') as f:
    f.write(public_pem)

#   Bob      
private_pem = rsa.exportKey()
public_pem = rsa.publickey().exportKey()
with open('key/bob_private.pem', 'wb') as f:
    f.write(private_pem)
with open('key/bob_public.pem', 'wb') as f:
    f.write(public_pem)

# Alice Bob      ,  Bob     
message = 'hello,bob!'
print('sending words:')
print(message)
with open('key/bob_public.pem') as f:
    key = f.read()
    pubkey = RSA.importKey(key)  #  bob_public.pem      
    cipher = Cipher_pkcs1_v1_5.new(pubkey)  #   pkcs1_v1_5          ,      
    #  message        ,      base64          
    #  base64                               ,             (ascii  128~255   )           
    #   base64              
    cipher_text = base64.b64encode(cipher.encrypt(message.encode(encoding="utf-8")))
    #    
    print('encrypt text is:')
    print(cipher_text)

# Bob                
with open('key/bob_private.pem') as f:
    key = f.read()
    prikey = RSA.importKey(key)
    cipher = Cipher_pkcs1_v1_5.new(prikey)
    plain_text = cipher.decrypt(base64.b64decode(cipher_text), random_generator)
    plain_text = plain_text.decode()
    #    
    print('plain text is:')
    print(plain_text)

print()

""" Bob      ,Bob          ,Alice     , Bob        """
#  
with open('key/bob_private.pem') as f:
    key=f.read()
    signkey=RSA.importKey(key)
    signer=Signature_pkcs1_v1_5.new(signkey)    #  pkcs1_v1_5          ,      ,         signer  
    digest=SHA.new()
    digest.update(message.encode("utf8"))   # message SHA      ,    ,    
    sign=signer.sign(digest)    #          
    signature=base64.b64encode(sign)
    print('signature is:')
    print(signature)
#  
with open('key/bob_public.pem') as f:
    key=f.read()
    unsignkey=RSA.importKey(key)
    unsigner=Signature_pkcs1_v1_5.new(unsignkey)    #  pkcs1_v1_5          ,      ,         unsigner  
    digest=SHA.new()
    digest.update(message.encode("utf8"))
    is_verify = unsigner.verify(digest, base64.b64decode(signature))    #      unsigner    ,     digest           
    print('verify result is:',is_verify)

이상 실행 결 과 는 다음 과 같 습 니 다(공개 키 는 무 작위 생 성)
sending words:
hello,bob!
encrypt text is:
b'NTLHSI8YAowmaRumkO1pl273Jtlfx4ztICZHPuGY9nYVPt1gXQRWvmhVZpi1uXcJckKrtG+8SRTjORZErs0fZm+BPt6btjXGSoPtb9y59iJQ/fsKf9A7QW1vOZhzJYr3qjSShwbyPB/nL6xDCqSKD/ynT8G8QZEbkEd6uzMc9zw='
plain text is:
hello,bob!

signature is:
b'jfKdug+WkvG4dbwqlxYoHaB1DHfciAeV8+Eiv2tjZkCK4E66Oiu4pN96xR7Hn7Tx10Mjt92luTRgl67zK6gxI8+4zSifYo/LJp2q2Hc6zP+ZfBK4HOhwfPBbkFvVwu9lZ2krX+gNslxH9XrNEFujXEkT9c7IMfXt5eXrkmHNrog='
verify result is: True

좋은 웹페이지 즐겨찾기