Disposable panel, encryption and decryption

I'm trying to pick up cryptography and tried this exercise

Write a program (preferably Java) to create a one-time pad, which is a relatively large file of all random data (say 1 MB). The program should also be able to encrypt / decrypt files based on the one generated.

Tip. Use the following test vector to verify that your program is properly encrypted.

Plaintext (ASCII): each cloud has a silver lining
OTP (HEX): 6dc72fc595e35dcd38c05dca2a0d2dbd8e2df20b129b2cfa29ad17972922a2
Encrypted Text (HEX): 28b14ab7ecc33ea157b539ea426c5e9def0d81627eed498809c17ef9404cc5

I tried to create a one-time block using a random number generator, since I need to convert them to a HEX form. and I’m sure that I am confused or did not solve it correctly.

public static void oneTimePad() { Random ran = new Random(); String s = "0123456789ABCDEF"; for(int i = 0; i < 100; i++) { System.out.print(s.charAt(ran.nextInt(s.length()))); } } 

My disposable notepad would be higher, and I was wondering, like any idea, how I can implement encryption using one-time input and decrypting it.

+7
source share
5 answers

Here you have a complete working example:

  // convert secret text to byte array final byte[] secret = "secret".getBytes() final byte[] encoded = new byte[secret.length]; final byte[] decoded = new byte[secret.length]; // Generate random key (has to be exchanged) final byte[] key = new byte[secret.length]; new SecureRandom().nextBytes(key); // Encrypt for (int i = 0; i < secret.length; i++) { encoded[i] = (byte) (secret[i] ^ key[i]); } // Decrypt for (int i = 0; i < encoded.length; i++) { decoded[i] = (byte) (encoded[i] ^ key[i]); } assertTrue(Arrays.equals(secret, decoded)); 
+8
source

For a one-time pass, you need an array of bytes, not a hexadecimal. Hexadecimal is only necessary for displaying data (we usually have problems reading bits). You can use the Apache Commons libraries (codec pack) to create hex arrays from byte arrays or vice versa if you want to decode test vectors from hexadecimal numbers to bytes.

You should use a safe random number generator, not Random . Therefore use new SecureRandom() . To generate random data, first create an array of bytes, then call nextBytes() on the random number generator. No need to generate integers.

+3
source

First, an OTP algorithm called HOTP, which is a standard RFC, is described here. Almost all other OTPs are correct, and we do not know the algorithm for them.

http://tools.ietf.org/html/rfc4226

There is some Java code that you can use to find out how to do this. Secondly, if you are going to do encryption, do not use Random. Random is good for psuedo random, but if you really need a good source of random data, you need to accept SecureRandom. This is a much better source of random numbers suitable for cryto algorithms.

To convert things to Hex you can easily use

http://docs.oracle.com/javase/1.5.0/docs/api/java/math/BigInteger.html#toString(int )

Or any of the varieties of Long.toString (value, radix), Integer.toString (value, radix) or Byte.toString (value, radix).

 byte[] bytes = ...; for( int i = 0; i < bytes.length; i++ ) { System.out.println( Integer.toString( bytes[i], 16 ); } 
+2
source
 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace one_time_pad { class Program { static void Main(string[] args) { Console.WriteLine(" TRYING\n"); Console.WriteLine("Enter : "); int input= int.Parse( Console.ReadLine()); //random num generation Random rnd = new Random(); int random = rnd.Next(1,10); //binary conversion string binary = Convert.ToString(random,2); string inbinary = Convert.ToString(input,2); Console.WriteLine("Data : " +input +" Binary : " + inbinary); Console.WriteLine(" Key : " +random + " Binary : " + binary); // taking xor int Ghul = input ^ random; //binary conversion string intcon = Convert.ToString(Ghul,2); Console.WriteLine("Encrypted : " + intcon); Console.WriteLine(":)"); Console.Read(); } } } 
0
source

https://en.wikipedia.org/wiki/One-time_pad

 public static String crypt(String string, String keyString) { // convert secret text to byte array byte[] bytes = string != null ? string.getBytes() : new byte[0]; int size = bytes != null ? bytes.length : 0; final byte[] encoded = new byte[size]; final byte[] key = keyString != null ? keyString.getBytes() : new byte[0]; // loop on input bytes for (int i = 0; i < size; i++) { // shift key index // (we assume key can be smaller or equal if larger then adjust) int keyi = i >= keySize ? size % (keySize-1) : i; // pad encoded[i] = (byte) (bytes[i] ^ key[keyi]); } return new String(encoded); } public static void test(String string, String keyString) { String encrypt = crypt(string, keyString); String decrypt = crypt(encrypt, keyString); assert(string.equals(decrypt)); } 
  • test("test","1234");
  • test("test","123");

ps. you can reorganize the method by stretching strings and replacing bytes

 public static byte[] crypt(byte[] bytes, byte[] key) { int size = bytes != null ? bytes.length : 0; final byte[] encoded = new byte[size]; int keySize = key != null ? key.length : 0; // loop on input bytes for (int i = 0; i < size; i++) { // shift key index (assuming key <= bytes) int keyi = i >= keySize ? size % (keySize-1) : i; // pad encoded[i] = (byte) (bytes[i] ^ key[keyi]); } return encoded; } 
0
source

All Articles