Octet String to integer and integer to octet string.
Signed-off-by: HeshamTB <hishaminv@gmail.com>
This commit is contained in:
parent
f854c46dd2
commit
a474683f5e
22
OAEP.py
Normal file
22
OAEP.py
Normal file
@ -0,0 +1,22 @@
|
||||
#From: https://stackoverflow.com/questions/39964383/implementation-of-i2osp-and-os2ip
|
||||
|
||||
def i2osp(x, xLen):
|
||||
if x >= 256**xLen:
|
||||
raise ValueError("integer too large")
|
||||
digits = []
|
||||
|
||||
while x:
|
||||
digits.append(int(x % 256))
|
||||
x //= 256
|
||||
for i in range(xLen - len(digits)):
|
||||
digits.append(0)
|
||||
return digits[::-1]
|
||||
|
||||
def os2ip(X):
|
||||
xLen = len(X)
|
||||
X = X[::-1]
|
||||
x = 0
|
||||
for i in range(xLen):
|
||||
x += X[i] * 256**i
|
||||
return x
|
||||
|
@ -38,3 +38,14 @@ print(sig_enc)
|
||||
sig_un = en.decrypt(sig_enc,d,n)
|
||||
print(sig_un)
|
||||
print(key)
|
||||
|
||||
import OAEP
|
||||
|
||||
key = en.generateKeys("encode-test", 2048)
|
||||
x = int(en.encrypt("test message", (key[en.N], key[en.E])))
|
||||
print(x)
|
||||
encoded_msg = OAEP.i2osp(x, key[en.N].bit_length())
|
||||
print(encoded_msg)
|
||||
|
||||
decoded = OAEP.os2ip(encoded_msg)
|
||||
print(decoded)
|
||||
|
Loading…
Reference in New Issue
Block a user