Notice
Recent Posts
Recent Comments
Link
250x250
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- Compose
- 앱
- CustomScrollView
- tabbar
- textfield
- DART
- Dialog
- binding
- Navigation
- 계측
- 앱바
- textview
- 안드로이드
- android
- livedata
- Coroutines
- viewmodel
- Kotlin
- intent
- TEST
- Flutter
- drift
- scroll
- 테스트
- activity
- ScrollView
- Button
- data
- LifeCycle
- appbar
Archives
- Today
- Total
Study Record
[암호 프로그래밍] RSA를 이용한 암/복호화 , 서명 본문
728x90
RSA 를 이용한 암/복호화
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
import os
import sys
def main():
mymsg = "samsjang loves python"
prikey = RSA.generate(1024)
pubkey = prikey.publickey()
priPemFile = 'prikey.pem'
pubPemFile = 'pubkey.pem'
if os.path.exists(priPemFile) and os.path.exists(pubPemFile):
print('[ OK ] key file exists.')
else:
createPEM(prikey, pubkey, priPemFile, pubPemFile)
encMSg = rsaEnc(mymsg, pubPemFile)
decMsg = rsaDec(encMSg, priPemFile)
print(mymsg)
print(encMSg)
print(decMsg)
pass
def rsaEnc(mymsg, file):
return PKCS1_OAEP.new(readPem(file)).encrypt(mymsg.encode())
def rsaDec(encMsg, file):
return PKCS1_OAEP.new(readPem(file)).decrypt(encMsg).decode()
def readPem(f):
readPem = open(f, 'rb')
pubkey = RSA.import_key(readPem.read())
readPem.close()
return pubkey
def createPEM(prikey, pubkey, priPemFile, pubPemFile):
# input : prikey , pubkey
# output : file(prikey.pem) , file(pubkey.pem)
with open(priPemFile, 'wb') as priFile:
priFile.write(prikey.export_key('PEM'))
with open(pubPemFile, 'wb') as pubFile:
pubFile.write(pubkey.export_key('PEM'))
if __name__ == '__main__':
main()
RSA 를 이용한 서명
# 사전 준비 : private.pem 파일이 미리 필요하다
from Crypto.Signature import pkcs1_15
from Crypto.PublicKey import RSA
from Crypto.Hash import SHA256 as SHA
def readPEM(pemfile):
h = open(pemfile, 'r')
key = RSA.importKey(h.read())
h.close()
return key
def rsa_sign(msg):
private_key = readPEM('prikey.pem')
public_key = private_key.publickey()
h = SHA.new(msg)
signature = pkcs1_15.new(private_key).sign(h)
return public_key, signature
def rsa_verify(msg, public_key, signature):
h = SHA.new(msg)
try:
pkcs1_15.new(public_key).verify(h, signature)
print('Authentic')
except Exception as e:
print('Error :', e)
print('Not Authentic')
def main():
#
# Sender
#
msg = b'My name is samsjang'
public_key, signature = rsa_sign(msg)
#
# Receiver
#
recv_msg = msg
recv_pubkey = public_key
recv_sign = signature
rsa_verify(recv_msg, recv_pubkey, recv_sign)
if __name__ == '__main__':
main()
728x90
'암호 > 프로그래밍' 카테고리의 다른 글
[암호 프로그래밍] 해시 함수 사용하기 (0) | 2021.12.10 |
---|---|
[암호 프로그래밍] 해시 함수 관련 프로그램 (0) | 2021.12.09 |
[암호 프로그래밍] 인코딩/디코딩 프로그램 (0) | 2021.12.07 |
[암호 프로그래밍] AES 암호화/복호화 프로그램 (0) | 2021.12.03 |
[암호 프로그래밍] 3DES-CBC 암호화 / 복호화 프로그램 (0) | 2021.12.02 |