Octet String to integer and integer to octet string.

Signed-off-by: HeshamTB <hishaminv@gmail.com>
This commit is contained in:
HeshamTB 2020-04-20 20:16:49 +03:00
parent f854c46dd2
commit a474683f5e
Signed by: Hesham
GPG Key ID: 74876157D199B09E
2 changed files with 33 additions and 0 deletions

22
OAEP.py Normal file
View 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

View File

@ -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)