本文整理汇总了Python中BitVector.multiplicative_inverse方法的典型用法代码示例。如果您正苦于以下问题:Python BitVector.multiplicative_inverse方法的具体用法?Python BitVector.multiplicative_inverse怎么用?Python BitVector.multiplicative_inverse使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BitVector
的用法示例。
在下文中一共展示了BitVector.multiplicative_inverse方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: RSA_decrypt
# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import multiplicative_inverse [as 别名]
def RSA_decrypt(cipherfile, private_key, p, q):
print ("Decrypt bagin#######################################")
f = open(cipherfile,'r')
hexstr = f.read()
print (hexstr)
m = 0
n = 64
strlis = []
while(n <= len(hexstr)):
strlis.append(hexstr[m:n])
m += 64
n += 64
plains_bv = BitVector(size=0)
for substr in strlis:
package = BitVector(hexstring = substr)
print ("package")
print (package.get_hex_string_from_bitvector())
package_int = package.int_val()
Vp = pow(package_int,private_key[0],p)
Vq = pow(package_int,private_key[0],q)
pbv = BitVector(intVal=p)
qbv = BitVector(intVal=q)
Xp = q * qbv.multiplicative_inverse(pbv).int_val()
Xq = p * pbv.multiplicative_inverse(qbv).int_val()
plain_int = (Vp*Xp + Vq*Xq) % private_key[1]
plain_bv = BitVector(intVal=plain_int,size=128)
print ("plain_bv")
print (plain_bv.get_hex_string_from_bitvector())
plains_bv += plain_bv
fo = open("decrypted.txt",'w')
plains_bv.write_to_file(fo)
示例2: decrypt
# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import multiplicative_inverse [as 别名]
def decrypt(filename, private, p, q):
decrypted = []
## Open file and get the contents
encrypted = openFile(filename, False)
## Using the chinese remainder theorem
## pCRT = C^d mod p
## qCRT = C^d mod q
pCRT , qCRT = [], []
for block in encrypted:
pCRT.append(pow(block, private[0], p))
qCRT.append(pow(block, private[0], q))
## Geting bitvector versions of the p * q values for their multiplicative inverses
pBV = BitVector(intVal = p)
qBV = BitVector(intVal = q)
## Xp = q * (q^-1 mod p)
## Xq = p * (p^-1 mod q)
pX = q * int(qBV.multiplicative_inverse(pBV))
qX = p * int(pBV.multiplicative_inverse(qBV))
## C^d mod n = (VpXp + VqXq) mod n
for i in range(len(encrypted)):
decrypted.append(((pCRT[i] * pX) + (qCRT[i] * qX)) % private[1])
return decrypted
示例3: CRT_simplify
# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import multiplicative_inverse [as 别名]
def CRT_simplify(data, d, n, p, q) :
Vp = binary_exp(data, d, p)
Vq = binary_exp(data, d, q)
# Set all values for chinese remainder thm as stated in notes
# for better understanding later
M = n
m1 = p
m2 = q
M1 = M / m1
M2 = M / m2
# Convert the numbers to BitVector for easy MI
m1_bv = BitVector(intVal=m1)
m2_bv = BitVector(intVal=m2)
M1_bv = BitVector(intVal=M1)
M2_bv = BitVector(intVal=M2)
M1_inv = int(M1_bv.multiplicative_inverse(m1_bv))
M2_inv = int(M2_bv.multiplicative_inverse(m2_bv))
Xp = q * M1_inv
Xq = p * M2_inv
result = (Vp*Xp + Vq*Xq) % n
return result
示例4: chinese_rem
# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import multiplicative_inverse [as 别名]
def chinese_rem(p, q, c, d, n):
pv = BitVector(intVal = p)
qv = BitVector(intVal = q)
vp = pow(c,d,p)
vq = pow(c,d,q)
xp = int(qv) * int(qv.multiplicative_inverse(pv))
xq = int(pv) * int(pv.multiplicative_inverse(qv))
rem = ( (vp*xp) + (vq*xq) ) % 128
return rem
示例5: createKeys
# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import multiplicative_inverse [as 别名]
def createKeys(eVAL):
## Setup the prime generator
pg = PrimeGenerator(bits=128, debug=0)
while(True):
p = pg.findPrime()
q = pg.findPrime()
## Check p and q are different
if p == q: continue
## Check left two MSB's are 1 (bin command returns 0b appended at front)
if not (bin(p)[2] and bin(p)[3] and bin(q)[2] and bin(q)[3]): continue
## Check that the totients of p and q are co-prime to e
if (bgcd(p-1, eVAL) != 1) or (bgcd(q-1, eVAL) !=1): continue
break
## Calculate modulus
n = p * q
## Calculate totient of n
tn = (p - 1) * (q-1)
modBV = BitVector(intVal = tn)
eBV = BitVector(intVal = eVAL)
## Calculate multiplicative inverse
d = eBV.multiplicative_inverse(modBV)
d = int(d)
## Create public and private sets
public, private = [eVAL, n], [d, n]
## Return items
return public, private, p, q, eVAL
示例6: create_keys
# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import multiplicative_inverse [as 别名]
def create_keys():
#creates the keys
e = BitVector(intVal = 65537)
uno = BitVector(intVal = 1)
tres = BitVector(intVal = 3)#used for checking the last two bits
generator = PrimeGenerator( bits = 128, debug = 0 )
p = BitVector(intVal = generator.findPrime())
while (p[0:2] != tres and int (e.gcd(BitVector(intVal = int(p)-1))) != 1):
p = BitVector(intVal = generator.findPrime())
q = BitVector(intVal = generator.findPrime())
while (q[0:2] != tres and int (e.gcd(BitVector(intVal = int(q)-1))) != 1 and p != q):
q = BitVector(intVal = generator.findPrime())
n = int(p) *int( q)
n = BitVector(intVal = n)
to = BitVector(intVal =((int(p)-1)*(int(q)-1)))
d = e.multiplicative_inverse(to)
d = int(d)
e = int (e)
n = int (n)
p = int (p)
q = int (q)
with open('private_key.txt', 'w') as f :
f.write(str(d)+"\n")
f.write(str(n)+"\n")
f.write(str(p)+"\n")
f.write(str(q)+"\n")
with open('public_key.txt', 'w') as f:
f.write(str(e)+"\n")
f.write(str(n)+"\n")
示例7: encrypt
# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import multiplicative_inverse [as 别名]
def encrypt(self, plainText, isHex=0):
if isHex==0:
p=self.getRandomPrime()
q=self.getRandomPrime()
if not p==q: #if those two primes numbers are not the same
pm1=BitVector(intVal=int(p)-1, size=128)
qm1=BitVector(intVal=int(q)-1, size=128)
modulo_n=p.gf_multiply(q)# this is n
e=BitVector(intVal=17, size=128) #this is e
MI=e.multiplicative_inverse(pm1.gf_multiply(qm1))
d=BitVector(intVal=int(MI), size=256) # this is d
self.e=e
self.d=d
self.n=modulo_n
self.p=p
self.q=q
print "n:",int(self.n)
print "e:",int(self.e)
print "d:",int(self.d)
print "p:",int(self.p)
print "q:",int(self.q)
strr=RSA.getBitVectorFromText(plainText)#just getting BitVector from the text
print "plain text converted to int"
print int(strr)
C=pow(int(strr), int(e), int(modulo_n))
cipher=BitVector(intVal=C, size=256)
cipher= RSA.getTextFromBitVector(cipher)
return cipher
示例8: crack_with_CRT
# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import multiplicative_inverse [as 别名]
def crack_with_CRT() :
e, n1 = get_public_key("pub_key1.txt")
_, n2 = get_public_key("pub_key2.txt")
_, n3 = get_public_key("pub_key3.txt")
C1_blocks = get_encrypted_blocks("encrypted1.txt")
C2_blocks = get_encrypted_blocks("encrypted2.txt")
C3_blocks = get_encrypted_blocks("encrypted3.txt")
N = n1 * n2 * n3
N1 = N / n1
N2 = N / n2
N3 = N / n3
n1_bv = BitVector(intVal=n1)
n2_bv = BitVector(intVal=n2)
n3_bv = BitVector(intVal=n3)
N1_bv = BitVector(intVal=N1)
N2_bv = BitVector(intVal=N2)
N3_bv = BitVector(intVal=N3)
d1 = int(N1_bv.multiplicative_inverse(n1_bv))
d2 = int(N2_bv.multiplicative_inverse(n2_bv))
d3 = int(N3_bv.multiplicative_inverse(n3_bv))
# Bitvector used to store decrypted
total_bv = BitVector(size=0)
# Decrypt each block
for i in range(len(C1_blocks)) :
# Use chinese remainder thm to simplify
x = (C1_blocks[i] * N1 * d1 +
C2_blocks[i] * N2 * d2 +
C3_blocks[i] * N3 * d3) % N
de_int = solve_pRoot(3, x)
# Condence all the decrypted into 1 bitvector
total_bv += BitVector(intVal=de_int, size=128)
# Write the decrypted output to file
with open("cracked.txt", 'w') as f :
total_bv.write_to_file(f)
示例9: Chinese_magic
# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import multiplicative_inverse [as 别名]
def Chinese_magic(blok, d, n, p, q):
Vq = find_exponent(int(blok), int(d), int(q))
Vp = find_exponent(int(blok), int(d), int(p))
n1 = int(n) / int(p)
n2 = int(n) / int(q)
n1_bv = BitVector(intVal = n1)
n2_bv = BitVector(intVal = n2)
n1_MI = int(n1_bv.multiplicative_inverse(p))
n2_MI = int(n2_bv.multiplicative_inverse(q))
Xp = int(q) * n1_MI
Xq = int(p) * n2_MI
magic_num = ((Vp * Xp) + (Vq*Xq)) % int(n)
return magic_num
示例10: genKey
# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import multiplicative_inverse [as 别名]
def genKey():
e = 3
finish = False
p = 0
q = 0
print ("###################")
while (finish == False):
somenum = PrimeGenerator(bits=128, debug=0)
p = somenum.findPrime()
q = somenum.findPrime()
# print (p),
#print (q)
finish = True
if (int(bin(p)[2]) * int(bin(p)[3]) * int(bin(q)[2]) * int(bin(q)[3]) == 0):
finish = False
if p == q:
finish = False
pb = bin(p)
qb = bin(q)
'''
print (pb[2])
print (qb[2])
print (pb[3])
print (qb[3])
'''
if (gcd(p - 1, e) != 1) or (gcd(q - 1, e) != 1):
finish = False
n = p * q
# print (n)
tn = (p - 1) * (q - 1)
print "tn"
print (tn)
tnbv = BitVector(intVal=tn)
ebv = BitVector(intVal=e)
print ("ebv")
print (ebv)
print ("tnbv")
print (tnbv)
dbv = ebv.multiplicative_inverse(tnbv)
d = dbv.int_val()
print ("dbv")
print (dbv)
#print (d)
puk = [e, n]
prk = [d, n]
return (puk, prk, p, q)
示例11: Crack_RSA
# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import multiplicative_inverse [as 别名]
def Crack_RSA(key1, key2, key3, etext1, etext2, etext3, output_file):
fin = BitVector(size = 0)
e = key1[0]
FILEOUT = open( output_file, 'a' )
n1 = key1[1]
n2 = key2[1]
n3 = key3[1]
bigN = int(n1) * int(n2) * int (n3)
bigN1 = BitVector (intVal = (bigN / int(n1)))
bigN2 = BitVector (intVal = (bigN / int(n2)))
bigN3 = BitVector (intVal = (bigN / int(n3)))
mi1 = int(bigN1.multiplicative_inverse(n1))
mi2 = int(bigN2.multiplicative_inverse(n2))
mi3 = int(bigN3.multiplicative_inverse(n3))
bv1 = BitVector(filename = etext1)
bv2 = BitVector(filename = etext2)
bv3 = BitVector(filename = etext3)
while(bv1.more_to_read):
bitvec1 = bv1.read_bits_from_file(256)
bitvec2 = bv2.read_bits_from_file(256)
bitvec3 = bv3.read_bits_from_file(256)
temp = ((int(bitvec1) * int(bigN1) * mi1) +
(int(bitvec2) * int(bigN2) * mi2) +
(int(bitvec3) * int(bigN3) * mi3)) % bigN
temp2 = solve_pRoot(3, temp)
temp = BitVector(intVal = temp2)
pad = 128 - temp.length()
temp.pad_from_left(pad)
fin +=temp
fin.write_to_file(FILEOUT)
return
示例12: create_keys
# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import multiplicative_inverse [as 别名]
def create_keys():
#creates the keys
e = BitVector(intVal = 3)
uno = BitVector(intVal = 1)
tres = BitVector(intVal = 3)#used for checking the last two bits
generator = PrimeGenerator( bits = 128, debug = 0 )
p = BitVector(intVal = generator.findPrime())
while (p[0:2] != tres and int (e.gcd(BitVector(intVal = int(p)-1))) != 1):
p = BitVector(intVal = generator.findPrime())
q = BitVector(intVal = generator.findPrime())
while (q[0:2] != tres and int (e.gcd(BitVector(intVal = int(q)-1))) != 1 and p != q):
q = BitVector(intVal = generator.findPrime())
n = int(p) *int( q)
n = BitVector(intVal = n)
to = BitVector(intVal =((int(p)-1)*(int(q)-1)))
d = e.multiplicative_inverse(to)
priv = (d, n, p, q)
pub = (e,n)
return (pub, priv)
示例13: generate_values
# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import multiplicative_inverse [as 别名]
def generate_values(num_bits_for_p_q, e, private_key_file, public_key_file):
# Prime number generator from given script
pg = PrimeGenerator(bits=num_bits_for_p_q, debug=0)
# Find a proper p value
while ( True ) :
p_candidate = pg.findPrime()
# if the first two bits are not set, find a new prime
if (p_candidate>>126) != 3 :
continue
# if the totient of p is not rel prime to e, find new prime
if gcd( p_candidate - 1 , e ) != 1 :
continue
# Otherwise the number is good
break
# Find a proper q value
while ( True ) :
q_candidate = pg.findPrime()
# if p and q are the same, find a new q
if( p_candidate is q_candidate ) :
continue
# if the first two bits are not set, find a new prime
if ( q_candidate>>126 ) != 3 :
continue
# if the totient of q is not rel prime to e, find new prime
if gcd( q_candidate - 1 , e ) != 1 :
continue
# otherwise number is good
break
# the modulus n
n = p_candidate * q_candidate
#The totient of n
tot_n = ( p_candidate - 1 ) * ( q_candidate - 1 )
# make e a BitVector for calculating the MI
tot_n_bv = BitVector( intVal=tot_n )
e_bv = BitVector( intVal=e )
d_bv = e_bv.multiplicative_inverse( tot_n_bv )
d = int( d_bv )
key_set = (e, d, n, p_candidate, q_candidate)
with open(private_key_file, 'w') as f :
f.write("d="+str(d)+"\n")
f.write("n="+str(n)+"\n")
f.write("p="+str(p_candidate)+"\n")
f.write("q="+str(q_candidate)+"\n")
with open(public_key_file, 'w') as f:
f.write("e="+str(e)+"\n")
f.write("n="+str(n)+"\n")
示例14: BitVector
# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import multiplicative_inverse [as 别名]
bv1 = BitVector(bitstring='01100110')
print("first arg bv: " + str(bv1) +
" of int value: " + str(int(bv1))) # 102
bv2 = BitVector(bitstring='011010')
print("second arg bv: " + str(bv2) +
" of int value: " + str(int(bv2))) # 26
bv = bv1.gcd(bv2)
print("gcd bitvec is: " + str(bv) + " of int value: " + str(int(bv))) # 2
print("\nTesting multiplicative_inverse:")
bv_modulus = BitVector(intVal=32)
print("modulus is bitvec: " + str(bv_modulus) +
" of int value: " + str(int(bv_modulus)))
bv = BitVector(intVal=17)
print("bv: " + str(bv) + " of int value: " + str(int(bv)))
result = bv.multiplicative_inverse(bv_modulus)
if result is not None:
print("MI bitvec is: " + str(result) +
" of int value: " + str(int(result)))
else:
print("No multiplicative inverse in this case")
# 17
print("\nTest multiplication in GF(2):")
a = BitVector(bitstring='0110001')
b = BitVector(bitstring='0110')
c = a.gf_multiply(b)
print("Product of a=" + str(a) + " b=" + str(b) + " is " + str(c))
# 00010100110
print("\nTest division in GF(2^n):")
mod = BitVector(bitstring='100011011') # AES modulus
示例15: encrypt
# 需要导入模块: import BitVector [as 别名]
# 或者: from BitVector import multiplicative_inverse [as 别名]
def encrypt(self, plainText, isHex=0):
if isHex==0:
if len(plainText)%32 != 0:
print "ERROR: length of inpur text must be multiple of 32(256 bits)"
print "Current Text length:", len(plainText)
sys.exit(-1)
string=""
finalcipher=""
count=0
for i in plainText:
string=string+i
count=count+1
if count==32:
count=0#
pprime=False
while (pprime==False):
p=BitVector(intVal=getPrime(128), size=128)
p[1]=1
p[0]=1
if not isPrime(int(p)):
pprime=False
else:
pprime=True
qprime=False
while (qprime==False):
q=BitVector(intVal=getPrime(128), size=128)
q[1]=1
q[0]=1
if not isPrime(int(q)):
qprime=False
else:
qprime=True
if not p==q: #if those two primes numbers are not the same
pm1=BitVector(intVal=int(p)-1, size=128)
qm1=BitVector(intVal=int(q)-1, size=128)
modulo_n=int(p)*int(q)# this is n
pm1qm1=int(pm1)*int(qm1)
e=BitVector(intVal=65537, size=256) #this is e
MI=e.multiplicative_inverse(BitVector(intVal=pm1qm1, size=256))
d=BitVector(intVal=int(MI), size=256) # this is d
self.e.append(e)
self.d.append(d)
self.n.append(BitVector(intVal=modulo_n, size=256))
self.p.append(p)
self.q.append(q)
strr=RSA.getBitVectorFromText(string)#just getting BitVector from the text
C=pow(int(strr), int(e), int(modulo_n))
cipher=BitVector(intVal=C, size=256)
ciphertxt=RSA.getTextFromBitVector(cipher)
finalcipher=finalcipher+ciphertxt
string=""
if isHex==1:
a = RSA.getBitVectorFromHexString(plainText)
text= RSA.getTextFromBitVector(a)
string=""
finalcipher=""
count=0
for i in text:
string=string+i
count=count+1
if count==32:
count=0#
pprime=False
while (pprime==False):
p=BitVector(intVal=getPrime(128), size=128)
p[1]=1
p[0]=1
if not isPrime(int(p)):
pprime=False
else:
pprime=True
qprime=False
while (qprime==False):
q=BitVector(intVal=getPrime(128), size=128)
q[1]=1
q[0]=1
if not isPrime(int(q)):
qprime=False
else:
qprime=True
if not p==q: #if those two primes numbers are not the same
pm1=BitVector(intVal=int(p)-1, size=128)
qm1=BitVector(intVal=int(q)-1, size=128)
modulo_n=int(p)*int(q)# this is n
pm1qm1=int(pm1)*int(qm1)
e=BitVector(intVal=65537, size=256) #this is e
MI=e.multiplicative_inverse(BitVector(intVal=pm1qm1, size=256))
d=BitVector(intVal=int(MI), size=256) # this is d
self.e.append(e)
self.d.append(d)
self.n.append(BitVector(intVal=modulo_n, size=256))
self.p.append(p)
self.q.append(q)
strr=RSA.getBitVectorFromText(string)#just getting BitVector from the text
C=pow(int(strr), int(e), int(modulo_n))
cipher=BitVector(intVal=C, size=256)
cipher_hextxt=RSA.getHexStringFromBitVector(cipher)
finalcipher=finalcipher+cipher_hextxt+","
string=""
#.........这里部分代码省略.........