Code archives/Algorithms/Vernam-Cipher | Encryption & Decryption

This code has been declared by its author to be Public Domain code.

Download source code

Vernam-Cipher | Encryption & Decryption by ozzi7892012
Those two functions let you encrypt&decrypt strings easly with a provided password.

It is pretty secure with a strong & long password.

Only ways to break the encryption is:
-Bruteforce (yeah lol)
-Frequency analysis of the letters


Note for those who know how Vernam works:
My functions repeat the keyword/password if its shorter than the string you are manipulating
;ozzi789 - 30.01.2012
original_string$="Vernam ist toll"
Print "Original String: "+original_string$

encrypted_string$= vernam_enc(original_string$,"secret1")
Print "Encrypted String: "+encrypted_string$

decrypted_string$=vernam_dec(encrypted_string$,"secret1")
If original_string$=decrypted_string$
	Print "Decrypted String: "+decrypted_string$
Else
	Print "This should not happen :'("
EndIf 


Function vernam_enc$(strng$,key$)
	len_strng=Len(strng$)
	len_key=Len(key$)
	
	key_index=1
	For x=1 To len_strng
		current_strng$=Mid(strng$,x,1)
		current_key$=Mid(key$,key_index,1)
		new_strng$=new_strng$+Chr(Asc(current_strng$)+Asc(current_key$))
		key_index=(x Mod len_key)+1
	Next	
	Return new_strng$
End Function 

Function vernam_dec$(strng$,key$)
	len_strng=Len(strng$)
	len_key=Len(key$)
	
	key_index=1
	For x=1 To len_strng
		current_strng$=Mid(strng$,x,1)
		current_key$=Mid(key$,key_index,1)
		new_strng$=new_strng$+Chr(Asc(current_strng$)-Asc(current_key$))
		key_index=(x Mod len_key)+1
	Next	
	Return new_strng$
End Function

Comments

Guy Fawkes2012
SWEET! I can use this to encrypt my map data even further! Thanks, Ozzi! =D


ozzi7892012
Glad i could help ! :)


SystemError512012
I'll test this for data encryption/decryption in my game project. This is extremely interesting considering what I just read about the Vernam Cipher.

// EDIT:
It's working :)


virtlands2013
I love encryption stuff, thanks for posting 'Vernam-Cipher'.


Guy Fawkes2014
Can someone show an example or site that has this exact function only in PHP, javascript, jquery, or JSON?

thanks!


virtlands2014
The Vernam Cipher {Crypto Museum} -- http://tinyurl.com/kzo37zx
Inside the world of Ciphers {in Java} -- http://tinyurl.com/m3wz2dw
Javascript - simple vernam cipher -- http://tinyurl.com/nm47u22
GitHub Gist Vernam Cipher in JS -- http://tinyurl.com/nast2jb
Vernam Cipher in C# {CodeProject } -- http://tinyurl.com/l3cbyrf
Vernam Cipher in Java {dream in code} -- http://tinyurl.com/n2zzz56



zoqfotpik2014
This is essentially a one-time pad and thus is secure. But he says this:

My functions repeat the keyword/password if its shorter than the string you are manipulating


If it does that, it is instantly zero security (due to differential cryptanalysis) so shouldn't be used for anything serious with keys shorter than the message. The problem with sufficiently long keys becomes key exchange.


Guy Fawkes2014
Then tell us, oh wise one. How would you fix this script then?


Matty2014
Use a key as long as the message....


dna2017
When you use:
VERNAM_DEC$(E$,Left$(K$,Rand(1,Len(K$))))

You get a partial answer to the original text. I did it this way to see if this could be a weak spot in the cypher.
Is it?


Guy Fawkes2017
Not if you do it right.

~GF


Code Archives Forum