vernam cipher

java coding for vernam cipher

Page 1 of 1

10 Replies - 58391 Views - Last Post: 16 March 2008 - 09:22 AM Rate Topic: -----

#1 mark qiesh   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 26-August 07

vernam cipher

Posted 26 August 2007 - 05:02 PM

my lecture had gave me a homework to create a vernam cipher program using java programming...
the problem is i know nothing about java...
i mean i just learn this program from other lecture a day before my security lecture give the assignment...
i have search it on internet but it won't work..
i mean i found it but i didn't understand it...
please anybody help me...
Is This A Good Question/Topic? 0
  • +

#3 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5601
  • View blog
  • Posts: 14,678
  • Joined: 18-April 07

Re: vernam cipher

Posted 26 August 2007 - 06:36 PM

Well the basic idea behind a Vernam cipher is that it is a random string of data that is the same length as the data we want to encrypt, we then XOR the characters together one by one to generate the encoding. We then XOR it against the same cipher to decrypt it.

The main idea is that each party must know the same private key (using something like a one-time pad which is a pad with generated random letters on it. The Nazi's used this style of cipher back in WWII with the Enigma Machine). Once used for one message you destroy the paper.

I have written a basic Vernam cipher example in Java for you below. Of course you will need to expand on this to accept any text and generate a stream cipher (the stream of random data used for encryption/decryption).

Keep in mind that this method was devised in an era before the computer age so given that computers can analyze trillions of keys looking for commonalities, they can essentially break any message given enough time. It is secure enough for general usage (like WEP etc) but it is not industrial strength (just in case you want to do some espionage).

import java.lang.Math;

public class xor {
	public static void main(String args[]) {
		// This would be the text we encrypt (in this case "hello")
		// We convert it to a character array
		String text = new String("hello");
		char[] arText = text.toCharArray();

		// This would be our vernam cipher (should be same length as our text)
		// Here we use the same letters, but theoretically should be random
		// characters generated on the fly. USE RANDOM LETTERS!
		String cipher = new String("XYZHG");
		char[] arCipher = cipher.toCharArray();

		// Array to hold our encryption (again same length)
		char[] encoded = new char[5];

		// Encrypt the text by using XOR (exclusive OR) each character
		// of our text against cipher.
		System.out.println("Encoded " + text + " to be... ");
		for (int i = 0; i < arText.length; i++) {
			encoded[i] =  (char) (arText[i] ^ arCipher[i]);
			System.out.print(encoded[i]);
		}

		System.out.println("\nDecoded to be... ");

		// Run through the encrypted text and against the cipher again
		// This decrypts the text.
		for (int i = 0; i < encoded.length; i++) {
			char temp = (char) (encoded[i] ^ arCipher[i]);
			System.out.print(temp);
		}
	 }
}



Read the inline comments to figure it out and good luck with the Vernam cipher!

:snap:
Was This Post Helpful? 2

#4 Programmist   User is offline

  • Refactorer in Chief
  • member icon

Reputation: 256
  • View blog
  • Posts: 1,843
  • Joined: 02-January 06

Re: vernam cipher

Posted 26 August 2007 - 06:41 PM

If you are taking a class that requires knowledge that you don't have, I recommend that you either talk to your instructor about using another language that you know, or drop the class and take a Java class. If neither of those are options then you'd better start doing these tutorials:
http://java.sun.com/...books/tutorial/
Was This Post Helpful? 0
  • +
  • -

#5 mark qiesh   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 26-August 07

Re: vernam cipher

Posted 26 August 2007 - 08:27 PM

thanks dude...
you help me a lot...
either vernam cipher and vigenere cipher which one is more secure?
Was This Post Helpful? 0
  • +
  • -

#6 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5601
  • View blog
  • Posts: 14,678
  • Joined: 18-April 07

Re: vernam cipher

Posted 26 August 2007 - 10:31 PM

Perhaps you can look them up and see. :)
Was This Post Helpful? 0
  • +
  • -

#7 Programmist   User is offline

  • Refactorer in Chief
  • member icon

Reputation: 256
  • View blog
  • Posts: 1,843
  • Joined: 02-January 06

Re: vernam cipher

Posted 27 August 2007 - 02:38 AM

I must have misunderstood. When you said your problem was that you "know nothing about java" I assumed that your problem was that you...well...knew nothing about Java.
Was This Post Helpful? 0
  • +
  • -

#8 mark qiesh   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 26-August 07

Re: vernam cipher

Posted 28 August 2007 - 03:38 AM

View Postalcdotcom, on 27 Aug, 2007 - 02:38 AM, said:

I must have misunderstood. When you said your problem was that you "know nothing about java" I assumed that your problem was that you...well...knew nothing about Java.


yup...it was me...well as i said before, i just learn it a day before my other lecture (security lecturer) give me an assignment...
that's why i joined this group...
to get advice from you guy's
Was This Post Helpful? 0
  • +
  • -

#9 mark qiesh   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 26-August 07

Re: vernam cipher

Posted 15 September 2007 - 01:59 AM

how to generate numeric equivalent based on plaintext?
a=0
b=1
etc

This post has been edited by mark qiesh: 15 September 2007 - 01:59 AM

Was This Post Helpful? 0
  • +
  • -

#10 potator   User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 84
  • Joined: 02-December 07

Re: vernam cipher

Posted 08 January 2008 - 09:41 PM

I finished my own version of the enigma machine just tonight. Unfortunately, there is no gui for it yet, but you can just change up the tester class for your needs. Lucky for you, I've filled in all documentation.

*the reason i have the tester set up to such obscure values is because i had my friend try to crash it (a good practice for all programs) The first four characters of the output ended up as "WTF?" so I've left it on that ever since.

Attached File(s)


This post has been edited by potator: 08 January 2008 - 09:44 PM

Was This Post Helpful? 0
  • +
  • -

#11 potator   User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 84
  • Joined: 02-December 07

Re: vernam cipher

Posted 19 January 2008 - 12:35 AM

I just learned all about throwing exceptions and I couldn't resist. Here is the same code, but now if you try to call methods or create objects with specific parameters, it throws a nice exception at you if you mess up. This feature is really only useful to those of you who are interested in creating another EnigmaMachine class with a different amount of Rotors.

To: Tech guys at dreamincode
code snippets should also allow uploading files. This, project for example, contains 5 files. This would be much too eye strenuous to post as a snippet. Maybe a 'code submissions' area.

Attached File(s)


This post has been edited by potator: 19 January 2008 - 12:36 AM

Was This Post Helpful? 0
  • +
  • -

#12 potator   User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 84
  • Joined: 02-December 07

Re: vernam cipher

Posted 16 March 2008 - 09:22 AM

Hey: this code has been updated and explained!!
Check it out here:
http://www.dreaminco...wtopic45410.htm
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1