python杂记-RSA加解密实现(4)-加解密消息及文件
生活随笔
收集整理的這篇文章主要介紹了
python杂记-RSA加解密实现(4)-加解密消息及文件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
3、消息m分段與非負整數n之間的互相轉換
#!/usr/bin/env python3 # -*- coding: utf-8 -*- #2-10-3-6.py import base64 import randomdef enCodeBase64(myStr):return base64.b64encode(myStr.encode("utf8"))def deCodeBase64(myStr):return base64.b64decode(myStr).decode("utf8")strTxt=""" 1978年出現了著名的RSA算法,它通常是先生成一對RSA密鑰, 其中之一是保密密鑰,由用戶保存; 另一個為公開密鑰,可對外公開,甚至可在網絡服務器中注冊。 為提高保密強度,RSA密鑰至少為500位長,一般推薦使用1024位。 這就使加密的計算量很大。 """ maxMesLen=5 def getSegBytes(strTxt):encodeBytesStr=enCodeBase64(strTxt)resultSegBytes=[len(encodeBytesStr)]for i in range(0,len(encodeBytesStr),maxMesLen):segBytes=encodeBytesStr[i:i+maxMesLen]resultSegBytes.append(int.from_bytes(segBytes,'little'))return resultSegBytesdef putSegBytes(segBytes):strBytes=[]strLen=segBytes[0]for data in segBytes[1:]:strBytes.append(data.to_bytes(length=maxMesLen,byteorder='little'))return bytes.join(b"",strBytes)[0:strLen]print(strTxt) print(enCodeBase64(strTxt)) segBytesStr=getSegBytes(strTxt) print(segBytesStr) resStr=putSegBytes(segBytesStr) print(resStr) print(deCodeBase64(resStr)) 1978年出現了著名的RSA算法,它通常是先生成一對RSA密鑰, 其中之一是保密密鑰,由用戶保存; 另一個為公開密鑰,可對外公開,甚至可在網絡服務器中注冊。 為提高保密強度,RSA密鑰至少為500位長,一般推薦使用1024位。 這就使加密的計算量很大。b'CjE5NzjlubTlh7rnjrDkuobokZflkI3nmoRSU0Hnrpfms5XvvIzlroPpgJrluLjmmK/lhYjnlJ/miJDkuIDlr7lSU0Hlr4bpkqXvvIwK5YW25Lit5LmL5LiA5piv5L+d5a+G5a+G6ZKl77yM55Sx55So5oi35L+d5a2Y77ybCuWPpuS4gOS4quS4uuWFrOW8gOWvhumSpe+8jOWPr+WvueWkluWFrOW8gO+8jOeUmuiHs+WPr+WcqOe9kee7nOacjeWKoeWZqOS4reazqOWGjOOAggrkuLrmj5Dpq5jkv53lr4blvLrluqbvvIxSU0Hlr4bpkqXoh7PlsJHkuLo1MDDkvY3plb/vvIzkuIDoiKzmjqjojZDkvb/nlKgxMDI05L2N44CCCui/meWwseS9v+WKoOWvhueahOiuoeeul+mHj+W+iOWkp+OAggo=' [444, 335901190723, 422876834426, 490555862100, 460709849710, 461430222709, 315334878810, 354056760883, 473657529683, 495756669042, 315520079925, 345467153530, 465773946736, 469987118197, 384004009803, 203112083050, 460707228013, 491442686325, 207589960759, 421786709064, 508289969008, 228899375478, 327310006105, 469430006889, 280939476300, 229619888181, 417507584844, 186314475307, 465120671303, 228933056311, 228530344757, 452837338963, 430223144243, 237719675189, 503641700663, 358452580439, 224736012084, 503389058417, 341219628917, 374994319447, 358318696566, 456208901488, 186601461583, 375364351575, 302115023979, 443326877554, 341084482383, 452941665637, 345059849032, 486997961586, 435590620495, 417944450917, 323588811363, 486846981487, 435707728719, 374994991713, 280503478855, 504313833319, 229418824268, 456163160132, 464715609707, 508624516210, 487301345868, 516628575842, 465067595091, 461446984818, 237975328881, 310486723664, 212320679275, 508605776973, 422726087513, 525218510383, 477887034731, 457103264617, 388332743281, 203515390788, 517129071726, 228443374669, 224215839308, 452938908483, 512567373103, 507767907699, 341169624875, 435761477207, 504277985377, 465826047343, 186466725163, 374994447191, 280501121131, 1030711143] b'CjE5NzjlubTlh7rnjrDkuobokZflkI3nmoRSU0Hnrpfms5XvvIzlroPpgJrluLjmmK/lhYjnlJ/miJDkuIDlr7lSU0Hlr4bpkqXvvIwK5YW25Lit5LmL5LiA5piv5L+d5a+G5a+G6ZKl77yM55Sx55So5oi35L+d5a2Y77ybCuWPpuS4gOS4quS4uuWFrOW8gOWvhumSpe+8jOWPr+WvueWkluWFrOW8gO+8jOeUmuiHs+WPr+WcqOe9kee7nOacjeWKoeWZqOS4reazqOWGjOOAggrkuLrmj5Dpq5jkv53lr4blvLrluqbvvIxSU0Hlr4bpkqXoh7PlsJHkuLo1MDDkvY3plb/vvIzkuIDoiKzmjqjojZDkvb/nlKgxMDI05L2N44CCCui/meWwseS9v+WKoOWvhueahOiuoeeul+mHj+W+iOWkp+OAggo=' 1978年出現了著名的RSA算法,它通常是先生成一對RSA密鑰, 其中之一是保密密鑰,由用戶保存; 另一個為公開密鑰,可對外公開,甚至可在網絡服務器中注冊。 為提高保密強度,RSA密鑰至少為500位長,一般推薦使用1024位。 這就使加密的計算量很大。4、加密與解密分段消息
#!/usr/bin/env python3 # -*- coding: utf-8 -*- #2-10-3-7.py import base64 import random import mathdef enCodeBase64(myStr):return base64.b64encode(myStr.encode("utf8"))def deCodeBase64(myStr):return base64.b64decode(myStr).decode("utf8")strTxt=""" 1978年出現了著名的RSA算法,它通常是先生成一對RSA密鑰, 其中之一是保密密鑰,由用戶保存; 另一個為公開密鑰,可對外公開,甚至可在網絡服務器中注冊。 為提高保密強度,RSA密鑰至少為500位長,一般推薦使用1024位。 這就使加密的計算量很大。 """def getPrimeNumbers(minNum,maxNum):for n in range(minNum,maxNum):for i in range(2,int(math.sqrt(n))+1): if (n%i==0):break else:pN=nbreakreturn pNdef getGreatestCommonDivisor(a,b):while a != 0:a, b = b % a, areturn bdef getAnotherGcd(x):minN=random.randint(100,x) for i in range(minN,x):print("=",end="")if (getGreatestCommonDivisor(i,x)==1):print(f">\n找到互質數:{i}")breakreturn i def getModuloInverse(e,r):d=r-1for i in range(2,r):if i%100000==0: print(f"{i/r*100}%")if ((e*i)%r==1):d=iprint("\n找到模逆元")breakreturn d def getSegBytes(strTxt,maxMesLen):encodeBytesStr=enCodeBase64(strTxt)resultSegBytes=[len(encodeBytesStr)]for i in range(0,len(encodeBytesStr),maxMesLen):segBytes=encodeBytesStr[i:i+maxMesLen]resultSegBytes.append(int.from_bytes(segBytes,'little'))return resultSegBytesdef putSegBytes(segBytes,maxMesLen):strBytes=[]strLen=segBytes[0]for data in segBytes[1:]:strBytes.append(data.to_bytes(length=maxMesLen,byteorder='little'))return bytes.join(b"",strBytes)[0:strLen] def encryptBytes(segBytesList,e,N):encryedBytes=[]for data in segBytesList:encryedBytes.append(pow(data,e)% N)return encryedBytesdef decryptBytes(encryedSegBytesList,d,N):decryedBytes=[]for data in encryedSegBytesList:decryedBytes.append(pow(data,d)% N)return decryedBytes#p,q minN=random.randint(10, 200) maxN=random.randint(minN+50, minN+100) p=getPrimeNumbers(minN,maxN) minN=random.randint(200, 300) maxN=random.randint(minN+50, minN+100) q=getPrimeNumbers(minN,maxN) print(p,q) # N=p*q #r r=(p-1)*(q-1) #e e=getAnotherGcd(r) #e關于r的模逆元d d=getModuloInverse(e,r) #公鑰,私鑰 pubKey=(N,e) priKey=(N,d) print(f"\n公鑰:({N},{e})\n私鑰:({N},{d})")encodeStr=enCodeBase64(strTxt)lenN=len(str(N)) maxChar="~"#ascii=126 maxTestChar="" segMessageLen=0 for i in range(0,lenN):maxTestChar+=maxCharif (int.from_bytes(bytes(maxTestChar,encoding='ascii'),'little')>N):segMessageLen=ibreak print(N,maxTestChar,int.from_bytes(bytes("~"*segMessageLen,encoding='ascii'),'little')) print(strTxt) print(encodeStr) segBytesStr=getSegBytes(strTxt,segMessageLen) print(segBytesStr) encryptedMessList=encryptBytes(segBytesStr,e,N) print(encryptedMessList) decryptedMessList=decryptBytes(encryptedMessList,d,N) print(decryptedMessList) resStr=putSegBytes(decryptedMessList,segMessageLen) print(resStr) decodeDecryedStr=deCodeBase64(resStr) print(decodeDecryedStr) 29 281 => 找到互質數:6641找到模逆元公鑰:(8149,6641) 私鑰:(8149,4721) 8149 ~~ 1261978年出現了著名的RSA算法,它通常是先生成一對RSA密鑰, 其中之一是保密密鑰,由用戶保存; 另一個為公開密鑰,可對外公開,甚至可在網絡服務器中注冊。 為提高保密強度,RSA密鑰至少為500位長,一般推薦使用1024位。 這就使加密的計算量很大。b'CjE5NzjlubTlh7rnjrDkuobokZflkI3nmoRSU0Hnrpfms5XvvIzlroPpgJrluLjmmK/lhYjnlJ/miJDkuIDlr7lSU0Hlr4bpkqXvvIwK5YW25Lit5LmL5LiA5piv5L+d5a+G5a+G6ZKl77yM55Sx55So5oi35L+d5a2Y77ybCuWPpuS4gOS4quS4uuWFrOW8gOWvhumSpe+8jOWPr+WvueWkluWFrOW8gO+8jOeUmuiHs+WPr+WcqOe9kee7nOacjeWKoeWZqOS4reazqOWGjOOAggrkuLrmj5Dpq5jkv53lr4blvLrluqbvvIxSU0Hlr4bpkqXoh7PlsJHkuLo1MDDkvY3plb/vvIzkuIDoiKzmjqjojZDkvb/nlKgxMDI05L2N44CCCui/meWwseS9v+WKoOWvhueahOiuoeeul+mHj+W+iOWkp+OAggo=' [444, 67, 106, 69, 53, 78, 122, 106, 108, 117, 98, 84, 108, 104, 55, 114, 110, 106, 114, 68, 107, 117, 111, 98, 111, 107, 90, 102, 108, 107, 73, 51, 110, 109, 111, 82, 83, 85, 48, 72, 110, 114, 112, 102, 109, 115, 53, 88, 118, 118, 73, 122, 108, 114, 111, 80, 112, 103, 74, 114, 108, 117, 76, 106, 109, 109, 75, 47, 108, 104, 89, 106, 110, 108, 74, 47, 109, 105, 74, 68, 107, 117, 73, 68, 108, 114, 55, 108, 83, 85, 48, 72, 108, 114, 52, 98, 112, 107, 113, 88, 118, 118, 73, 119, 75, 53, 89, 87, 50, 53, 76, 105, 116, 53, 76, 109, 76, 53, 76, 105, 65, 53, 112, 105, 118, 53, 76, 43, 100, 53, 97, 43, 71, 53, 97, 43, 71, 54, 90, 75, 108, 55, 55, 121, 77, 53, 53, 83, 120, 53, 53, 83, 111, 53, 111, 105, 51, 53, 76, 43, 100, 53, 97, 50, 89, 55, 55, 121, 98, 67, 117, 87, 80, 112, 117, 83, 52, 103, 79, 83, 52, 113, 117, 83, 52, 117, 117, 87, 70, 114, 79, 87, 56, 103, 79, 87, 118, 104, 117, 109, 83, 112, 101, 43, 56, 106, 79, 87, 80, 114, 43, 87, 118, 117, 101, 87, 107, 108, 117, 87, 70, 114, 79, 87, 56, 103, 79, 43, 56, 106, 79, 101, 85, 109, 117, 105, 72, 115, 43, 87, 80, 114, 43, 87, 99, 113, 79, 101, 57, 107, 101, 101, 55, 110, 79, 97, 99, 106, 101, 87, 75, 111, 101, 87, 90, 113, 79, 83, 52, 114, 101, 97, 122, 113, 79, 87, 71, 106, 79, 79, 65, 103, 103, 114, 107, 117, 76, 114, 109, 106, 53, 68, 112, 113, 53, 106, 107, 118, 53, 51, 108, 114, 52, 98, 108, 118, 76, 114, 108, 117, 113, 98, 118, 118, 73, 120, 83, 85, 48, 72, 108, 114, 52, 98, 112, 107, 113, 88, 111, 104, 55, 80, 108, 115, 74, 72, 107, 117, 76, 111, 49, 77, 68, 68, 107, 118, 89, 51, 112, 108, 98, 47, 118, 118, 73, 122, 107, 117, 73, 68, 111, 105, 75, 122, 109, 106, 113, 106, 111, 106, 90, 68, 107, 118, 98, 47, 110, 108, 75, 103, 120, 77, 68, 73, 48, 53, 76, 50, 78, 52, 52, 67, 67, 67, 117, 105, 47, 109, 101, 87, 119, 115, 101, 83, 57, 118, 43, 87, 75, 111, 79, 87, 118, 104, 117, 101, 97, 104, 79, 105, 117, 111, 101, 101, 117, 108, 43, 109, 72, 106, 43, 87, 43, 105, 79, 87, 107, 112, 43, 79, 65, 103, 103, 111, 61] [3572, 6530, 7097, 1058, 5111, 2228, 6355, 7097, 4613, 813, 2972, 5180, 4613, 7325, 1207, 4898, 3331, 7097, 4898, 7374, 3069, 813, 1225, 2972, 1225, 3069, 3462, 2069, 4613, 3069, 1692, 2768, 3331, 6712, 1225, 2153, 1499, 6174, 7358, 3731, 3331, 4898, 3790, 2069, 6712, 6611, 5111, 6671, 7949, 7949, 1692, 6355, 4613, 4898, 1225, 5001, 3790, 4953, 7186, 4898, 4613, 813, 3640, 7097, 6712, 6712, 307, 4945, 4613, 7325, 4585, 7097, 3331, 4613, 7186, 4945, 6712, 6366, 7186, 7374, 3069, 813, 1692, 7374, 4613, 4898, 1207, 4613, 1499, 6174, 7358, 3731, 4613, 4898, 6637, 2972, 3790, 3069, 6398, 6671, 7949, 7949, 1692, 3056, 307, 5111, 4585, 6206, 7136, 5111, 3640, 6366, 4698, 5111, 3640, 6712, 3640, 5111, 3640, 6366, 1930, 5111, 3790, 6366, 7949, 5111, 3640, 3006, 3544, 5111, 4010, 3006, 5545, 5111, 4010, 3006, 5545, 7357, 3462, 307, 4613, 1207, 1207, 544, 5386, 5111, 5111, 1499, 5113, 5111, 5111, 1499, 1225, 5111, 1225, 6366, 2768, 5111, 3640, 3006, 3544, 5111, 4010, 7136, 4585, 1207, 1207, 544, 2972, 6530, 813, 6206, 5001, 3790, 813, 1499, 6637, 4953, 2148, 1499, 6637, 6398, 813, 1499, 6637, 813, 813, 6206, 5087, 4898, 2148, 6206, 6638, 4953, 2148, 6206, 7949, 7325, 813, 6712, 1499, 3790, 3992, 3006, 6638, 7097, 2148, 6206, 5001, 4898, 3006, 6206, 7949, 813, 3992, 6206, 3069, 4613, 813, 6206, 5087, 4898, 2148, 6206, 6638, 4953, 2148, 3006, 6638, 7097, 2148, 3992, 6174, 6712, 813, 6366, 3731, 6611, 3006, 6206, 5001, 4898, 3006, 6206, 766, 6398, 2148, 3992, 5190, 3069, 3992, 3992, 1207, 3331, 2148, 4010, 766, 7097, 3992, 6206, 307, 1225, 3992, 6206, 3462, 6398, 2148, 1499, 6637, 4898, 3992, 4010, 6355, 6398, 2148, 6206, 5545, 7097, 2148, 2148, 1930, 4953, 4953, 4898, 3069, 813, 3640, 4898, 6712, 7097, 5111, 7374, 3790, 6398, 5111, 7097, 3069, 7949, 5111, 2768, 4613, 4898, 6637, 2972, 4613, 7949, 3640, 4898, 4613, 813, 6398, 2972, 7949, 7949, 1692, 5113, 1499, 6174, 7358, 3731, 4613, 4898, 6637, 2972, 3790, 3069, 6398, 6671, 1225, 7325, 1207, 5001, 4613, 6611, 7186, 3731, 3069, 813, 3640, 1225, 1735, 5386, 7374, 7374, 3069, 7949, 4585, 2768, 3790, 4613, 2972, 4945, 7949, 7949, 1692, 6355, 3069, 813, 1692, 7374, 1225, 6366, 307, 6355, 6712, 7097, 6398, 7097, 1225, 7097, 3462, 7374, 3069, 7949, 2972, 4945, 3331, 4613, 307, 4953, 5113, 5386, 7374, 1692, 7358, 5111, 3640, 7136, 2228, 6637, 6637, 6530, 6530, 6530, 813, 6366, 4945, 6712, 3992, 6206, 3056, 6611, 3992, 1499, 5190, 7949, 3006, 6206, 307, 1225, 2148, 6206, 7949, 7325, 813, 3992, 4010, 7325, 2148, 6366, 813, 1225, 3992, 3992, 813, 4613, 3006, 6712, 3731, 7097, 3006, 6206, 3006, 6366, 2148, 6206, 3069, 3790, 3006, 2148, 1930, 4953, 4953, 1225, 3433] [444, 67, 106, 69, 53, 78, 122, 106, 108, 117, 98, 84, 108, 104, 55, 114, 110, 106, 114, 68, 107, 117, 111, 98, 111, 107, 90, 102, 108, 107, 73, 51, 110, 109, 111, 82, 83, 85, 48, 72, 110, 114, 112, 102, 109, 115, 53, 88, 118, 118, 73, 122, 108, 114, 111, 80, 112, 103, 74, 114, 108, 117, 76, 106, 109, 109, 75, 47, 108, 104, 89, 106, 110, 108, 74, 47, 109, 105, 74, 68, 107, 117, 73, 68, 108, 114, 55, 108, 83, 85, 48, 72, 108, 114, 52, 98, 112, 107, 113, 88, 118, 118, 73, 119, 75, 53, 89, 87, 50, 53, 76, 105, 116, 53, 76, 109, 76, 53, 76, 105, 65, 53, 112, 105, 118, 53, 76, 43, 100, 53, 97, 43, 71, 53, 97, 43, 71, 54, 90, 75, 108, 55, 55, 121, 77, 53, 53, 83, 120, 53, 53, 83, 111, 53, 111, 105, 51, 53, 76, 43, 100, 53, 97, 50, 89, 55, 55, 121, 98, 67, 117, 87, 80, 112, 117, 83, 52, 103, 79, 83, 52, 113, 117, 83, 52, 117, 117, 87, 70, 114, 79, 87, 56, 103, 79, 87, 118, 104, 117, 109, 83, 112, 101, 43, 56, 106, 79, 87, 80, 114, 43, 87, 118, 117, 101, 87, 107, 108, 117, 87, 70, 114, 79, 87, 56, 103, 79, 43, 56, 106, 79, 101, 85, 109, 117, 105, 72, 115, 43, 87, 80, 114, 43, 87, 99, 113, 79, 101, 57, 107, 101, 101, 55, 110, 79, 97, 99, 106, 101, 87, 75, 111, 101, 87, 90, 113, 79, 83, 52, 114, 101, 97, 122, 113, 79, 87, 71, 106, 79, 79, 65, 103, 103, 114, 107, 117, 76, 114, 109, 106, 53, 68, 112, 113, 53, 106, 107, 118, 53, 51, 108, 114, 52, 98, 108, 118, 76, 114, 108, 117, 113, 98, 118, 118, 73, 120, 83, 85, 48, 72, 108, 114, 52, 98, 112, 107, 113, 88, 111, 104, 55, 80, 108, 115, 74, 72, 107, 117, 76, 111, 49, 77, 68, 68, 107, 118, 89, 51, 112, 108, 98, 47, 118, 118, 73, 122, 107, 117, 73, 68, 111, 105, 75, 122, 109, 106, 113, 106, 111, 106, 90, 68, 107, 118, 98, 47, 110, 108, 75, 103, 120, 77, 68, 73, 48, 53, 76, 50, 78, 52, 52, 67, 67, 67, 117, 105, 47, 109, 101, 87, 119, 115, 101, 83, 57, 118, 43, 87, 75, 111, 79, 87, 118, 104, 117, 101, 97, 104, 79, 105, 117, 111, 101, 101, 117, 108, 43, 109, 72, 106, 43, 87, 43, 105, 79, 87, 107, 112, 43, 79, 65, 103, 103, 111, 61] b'CjE5NzjlubTlh7rnjrDkuobokZflkI3nmoRSU0Hnrpfms5XvvIzlroPpgJrluLjmmK/lhYjnlJ/miJDkuIDlr7lSU0Hlr4bpkqXvvIwK5YW25Lit5LmL5LiA5piv5L+d5a+G5a+G6ZKl77yM55Sx55So5oi35L+d5a2Y77ybCuWPpuS4gOS4quS4uuWFrOW8gOWvhumSpe+8jOWPr+WvueWkluWFrOW8gO+8jOeUmuiHs+WPr+WcqOe9kee7nOacjeWKoeWZqOS4reazqOWGjOOAggrkuLrmj5Dpq5jkv53lr4blvLrluqbvvIxSU0Hlr4bpkqXoh7PlsJHkuLo1MDDkvY3plb/vvIzkuIDoiKzmjqjojZDkvb/nlKgxMDI05L2N44CCCui/meWwseS9v+WKoOWvhueahOiuoeeul+mHj+W+iOWkp+OAggo='1978年出現了著名的RSA算法,它通常是先生成一對RSA密鑰, 其中之一是保密密鑰,由用戶保存; 另一個為公開密鑰,可對外公開,甚至可在網絡服務器中注冊。 為提高保密強度,RSA密鑰至少為500位長,一般推薦使用1024位。 這就使加密的計算量很大。加解密文件
#!/usr/bin/env python3 # -*- coding: utf-8 -*- #2-10-3-8.py import base64 import random import math import jsonfileName="2-10-3-8.7z" logFileName="2-10-3-8.log"with open(fileName,'rb') as fileObject:strTxt=fileObject.read()logFile=open(logFileName,'w')def printLog(*logStr):for data in logStr:logFile.write(str(data))logFile.write("\n\n")def enCodeBase64(myStr):return base64.b64encode(myStr)def deCodeBase64(myStr):return base64.b64decode(myStr)def getPrimeNumbers(minNum,maxNum):for n in range(minNum,maxNum):for i in range(2,int(math.sqrt(n))+1): if (n%i==0):break else:pN=nbreakreturn pNdef getGreatestCommonDivisor(a,b):while a != 0:a, b = b % a, areturn bdef getAnotherGcd(x):minN=random.randint(100,x) for i in range(minN,x):printLog("=")if (getGreatestCommonDivisor(i,x)==1):printLog(f">\n找到互質數:{i}")breakreturn i def getModuloInverse(e,r):d=r-1for i in range(2,r):if i%100000==0: printLog(f"{i/r*100}%")if ((e*i)%r==1):d=iprintLog("\n找到模逆元")breakreturn d def getSegFromBytes(strTxt,maxMesLen,codeBase64=True):if codeBase64:encodeBytesStr=enCodeBase64(strTxt)else:encodeBytesStr=strTxtresultSegBytes=[len(encodeBytesStr)]for i in range(0,len(encodeBytesStr),maxMesLen):segBytes=encodeBytesStr[i:i+maxMesLen]resultSegBytes.append(int.from_bytes(segBytes,'little'))return resultSegBytesdef getBytesFromSeg(segBytes,maxMesLen,codeBase64=False):strBytes=[]strLen=segBytes[0]for data in segBytes[1:]:strBytes.append(data.to_bytes(length=maxMesLen,byteorder='little'))result=bytes.join(b"",strBytes)[0:strLen] if codeBase64:result=deCodeBase64(result)return resultdef encryptBytesList(segBytesList,e,N):encryedBytes=[]i=0bllen=len(segBytesList)encryedBytes.append(segBytesList[0])for data in segBytesList[1:]:i+=1if i% 500 ==0 :print(f"encrypt=>{i/bllen*100}%")encryedBytes.append(pow(data,e)% N)return encryedBytesdef decryptBytes(encryedSegBytesList,d,N):decryedBytes=[]i=0bllen=len(encryedSegBytesList)decryedBytes.append(encryedSegBytesList[0])for data in encryedSegBytesList[1:]:i+=1if i% 500 ==0 :print(f"decrypt=>{i/bllen*100}%")decryedBytes.append(pow(data,d)% N)return decryedBytesdef getJsonBytesFromSeg(strSeg,codeBase64=False):resultBytes=json.dumps(strSeg)if codeBase64:resultBytes=deCodeBase64(resultBytes)return resultBytesdef getSegFromJsonBytes(strBytes,codeBase64=True):if codeBase64:encodeBytesStr=enCodeBase64(strBytes)else:encodeBytesStr=strBytesresultSeg=json.loads(encodeBytesStr)return resultSeg#p,q minN=random.randint(10, 200) maxN=random.randint(minN+50, minN+100) p=getPrimeNumbers(minN,maxN) minN=random.randint(200, 300) maxN=random.randint(minN+50, minN+100) q=getPrimeNumbers(minN,maxN) printLog(f"p:{p},q:{q}") # N=p*q #r r=(p-1)*(q-1) #e e=getAnotherGcd(r) #e關于r的模逆元d d=getModuloInverse(e,r) #公鑰,私鑰 pubKey=(N,e) priKey=(N,d) printLog(f"\n公鑰:({N},{e})\n私鑰:({N},{d})")lenN=len(str(N)) maxChar="~"#ascii=126 maxTestChar="" segMessageLen=0 for i in range(1,lenN):maxTestChar+=maxCharif (int.from_bytes(bytes(maxTestChar,encoding='ascii'),'little')>N):segMessageLen=i-1breakprintLog(N,maxTestChar,int.from_bytes(bytes("~"*segMessageLen,encoding='ascii'),'little')) printLog(strTxt) segBytesStrList=getSegFromBytes(strTxt,segMessageLen) printLog(len(segBytesStrList),"segBytesStrList:",segBytesStrList)encryptedMessList=encryptBytesList(segBytesStrList,e,N) printLog(len(encryptedMessList),"encryptedMessList:",encryptedMessList)encryptedFileName="2-10-3-8-7z-encry.dat"encryptedFileJsonBytes=getJsonBytesFromSeg(encryptedMessList) printLog(len(encryptedFileJsonBytes),encryptedFileJsonBytes) tmp_decrySegBytesList=getSegFromJsonBytes(encryptedFileJsonBytes,False) printLog(len(tmp_decrySegBytesList),"tmp_decrySegBytesList:",tmp_decrySegBytesList)with open(encryptedFileName,'wb') as fileObject:fileObject.write(bytes(str(segMessageLen).encode('ascii')))fileObject.write(b'\x00'*20)fileObject.write(bytes(encryptedFileJsonBytes.encode("ascii")))with open(encryptedFileName,'rb') as fileObject:encryptedFileCt=fileObject.read(100)printLog("head:",encryptedFileCt)encryptedsegMessageLen=0encryptedsegMessageLenI=0for i in range(0,len(encryptedFileCt)):if encryptedFileCt[i:i+20]==b'\x00'*20:encryptedsegMessageLen=int(encryptedFileCt[:i])encryptedsegMessageLenI=ibreakprintLog(encryptedsegMessageLen)printLog(encryptedsegMessageLenI)fileObject.seek(encryptedsegMessageLenI+20)encryptedFileCt=fileObject.read().decode()decrySegBytesList=getSegFromJsonBytes(encryptedFileCt,False) printLog(len(decrySegBytesList),"decrySegBytesList:",decrySegBytesList)decryptedMessList=decryptBytes(decrySegBytesList,d,N) printLog(len(segBytesStrList),"segBytesStrList:",segBytesStrList) printLog(len(decryptedMessList),"decryptedMessList:",decryptedMessList) resStr=getBytesFromSeg(decryptedMessList,segMessageLen,True) printLog(resStr) printLog(strTxt) logFile.close()with open(encryptedFileName+".7z",'wb') as fileObject:fileObject.write(resStr) 運行結果如下: p:47,q:277===> 找到互質數:2867找到模逆元公鑰:(13019,2867) 私鑰:(13019,10163)13019~~126b'7z\xbc\xaf\'\x1c\x00\x04\x95\x9a_\xbd\x0f-\x00\x00\x00\x00\x00\x00Z\x00\x00\x00\x00\x00\x00\x00...\x00\x00\x00\x00\x00'15545segBytesStrList:[15544, 78, ... , 61]b'7z\xbc\xaf\'\x1c\x00\x04\x95\x9a_...\x00\x00'總結
以上是生活随笔為你收集整理的python杂记-RSA加解密实现(4)-加解密消息及文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何将vue项目打包为.apk文件
- 下一篇: 解决Springboot get请求是参