์ƒˆ์†Œ์‹

Languages/Python

[์•”ํ˜ธ ํ”„๋กœํ† ์ฝœ] ์ „์ž์„œ๋ช… ๊ตฌํ˜„ํ•˜๊ธฐ

  • -
๋ฐ˜์‘ํ˜•

https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcR21oLc_VlSi_kU5M8ACpbvSd3g0JhF18BdJQ&usqp=CAU

 

์†Œ์Šค์ฝ”๋“œ์— ๋Œ€ํ•œ ์„ค๋ช…์€ ์ฃผ์„์ฒ˜๋ฆฌ ํ•ด๋‘์—ˆ์Šต๋‹ˆ๋‹ค.

 

๊ฐœ๋ฐœ๋„๊ตฌ : "anaconda -> spyder"

์–ธ์–ด : "python"

 

 

 

์ „์ž์„œ๋ช… ๊ตฌํ˜„์ฝ”๋“œ - myRSA.py

# -*- coding: utf-8 -*- """ Created on Thu May 13 13:12:25 2021 @author: hk """ import base64 from Crypto.PublicKey import RSA from Crypto.Cipher import PKCS1_OAEP import tools # ============================================================================= # Key ์ƒ์„ฑ ๋ฐ ๋กœ๋”ฉ # ============================================================================= def make_key(pr_path, pu_path, size_bit = 1024): pr_key = RSA.generate(size_bit) pu_key = pr_key.public_key() fw = open(pr_path,'wb') fw.write(pr_key.exportKey('PEM'))#base64๋กœ ์ธ์ฝ”๋”ฉ๋œ ๊ฒƒ์„ ๋„ฃ๊ธฐ fw.close() fw = open(pu_path,'wb') fw.write(pu_key.exportKey('PEM')) fw.close() #make_key('pr1.key', 'pu1.key') #ํ‚ค ์ƒ์„ฑํ•˜๊ธฐ def get_key(key_path): fr = open(key_path,'rb') key = RSA.importKey(fr.read()) return key #ํ‚ค ์ฝ์–ด์˜ค๊ธฐ #pr_key = get_key('pr1.key') #pu_key = get_key('pu1.key') #print(pr_key) #print(pu_key) # ============================================================================= # ๋ฉ”์‹œ์ง€ ์•”๋ณตํ˜ธํ™” # ============================================================================= def encrypt_msg(msg,key): ch = PKCS1_OAEP.new(key) # ํ‚ค๋ฅผ ์ฃผ์–ด์ฃผ๋ฉด ์•”ํ˜ธ ํ’€๊ธฐ๊ฐ€ ๊ฐ€๋Šฅ msg_en = ch.encrypt(msg) return msg_en def decrypt_msg(msg,key): ch = PKCS1_OAEP.new(key) msg_de = ch.decrypt(msg) return msg_de def test1(): msg = 'i love you!' pu_key = get_key('pu1.key') msg_en = encrypt_msg(msg.encode(), pu_key) print("์•”ํ˜ธํ™” : ",msg_en,"\n๊ธธ์ด : ",len(msg_en)) pr_key = get_key('pr1.key') msg_de = decrypt_msg(msg_en, pr_key) print("๋ณตํ˜ธํ™” : ",msg_de) #test1() # ============================================================================= # ํŒŒ์ผ ์•”๋ณตํ˜ธํ™” # ============================================================================= def encrypt_file(file_name, key): save_name = file_name +'.enc' fr = open(file_name,'rb') #์ฝ์–ด์˜ค๋Š” ๊ฒƒ fw = open(save_name, 'wb')#์•”ํ˜ธํ™” ๋œ๊ฑธ ์“ฐ๋Š” ๊ฒƒ BSIZE = 86 # 1024bit key์˜ ๊ฒฝ์šฐ ์ตœ๋Œ€ ์ˆ˜์ž„. data = fr.read(BSIZE) while data: data_en = encrypt_msg(data, key) #๋ฐ์ดํ„ฐ ์•”ํ˜ธํ™” fw.write(data_en) data = fr.read(BSIZE) return True #ํŒŒ์ด์ฌ์€ ํŠธ๋ฃจ๋ฅผ ๋Œ€๋ฌธ์ž๋กœ! def decrypt_file(file_name, key): save_file = file_name + '.txt' #์—ด๊ธฐ ์‰ฌ์šฐ๋ผ๊ณ  ํ™•์žฅ์ž ๋ฐ”๊ฟ”์ค€ ๊ฒƒ. fr = open(file_name,'rb') fw = open(save_file,'wb') BSIZE = 128 #๋“ค์–ด์˜ค๋Š” ๊ฐ’์— ๋ฌด๊ด€ํ•˜๊ฒŒ ์ถœ๋ ฅ์€ 128์ด๊ธฐ ๋•Œ๋ฌธ์ž„. data = fr.read(BSIZE) while data: data_dec = decrypt_msg(data, key) fw.write(data_dec) data = fr.read(BSIZE) return True def test2(): # file_name = '5-1.py' # pu_key = get_key('pu1.key') # encrypt_file(file_name, pu_key) # pr_key = get_key('pr1.key') # file_name = '5-1.py.enc' # decrypt_file(file_name, pr_key) # ============================================================================= # hash ๊ฐ’ ๋ฌด๊ฒฐ์„ฑ ๊ฒ€์ฆํ•˜๊ธฐ # ============================================================================= hash1 = tools.get_hash_file('5-1.py') hash2 = tools.get_hash_file('5-1.py.enc.txt') if hash1 == hash2: print('๋ฌด๊ฒฐ์„ฑ ๊ฒ€์ฆ์™„๋ฃŒ --same') else: print('๋ฌด๊ฒฐ์„ฑ ๊ฒ€์ฆ ์‹คํŒจ --fail ') #๊ณต๊ฐœํ‚ค ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ด์šฉํ•œ ๋ฉ”์„ธ์ง€/ํŒŒ์ผ ์•”๋ณตํ˜ธํ™” #test2()

์‹คํ–‰์‹œํ‚ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ํ‚ค๊ฐ€ ์ƒ์„ฑ๋œ๋‹ค.

 

tools.py

# -*- coding: utf-8 -*- """ Created on Thu Mar 11 17:10:52 2021 @author: clccclcc """ # ============================================================================= # >> conda install pycryptodome # ============================================================================= # ============================================================================= # # hash ์™€ base64์— ๋Œ€ํ•˜์—ฌ ์ดํ•ด ๋ฐ ์‚ฌ์šฉ binary_to_text # # ============================================================================= # ============================================================================= # Hash tools # ============================================================================= # from Cryptodome.Hash import SHA256 from Crypto.Hash import SHA256 import base64 def get_hash(msg): sha = SHA256.new() # sha = Hash.SHA256.new() sha.update(msg) hv = sha.digest() return hv def get_hash_file(file_name): fh = open(file_name,'rb') # sha = Hash.SHA256.new() sha = SHA256.new() data=fh.read(1024*2) while data: sha.update(data) data = fh.read(1024*2) hv = sha.digest() return hv def main(): msg='i love you' hv = get_hash(msg.encode()) print(hv) hv_enc64= base64.b64encode(hv) print('b64_en:',hv_enc64) hv_dec64 = base64.b64decode(hv_enc64) print('b64_de:',hv_dec64) file_name='tools.py' hv = get_hash_file(file_name) print(hv) # main()

 

์ „์ž์„œ๋ช… ๋ถ€๋ถ„!

# -*- coding: utf-8 -*- """ Created on Thu May 13 14:06:06 2021 @author: hk """ #์ „์ž์„œ๋ช… import base64 from Crypto.PublicKey import RSA from Crypto.Signature import PKCS1_v1_5 #์‹œ๊ทธ๋‹ˆ์ฒ˜ ๋ชจ๋“ˆ from Crypto.Hash import SHA256 import myRSA as mytool def make_sign_msg(key,msg): hash_obj = SHA256.new(msg) signer = PKCS1_v1_5.new(key) #์„œ๋ช…์‹œ ๊ฐœ์ธํ‚ค(๊ฒ€์ฆ ์‹œ public key) ans = signer.sign(hash_obj) #์‚ฌ์ธํ•œ ๊ฐ’, ๋ฉ”์„ธ์ง€ ํ•ด์‹œ ๊ฐ’ ๋„ฃ๊ธฐ. return ans # ============================================================================= # ์ „์ž์„œ๋ช… ๊ฒ€์ฆ ๋กœ์ง # ============================================================================= def verify_sign(key,msg,sign): #์ „์ž์„œ๋ช… ๊ฒ€์ฆ hash_obj = SHA256.new(msg) verifyer = PKCS1_v1_5.new(key) #๋ณดํ†ต base64 ์ธ์ฝ”๋”ฉ if verifyer.verify(hash_obj, sign): #์ด๋ ‡๊ฒŒ ๋ฒ ๋ฆฌํŒŒ์ด ํ•ด์คŒ return True else: return False # ============================================================================= # ์ „์ž์„œ๋ช… # ============================================================================= def test1(): msg = 'i love you' pr_key = mytool.get_key('pr1.key') sign = make_sign_msg(pr_key, msg.encode()) print("์ „์ž์„œ๋ช… : ",sign,"\n๊ธธ์ด : ",len(sign)) # msg1 = 'i hate you' # sign1 = make_sign_msg(pr_key, msg1.encode()) # print("\n๋‹ค๋ฅธ msg ์„œ๋ช… : ",sign1) pu_key = mytool.get_key('pu1.key') if verify_sign(pu_key, msg.encode(), sign): print('verify') else: print('not verify') #test1() def test2(): msg = 'i love you' pr_key = mytool.get_key('pr1.key') sign = make_sign_msg(pr_key, msg.encode()) sign_txt = base64.b64encode(sign) #base64๋กœ ์ธ์ฝ”๋”ฉ print(sign_txt) #base64๋กœ ์ „์ž์„œ๋ช…๋˜์–ด์žˆ์Œ. ex) ๊ณต์ธ ์ธ์ฆ์„œ #test2() # ============================================================================= # ๋ฌด๊ฒฐ์„ฑ ๋ถ€์ธ๋ฐฉ์ง€ ๊ฐ€๋Šฅ(mac์€ ๋ถ€์ธ๋ฐฉ์ง€ ์•ˆ๋จ) # ============================================================================= def test3(): msg = 'i love you' #์ „์ž์„œ๋ช… : ๋ฌด๊ฒฐ์„ฑ, ๋ถ€์ธ๋ฐฉ์ง€ ๊ฐ€๋Šฅ, mac์€ ๋ถ€์ธ๋ฐฉ์ง€ ๋ถˆ๊ฐ€๋Šฅ #์œ„ test2ํ•จ์ˆ˜์—์„œ ๋‚˜์˜จ base64 encoding๊ฐ’ sign_txt ='cZ27zUkhciRWoe1x17JqsIqUpTyg5/ZlI2vHu/1IBaKVOM3Oeaw/qdxlSH7DHoohf8y5qIr7UnRVl6CIeMZEatqazwNn7Cke2rFIjtBJ1xO5kEsbl8SVhMuV2tucS5jF3g1T6n5+cjlulO6XXwZ7ZkYkjhboX5S45ZNIfKLZsKk=' sign = base64.b64decode(sign_txt) #์ธ์ฝ”๋”ฉ๋œ ๊ฒƒ์„ ๋ฐ”์ด๋„ˆ๋ฆฌ๋กœ ๋””์ฝ”๋”ฉํ•ด์ค€๋‹ค. pu_key = mytool.get_key('pu1.key')#๊ฒ€์ฆ์„ ์œ„ํ•จ. if verify_sign(pu_key, msg.encode(), sign): print('verify') else: print('not verify!')#msg ๊ฐ’์ด ๋ณ€์กฐ๋œ๋‹ค๋ฉด ๋ฌด๊ฒฐ์„ฑ์ด ๊นจ์ง„๋‹ค. #test3() # ============================================================================= # ํŒŒ์ผ์˜ ์ „์ž์„œ๋ช… ๊ตฌํ•˜๊ธฐ # ============================================================================= def make_sign_file(key,file_name): save_file = file_name + '.txt' #์—ด๊ธฐ ์‰ฌ์šฐ๋ผ๊ณ  ํ™•์žฅ์ž ๋ฐ”๊ฟ”์ค€ ๊ฒƒ. fr = open(file_name,'rb') data = fr.read() print("๊ธธ์ด : ",len(data)) hash_obj = SHA256.new(data) signer = PKCS1_v1_5.new(key) #์„œ๋ช…์‹œ ๊ฐœ์ธํ‚ค(๊ฒ€์ฆ ์‹œ public key) ans = signer.sign(hash_obj) #์‚ฌ์ธํ•œ ๊ฐ’, ๋ฉ”์„ธ์ง€ ํ•ด์‹œ ๊ฐ’ ๋„ฃ๊ธฐ. return ans def verify_sign_file(key,file_name,sign): fr = open(file_name,'rb') data = fr.read() hash_obj = SHA256.new(data) verifyer = PKCS1_v1_5.new(key) #์„œ๋ช…์‹œ ๊ฐœ์ธํ‚ค(๊ฒ€์ฆ ์‹œ public key) if verifyer.verify(hash_obj, sign): return True else: return False def test4(): # key = mytool.get_key('pr1.key') # sign = make_sign_file(key,'5-1.py') # sign_txt = base64.b64encode(sign) # print(sign) # print(sign_txt) sign_txt = 'I06/ItATOoGLqkzry9FHBgJu7PP1jbT4x47ORA3fljcaOLMubJgEZrPxX4EhIm1EeF6kqParMQpSeMBXfuvymupiF6uuJiFrCZchRNyQYwEA8cu08Ijdcw/4masEHfkSPHjp/iNz5oJseJBUpu9Kv8tzUptwWIozpfdwvZi2LbU=' sign = base64.b64decode(sign_txt) pu_key = mytool.get_key('pu1.key') if verify_sign_file(pu_key, '5-1.py', sign): print('verify!') else: print('not verify!') test4()

 

anaconda ํ™˜๊ฒฝ

 

->conda env list

ํ˜„์žฌ๊น”๋ฆฐ ๋„์ปค๋ฅผ ์•Œ๋ ค์คŒ

 

->conda list 

ํ˜„์žฌ ๋‚˜์˜ ๋„์ปค์˜ ๋ชจ๋“  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๋ณด์—ฌ์คŒ

 

->conda create -n my1 python=3.8 anaconda

๋’ค์— ์•„๋‚˜์ฝ˜๋‹ค๋ฅผ ์•ˆ์จ์ฃผ๋ฉด ๊ฐ€์ƒํ™˜๊ฒฝ์— spyder์™€ ๊ฐ™์€ ์ธํ„ฐํŽ˜์ด์Šค(์ฃผํ”ผํ„ฐ ๋“ฑ) ๊ฐ€ ํ•จ๊ป˜ ๊น”๋ฆฌ์ง€ ์•Š๋Š”๋‹ค.

์„ค์น˜๋จ.

์ƒ์„ฑ๋˜๋ฉด my1 ์œผ๋กœ ์ ‘์†ํ•œ๋‹ค.

 

๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๊นŒ๋Š” ๋ฐฉ๋ฒ•์€ 2๊ฐ€์ง€๊ฐ€ ์žˆ์Œ.

1. conda install [ํŒจํ‚ค์ง€ ์ด๋ฆ„]

2. pip install [ํŒจํ‚ค์ง€ ์ด๋ฆ„]

 

2๊ฐ€์ง€๋Š” ์ฐจ์ด๊ฐ€ ์žˆ๋Š”๋ฐ, ์ฝ˜๋‹ค ์ธ์Šคํ†จ์€ ์„ค์น˜ ์žฅ์†Œ๋ฅผ ์•„๋‚˜์ฝ˜๋‹ค์—์„œ ์šด์˜ํ•œ๋‹ค. ๊ฑฐ๊ธฐ์— ๋“ฑ๋ก๋œ๊ฒŒ ์žˆ์œผ๋ฉด ์ฐพ์•„๋ณด๊ณ  ๋‹ค์šด๋กœ๋“œ ํ•˜๋Š” ๊ฒƒ์ด๋‹ค.(๊ธฐ๋ณธ ์ €์žฅ์†Œ)์ž„ pip๋Š” python์–ธ์–ด ์ž์ฒด๊ฐ€ ์ œ๊ณตํ•˜๋Š” ์•ฑ ์Šคํ† ๋ฆฌ์ง€ ์—ฌ๊ธฐ์„œ ๊ฐ€์ง€๊ณ  ์™€์„œ ๊นŒ๋Š”๊ฒƒ์ž„. ๋‚ด๋ถ€์ ์œผ๋ก  conda ์—์„œ ๋•ก๊ฒจ์™€์„œ ๊นŒ๋Š” ๊ฒƒ์ž„.

๊ฒฐ์ •์  ์ฐจ์ด :

conda install : a๋ฅผ ๊น”๋•Œ b๊ฐ€ ํ•„์š”ํ•˜๋‹ค๋ฉด a๋ฅผ ๊น”๋•Œ b๊นŒ์ง€ ๊น”์•„์คŒ.(์ข…์† ๊ด€๊ณ„๋ฅผ ์ž๋™์œผ๋กœ ์ œ๊ณตํ•ด์คŒ)

pip install : a ๋ฅผ ๊น”๋•Œ b๊ฐ€ ํ•„์š”ํ•˜๋‹ค๋ฉด a ๋งŒ ๊น”์•„์คŒ (์ข…์† ๊ด€๊ณ„ x)

 

์ŠคํŒŒ์ด๋”์— crypto  ํŒจํ‚ค์ง€๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด 

 

1. pip install pycryptodome -> ๊น”์•„์ค€๋‹ค.

2. python -m Crypto.SelfTest  -> ์Šค์Šค๋กœ ํ…Œ์ŠคํŠธ ํ•ด๋ณด๋Š” ๊ฒƒ

 

๋ฐ˜์‘ํ˜•
Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.