♣ 도움받은 사이트
https://8gwifi.org/docs/python-rsa.jsp
https://stackoverflow.com/questions/21327491/using-pycrypto-how-to-import-a-rsa-public-key-and-use-it-to-encrypt-a-string
♣ Python 에서 RSA 를 사용하기 위해 아래와 같이 pycryot를 설치하였다.
1. pycropto 설치 (Linux)
https://pypi.python.org/pypi/pycrypto 에서 모듈을 다운받는다.
아래와 같이 설치한다.
$ sudo python3 setup.py install
설치하면... time.clock() 함수가 없다고 뜬다.
찾아보니 Python 3.8 부터 위 함수가 폐기됐다고 한다. 아오~~
결국 Python 2.7 에서 설치하였다.
리눅스에서
$ python -> 2.7
$ python3 -> 3.8 로 실행되기에 python 으로 실행했다.
♣ Python 에서 Public Key, Private Key 생성하기
Public Key, Priavte 키 생성 (PEM 방식) |
from Crypto.PublicKey import RSA #Export RSA public/private KEY in PEM format
#save PEM key into the file
with open('public.pem', 'w') as file: |
테스트를 위해 plain.txt 파일을 만들고 아래와 같이 내용을 추가한다. (변경 가능0
Open SSL, Python Test
♣ OpenSSL 로 키 테스트
Python에서 생성한 키로 OpenSSL 에서 테스트해 본다.
1. Public key 로 암호화하기
$ openssl rsautl -encrypt -inkey ./public.pem -pubin -in ./plain.txt -out encrypt.txt
2. Private Key 로 복호화하기
$ openssl rsautl -decrypt -inkey private.pem -in encrypt.txt -out decPlain.txt
3. plain.txt 와 decPalin.txt 의 내용이 같은지 확인해 본다.
♣ Python 에서 Public Key 로 암호화 하기.
위에서 생성한 public key 로 아래와 같이 암호화 해 본다.
Encrypt Plain Text by public key |
from Crypto.PublicKey import RSA fPri = open('private.pem', 'rb')
prikey = RSA.importKey(fPri.read())
#RSA Encryption Using Public Key cipherKey = Cipher_PKCS1_v1_5.new(pubKey)
fEnc = open('encrypt.txt', 'wb')
# archive
|
위에서 암호화 한 문장 (encrypt.txt) 을 openSSL 에서 복호화할 수 있다.
♣ Python 에서 Private Key 로 복호화 하기.
위에서 생성한 Private Key 로 아래와 같이 복호화 한다.
from Crypto.PublicKey import RSA fPri = open('private.pem', 'rb') prikey = RSA.importKey(fPri.read())
#RSA Encryption Using Public Key cipher = Cipher_PKCS1_v1_5.new(prikey)
fDec = open('decPlain.txt', 'wb')
fPri.close() |
저 위의 OpenSSL 에서 암호화 한 후,
Python 에서 위와 같이 복호화하면 동일하게 복호화 되었다.
'삽질미학 > 잡동사니' 카테고리의 다른 글
Android Studio 에서 Java 컴파일하기 (0) | 2024.09.11 |
---|---|
Zebra ZPL 의 GFA 명령어 및 Bitmap 데이터 압축하기 (0) | 2024.05.13 |
단색 Bitmap 파일을 ZPL GFA 코드로 변환하기 (3) | 2018.10.01 |
[ZPL] ZPL 로 특수문자 출력하기. (0) | 2018.07.11 |
[ZPL] QRCode 출력 시, 상단에 갭이 생기는 경우 (0) | 2018.07.11 |