Skip to content

Instantly share code, notes, and snippets.

@neo-32
Last active April 26, 2020 02:58
Show Gist options
  • Save neo-32/b1e05483ab6d712bf5b2433906fcb219 to your computer and use it in GitHub Desktop.
Save neo-32/b1e05483ab6d712bf5b2433906fcb219 to your computer and use it in GitHub Desktop.
elgamal dec
from libnum import *
import re
print "Breaking Elgamal Cipher using known Plain-Text..."
p=234234
g=2342
y=1234
plain_c2="known text"
c2=123414134
c4=123414
print "(c1,c2) known plain-text, (c3,c4) not known, c1==c3 (factor) which mean they are all encrypted with the same randomness k"
plain = s2n(plain_c2.encode('utf16')[::-1])
yk = c2*invmod(plain,p)%p
print "the solution is=",re.sub(r'[^\x01-\x7f]',r'',n2s(invmod(yk,p)*c4%p)[::-1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment