Skip to content

Instantly share code, notes, and snippets.

Created July 8, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/b9ed3dbe73ec053fcded to your computer and use it in GitHub Desktop.
Save anonymous/b9ed3dbe73ec053fcded to your computer and use it in GitHub Desktop.
def LCG(seed)
return (1103515245*(seed)+12345)%128
end
def enc(msg, key)
newSeed = LCG(key)
if msg.length > 0
return (msg[0].ord^newSeed).chr + enc(msg[1..-1], newSeed).to_s
end
end
def dec(ciph, key)
return enc(ciph, key)
end
key = 31337;
msg = "Attack at dawn!"
cipher_text = enc(msg, key)
plain_text = dec(cipher_text,key)
puts "Original Message: #{msg}"
puts "Cipher Text: #{cipher_text}"
puts "Plain Text: #{plain_text}"
@funny-falcon
Copy link

def LCG(seed)
   (1103515245*(seed)+12345)&0xffffffff
end

def enc(msg, key)
  msg = msg.dup
  msg.each_byte.with_index do |c,i|
    key =  LCG(key)
    msg.setbyte(i, c^(key>>24))
  end
  msg
end

And if you make LCG with much more bits, then it will be "almost secure":

def LCG(key)
  (0xdeadbeef71fe1234bebada55*key + 1)&0xffffffffffffffffffffffff
end
def enc(msg, key)
  msg = msg.dup
  msg.each_byte.with_index do |c,i|
    key =  LCG(key)
    msg.setbyte(i, c^(key>>88))
  end
  msg
end

but... slow

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment