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)-加解密消息及文件相关推荐

  1. python杂记-RSA加解密实现(2)

    二.计算素数 质数(Prime number),又称素数,指在大于1的自然数中,除了1和该数自身外,无法被其他自然数整除的数(也可定义为只有1与该数本身两个正因数的数).大于1的自然数若不是素数,则称 ...

  2. 【Python杂记】:课程表生成日历程序(生成.ics文件可直接导入日历)

    项目场景: 临近开学,大家也基本收到了自己的课表,但是一想想天天还得看上什么课,还分哪些周上课,有些课从第一周上到第八周就结课了,有些要上到期末,于是像我这么懒的人,自然是要手机来提醒我的.于是这篇文 ...

  3. python实现非对称加密算法_Python使用rsa模块实现非对称加密与解密

    Python使用rsa模块实现非对称加密与解密 1.简单介绍: RSA加密算法是一种非对称加密算法 是由已知加密密钥推导出解密密钥在计算上是不可行的"密码体制.加密密钥(即公开密钥)PK是公 ...

  4. java中使用openssl生成的rsa公私钥进行数据加解密_使用openssl生成RSA公钥和私钥对...

    在ubuntu上要使用openssl的话需要先进行安装,命令如下: sudo apt-get install openssl 安装完成就可以使用openssl了. 首先需要进入openssl的交互界面 ...

  5. Java中使用OpenSSL生成的RSA公私钥进行数据加解密

    本文出处:http://blog.csdn.net/chaijunkun/article/details/7275632,转载请注明.由于本人不定期会整理相关博文,会对相应内容作出完善.因此强烈建议在 ...

  6. Python实现AES中ECB模式pkcs5padding填充加密/解密(需要加密文档中可以有中文)

    Python实现AES中ECB模式pkcs5padding填充加密/解密(需要加密文档中可以有中文) 一.本文主要解决的问题 二.完整版代码 结果: 三.遇到的问题 1.填充格式错误 2.传入类型错误 ...

  7. Java OpenSSL生成的RSA公私钥进行数据加解密详细介绍

    最近用到企业微信向银行卡转账功能,因为需要使用到:标准RSA算法 故在网上了解一下相关的信息 SA是什么:RSA公钥加密算法是1977年由Ron Rivest.Adi Shamirh和LenAdlem ...

  8. Java中不依赖于第三方库使用OpenSSL生成的RSA公私钥进行数据加解密

    本文出处:http://blog.csdn.net/chaijunkun/article/details/7275632,转载请注明.由于本人不定期会整理相关博文,会对相应内容作出完善.因此强烈建议在 ...

  9. java rsa enc 源码_RSA加解密源码 | 学步园

    源码: #include #include #include #include #include #include #include typedef struct{ unsigned char enc ...

最新文章

  1. 机器学习竞赛必备基础知识_Word2Vec
  2. Google推出的新服务:Docs Spreadsheets
  3. django使用bootstrap快速美化 admin后台
  4. redis.mecmcached和mongoDB的区别
  5. windows + cmake + vs2019 编程
  6. Linux下好用的日志库,我使用過的Linux命令之tailf - 跟蹤日志文件/更好的tail -f版本...
  7. 网络教育统考计算机怎么考试,网络教育统考怎么考
  8. linux vim 粘贴 没有保持原来的格式,linux中的剪贴板用法,实现vim中原格式粘贴...
  9. PreScan第三课:Sensors Model
  10. tomcat乱码问题解决
  11. 网站服务器后缀名,服务器域名,域名后缀
  12. Biobank genetic data探析(三)
  13. 【FINAL】NOI
  14. 计算机内存清理原理,怎样清理计算机内存
  15. 苹果id是什么格式的_可以修改微信号了,怎样起一个好看又好记的微信号ID?...
  16. OneTab: 一键合并所有 Chrome 浏览器标签页
  17. LaTeX之双栏模板表格布局(单双栏满宽+不满宽)
  18. 人工智能实战第三次作业 焦宇恒
  19. 58 同城 iOS 客户端 Hybrid 框架探索
  20. HTML多个单选按钮怎么分组,如何在Excel中对多个选项/单选按钮进行分组?

热门文章

  1. luogu P2572 [SCOI2010]序列操作
  2. 【WPF】ListBox嵌套与事件冒泡
  3. VMWARE错误-VirtualInfrastructure.Utils.ClientsXml的类型初始值设定项引发异常
  4. 利用grep命令查找文件内容
  5. Web系统开发构架再思考-前后端的完全分离
  6. hdu2896 病毒侵袭 ac自动机
  7. android HDMI 清晰度 分辨率
  8. SQL 2008 外网访问说明
  9. DDoS攻防战 (二) :CC攻击工具实现与防御理论
  10. 一个C语言小程序,有10几个命令和MSDOS一样哦:)