Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 132,665 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,163 people online right now. Registration is fast and FREE... Join Now!




vernam cipher

 
Reply to this topicStart new topic

vernam cipher, java coding for vernam cipher

mark qiesh
post 26 Aug, 2007 - 04:02 PM
Post #1


New D.I.C Head

*
Joined: 26 Aug, 2007
Posts: 5


My Contributions


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...
User is offlineProfile CardPM

Go to the top of the page

Martyr2
post 26 Aug, 2007 - 05:36 PM
Post #2


Programming Theoretician

Group Icon
Joined: 18 Apr, 2007
Posts: 5,062



Thanked 175 times

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions


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).

CODE

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!

decap.gif



User is offlineProfile CardPM

Go to the top of the page

Programmist
post 26 Aug, 2007 - 05:41 PM
Post #3


Four-letter word

Group Icon
Joined: 2 Jan, 2006
Posts: 1,177



Thanked 6 times

Dream Kudos: 100

Expert In: Java

My Contributions


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/docs/books/tutorial/
User is offlineProfile CardPM

Go to the top of the page

mark qiesh
post 26 Aug, 2007 - 07:27 PM
Post #4


New D.I.C Head

*
Joined: 26 Aug, 2007
Posts: 5


My Contributions


thanks dude...
you help me a lot...
either vernam cipher and vigenere cipher which one is more secure?
User is offlineProfile CardPM

Go to the top of the page

Martyr2
post 26 Aug, 2007 - 09:31 PM
Post #5


Programming Theoretician

Group Icon
Joined: 18 Apr, 2007
Posts: 5,062



Thanked 175 times

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions


Perhaps you can look them up and see. smile.gif
User is offlineProfile CardPM

Go to the top of the page

Programmist
post 27 Aug, 2007 - 01:38 AM
Post #6


Four-letter word

Group Icon
Joined: 2 Jan, 2006
Posts: 1,177



Thanked 6 times

Dream Kudos: 100

Expert In: Java

My Contributions


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.
User is offlineProfile CardPM

Go to the top of the page

mark qiesh
post 28 Aug, 2007 - 02:38 AM
Post #7


New D.I.C Head

*
Joined: 26 Aug, 2007
Posts: 5


My Contributions


QUOTE(alcdotcom @ 27 Aug, 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.


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
User is offlineProfile CardPM

Go to the top of the page

mark qiesh
post 15 Sep, 2007 - 12:59 AM
Post #8


New D.I.C Head

*
Joined: 26 Aug, 2007
Posts: 5


My Contributions


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

This post has been edited by mark qiesh: 15 Sep, 2007 - 12:59 AM
User is offlineProfile CardPM

Go to the top of the page

potator
post 8 Jan, 2008 - 08:41 PM
Post #9


D.I.C Head

Group Icon
Joined: 2 Dec, 2007
Posts: 74



Thanked 1 times

Dream Kudos: 175
My Contributions


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.

This post has been edited by potator: 8 Jan, 2008 - 08:44 PM


Attached File(s)
Attached File  Enigma.zip ( 58.6k ) Number of downloads: 76
User is offlineProfile CardPM

Go to the top of the page

potator
post 18 Jan, 2008 - 11:35 PM
Post #10


D.I.C Head

Group Icon
Joined: 2 Dec, 2007
Posts: 74



Thanked 1 times

Dream Kudos: 175
My Contributions


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.

This post has been edited by potator: 18 Jan, 2008 - 11:36 PM


Attached File(s)
Attached File  Enigma.zip ( 90.25k ) Number of downloads: 72
User is offlineProfile CardPM

Go to the top of the page

potator
post 16 Mar, 2008 - 08:22 AM
Post #11


D.I.C Head

Group Icon
Joined: 2 Dec, 2007
Posts: 74



Thanked 1 times

Dream Kudos: 175
My Contributions


Hey: this code has been updated and explained!!
Check it out here:
http://www.dreamincode.net/forums/showtopic45410.htm
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/23/08 05:49AM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month