diff --git a/OAEP.py b/OAEP.py new file mode 100644 index 0000000..ceadac5 --- /dev/null +++ b/OAEP.py @@ -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 + diff --git a/encodetest.py b/encodetest.py index 242d42d..970d22b 100644 --- a/encodetest.py +++ b/encodetest.py @@ -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)