Python RSA 정보 암호 화 및 신분 서명 검증 실현
3768 단어 네트워크 보안
# -*- 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
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
NGINX: SELinux 13:permission deniedWhen you upgrade a running system to Red Hat Enterprise Linux (RHEL) 6.6 or CentOS 6.6, the Security Enhanced Linux (SEL...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.