10 Replies - 1115 Views - Last Post: 01 October 2014 - 12:07 PM Rate Topic: -----

#1 derik72090  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 17-November 13

Help with SecureRandom program

Posted 29 September 2014 - 09:08 PM

Me and a few other guys in class are working on an assignment around developing a stream cipher program. We are using the SecureRandom class and the Big Integer class. We have the jist of it written so it can encrypt any characters/numbers and print the key, ciphertext(in binary), and ciphertext(in characters after being encrypted).

The Problem: There are two things we're stuck at. The first is that it sometimes tries to encrypt with negative binary numbers which then results in it not printing out the ciphertext in characters. If you run the program and test it, you'll see what I mean. Second, we have to develop the program into an encryption package so that it can be used for sending and receiving encrypted data through plaintext e-mail. Then, the sender should have a choice of using a password of his/her choice and share that password with the receiver. The receiver may use the password for decryption. So...I've made an attempt in the code with the password part but have all of that commented out for now since I don't think I'm really on the right track.
Here's what we have so far:

We've tried to a million different things to fix the negative binary number issue. The main thing we've tried is using the line
//random.setSeed(seed);
instead of the line
random.generateSeed(seed);
. The advantage of setSeed() is that it will never try to use a negative binary number. The problem, is it uses the same key over and over again. It won't ever generate a different key. This would be a security problem. Now, with generateSeed(), when it uses a positive binary number, it will always generate a different key. But if you keep testing it, it sometimes uses a negative binary number which won't print any ciphertext characters.

As far as the password implementation goes, I'll admit I am not sure how I would implement it. Note that our attempts at implementing the password feature is in the code and commented out. Any advice there to get me in the right direction would be awesome.

import java.util.Scanner;
import java.security.SecureRandom;
import java.math.BigInteger;

class XOR{

   public static void main (String args[]) 
   {
      try {
      
        /*if(verify(password) == true)
        {
         password = null;
         System.gc();
        } */
        
      //Read a line from keyboard
         Scanner in = new Scanner(System.in); //define scanner for input
         String messageString;  
      //String password;
      //char option;
      
      /*System.out.println("Would you like to protect this e-mail with a password? (y/n): ");
      option = in.next().charAt(0);
      if(option == 'y')
      {
            System.out.println("Please enter a password: ");
            password = in.nextLine();
      }
      */
         System.out.println("Please enter e-mail message to send:");
         messageString=in.nextLine(); //read a line
         System.out.println("Original E-Mail Message: "+ messageString);
      
         BigInteger message = convertStringToBigInteger(messageString);		
      
      //SET UP A SECURE RANDOM NUMBER GENERATOR
         SecureRandom random = SecureRandom.getInstance("SHA1PRNG"); 
         int seed = 10;
      //random.setSeed(seed);  //initilize the random number generator
         random.generateSeed(seed);  //initilize the random number generator
      
      //GENERATE KEY (WARNING! BOTCHED)
         int i = messageString.length();
         byte [] keyStream = new byte[i];
         random.nextBytes(keyStream); //generate random bytes input in keyStream
         BigInteger key = new BigInteger(keyStream);
      
      //XOR encryption -- Sender's (Alice's) end
         BigInteger cipherText = message.xor(key);
      
      //Convert ciphertext to a string so that we can print it
      
         System.out.println("plaintext (in binary):" + message.toString(2));
         System.out.println("key (in binary):      " + key.toString(2));
         System.out.println("ciphertext(in binary):" + cipherText.toString(2));
         System.out.println("Ciphertext (in characters): "+ convertBigIntegerToString(cipherText));
      
      //XOR decryption -- Receiver's end (Bob's end)
         BigInteger receivedMessage = cipherText.xor(key);
         String receivedMessageString = convertBigIntegerToString(receivedMessage);		
         System.out.println("Received Message: "+ receivedMessageString);
      }
      catch (Exception e) {
         e.printStackTrace();
      }
   }


//This is a method to convert a String to BigInteger by
//packing each character into a BigInteger
//Input: String
//Output: BigInteger
   public static BigInteger convertStringToBigInteger(String s)
   {
      BigInteger b = new BigInteger("0");
      for (int i = 0; i < s.length(); i++)
      {
         Integer code = new Integer((int)s.charAt(i));
         BigInteger c = new BigInteger(code.toString());
         b = b.shiftLeft(8);
         b = b.or(c);
      }
      return b;
   }


//This is a method to convert a BigInteger to String
//by converting each byte into a character and forming 
//a string of characters.
//Input: BigInteger
//Output: String

   public static String  convertBigIntegerToString(BigInteger B)/>/>
   { 
      String s = new String();
      while (b.compareTo(BigInteger.ZERO) == 1)
      {
         BigInteger c = new BigInteger("11111111", 2);
         int cb = (b.and(c)).intValue();
         Character cv= new Character((char)cb);
         s = (cv.toString()).concat(s);
         b = b.shiftRight(8);
      }
      return s;
   }
}


I am in no way expecting anyone to finish this homework. I just ask for any advice to get me headed in the right direction to solving the problem. The more advice, the better :)/> Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Help with SecureRandom program

#2 infernorthor  Icon User is offline

  • D.I.C Lover

Reputation: 362
  • View blog
  • Posts: 1,718
  • Joined: 07-February 14

Re: Help with SecureRandom program

Posted 29 September 2014 - 09:29 PM

You need to be a little more specific. Let's focus on the negative numbers. What is the relevant code. What is the intended output, and what problem are you having.
Was This Post Helpful? 0
  • +
  • -

#3 derik72090  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 17-November 13

Re: Help with SecureRandom program

Posted 29 September 2014 - 09:35 PM

View Postinfernorthor, on 29 September 2014 - 09:29 PM, said:

You need to be a little more specific. Let's focus on the negative numbers. What is the relevant code. What is the intended output, and what problem are you having.

Lines 37-47 is where I think the problem is.

The intended output is like this:

Quote

Please enter e-mail message to send:
hello
Original E-Mail Message: hello
plaintext (in binary):110100001100101011011000110110001101111
key (in binary): 10101110111001100001010111101011110011
ciphertext(in binary):100001111011100111010010001011010011100
Ciphertext (in characters): C?
Received Message: hello


This is the output I don't want:

Quote

Please enter e-mail message to send:
hello
Original E-Mail Message: hello
plaintext (in binary):110100001100101011011000110110001101111
key (in binary): -11100011000101001111111000101000100011
ciphertext(in binary):-101000010100000010100111110011001001110
Ciphertext (in characters):
Received Message: hello


This is just in regards to the negative binary key/ciphertext in binary. If you notice the Ciphertext(in characters) section, there is nothing printed to the screen in this case. I want the message to be encrypted and a random key generated every time. But I don't want the negatives that are causing nothing to be printed.
Was This Post Helpful? 0
  • +
  • -

#4 infernorthor  Icon User is offline

  • D.I.C Lover

Reputation: 362
  • View blog
  • Posts: 1,718
  • Joined: 07-February 14

Re: Help with SecureRandom program

Posted 29 September 2014 - 09:59 PM

Well doing random.nextBytes(keyStream); is going to produce negative bytes is that the problem?
Was This Post Helpful? 1
  • +
  • -

#5 derik72090  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 17-November 13

Re: Help with SecureRandom program

Posted 29 September 2014 - 10:14 PM

View Postinfernorthor, on 29 September 2014 - 09:59 PM, said:

Well doing random.nextBytes(keyStream); is going to produce negative bytes is that the problem?

Are you saying that is what's causing the problem, or are you asking me? Lol. There's something in the code that is causing the negative bytes and I don't know what it is.
Was This Post Helpful? 0
  • +
  • -

#6 infernorthor  Icon User is offline

  • D.I.C Lover

Reputation: 362
  • View blog
  • Posts: 1,718
  • Joined: 07-February 14

Re: Help with SecureRandom program

Posted 29 September 2014 - 10:17 PM

I'm saying random.nextBytes(keyStream); will give you array of random numbers of -128 to 127 . I'm asking do you want that?
Was This Post Helpful? 1
  • +
  • -

#7 derik72090  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 17-November 13

Re: Help with SecureRandom program

Posted 29 September 2014 - 10:20 PM

View Postinfernorthor, on 29 September 2014 - 10:17 PM, said:

I'm saying random.nextBytes(keyStream); will give you array of random numbers of -128 to 127 . I'm asking do you want that?

I'm guessing not if that is causing it to venture to -128....hmm...
Was This Post Helpful? 0
  • +
  • -

#8 derik72090  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 17-November 13

Re: Help with SecureRandom program

Posted 29 September 2014 - 10:26 PM

How would I prevent it from doing that?
Was This Post Helpful? 0
  • +
  • -

#9 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3420
  • View blog
  • Posts: 15,464
  • Joined: 20-September 08

Re: Help with SecureRandom program

Posted 30 September 2014 - 01:43 AM

ALL proper encryption methods (as opposed to toy ones like Rot13, Caesar etc.) produce binary output (ciphertext) whether the input was text or not. If for some reason you want to represent the ciphertext AS text, the common ways of doing that are either to further Base64-encode it or use a hexadecimal representation
Was This Post Helpful? 1
  • +
  • -

#10 derik72090  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 17-November 13

Re: Help with SecureRandom program

Posted 01 October 2014 - 11:46 AM

Well we were told that the negative binary numbers are OKAY. I'm still having trouble with implementing the password.
Was This Post Helpful? 0
  • +
  • -

#11 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3420
  • View blog
  • Posts: 15,464
  • Joined: 20-September 08

Re: Help with SecureRandom program

Posted 01 October 2014 - 12:07 PM

Quote

Well we were told that the negative binary numbers are OKAY.


There's a bit of confusion here: they're not negative binary numbers but simply byte values exhibiting their natural property of being signed
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1