Skip to content

Instantly share code, notes, and snippets.

@rtoal
Created April 17, 2022 22:15
Show Gist options
  • Save rtoal/677cebd14bfbb31edf7ef6a02050f499 to your computer and use it in GitHub Desktop.
Save rtoal/677cebd14bfbb31edf7ef6a02050f499 to your computer and use it in GitHub Desktop.
Python shell session to check RSA-style math
>>> p = 36469
>>> q = 50929
>>> n = p * q
>>> n
1857329701
>>> e = 65537
>>> d = pow(e, -1, (p-1)*(q-1))
>>> d
395695169
>>> m = bytes("¿Dónde está ud.?", "utf-8")
>>> m = " ".join(f"{b:x}" for b in m)
>>> m
'c2 bf 44 c3 b3 6e 64 65 20 65 73 74 c3 a1 20 75 64 2e 3f'
>>> m = 'c2bf44 c3b36e 646520 657374 c3a120 75642e 3f0202'
>>> m = [int(x, 16) for x in m.split(" ")]
>>> m
[12762948, 12825454, 6579488, 6648692, 12820768, 7693358, 4129282]
>>> c = [pow(x, e, n) for x in m]
>>> c
[1674934738, 920121142, 703310795, 1740932196, 512101030, 1327283085, 1468977038]
>>> [pow(x, d, n) for x in c]
[12762948, 12825454, 6579488, 6648692, 12820768, 7693358, 4129282]
>>> [pow(x, d, n) for x in c] == m
True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment