aspose file tools*
The moose likes Java in General and the fly likes DES Decryption and Key Finding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "DES Decryption and Key Finding" Watch "DES Decryption and Key Finding" New topic
Author

DES Decryption and Key Finding

Alex Day
Greenhorn

Joined: Feb 10, 2013
Posts: 5
Given a cipher and and key given in the code, Im supposed to find the plaintext, I don't think I'm doing it quite right because my result isn't an actual word its just 8 random letters and its supposed to be a word.
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;


public class FindthePlainText
{

public static void printByteArray(byte [] array)
{

for(int i = 0; i < array.length-1; i++)
{
System.out.print(array[i]);
}}

public static void main(String[] args)
{
Cipher cipher;
SecretKeySpec key;

byte [] keyBytes;
byte [] pt;
byte [] ct;


keyBytes = new byte[] {(byte) 0xFE, (byte) 0xDC, (byte) 0xBA, (byte) 0x98, (byte) 0x76, (byte) 0x54, (byte) 0x32, (byte) 0x10};

ct = new byte[] {(byte) 0x2C, (byte) 0xE6, (byte) 0xDD, (byte) 0xA4, (byte) 0x98, (byte) 0xCA, (byte) 0xBA, (byte) 0x99};


key = new SecretKeySpec(keyBytes, "DES");

try
{

cipher = Cipher.getInstance("DES/ECB/NoPadding");


cipher.init(Cipher.DECRYPT_MODE, key);


pt = cipher.doFinal(ct);


System.out.print(pt);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}

And also given a cipher and plaintext I'm supposed to find the key, and have this so far but am getting errors:


import java.security.KeyRep;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;


public class FindtheKey
{

public static void printByteArray(byte[] array)
{

for(int i = 0; i < array.length-1; i++)
{

System.out.print(Integer.toHexString((array[i]>>4)&0x0F).toUpperCase());
System.out.print(Integer.toHexString(array[i]&0x0F).toUpperCase() + ", ");
}
System.out.print(Integer.toHexString(array[array.length-1]>>4&0x0F).toUpperCase());
System.out.println(Integer.toHexString(array[array.length-1]&0x0F).toUpperCase() + "]");
}


public static void main(String[] args)
{
Cipher cipher;
SecretKeySpec key;

byte [] keyBytes;
String pt;
byte [] ct;


ct = new byte[] {(byte) 0x4A, (byte) 0xC4, (byte) 0x55, (byte) 0x3D, (byte) 0xB3, (byte) 0x37, (byte) 0xCA, (byte) 0xB3};


pt = "Plaintxt";
byte[] p= pt.getBytes();
keyBytes= new byte[] {(byte) 0x01, (byte) 0x23, (byte) 0x45, (byte) 0x67};

key = new SecretKeySpec(keyBytes, "DES");
try
{

cipher = Cipher.getInstance("DES/ECB/NoPadding");


cipher.init(Cipher.UNWRAP_MODE, key);


keyBytes = cipher.doFinal(p);


printByteArray(keyBytes);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
Richard Tookey
Ranch Hand

Joined: Aug 27, 2012
Posts: 1067
    
  10

I can see several things that are definitely wrong with your code ( for ecxample the line System.out.print(pt) does not do what you think it does ) but you are asking us to deduce your homework requirement from your probably flawed precis of the requirement and code that does not work. Even though I understand the JCE I for one cannot from what you have posted deduce the original homework requirement so cannot point out where you are going wrong.

If you post the text of you homework and not just your interpretation of it then maybe somebody here will be able to point out where you are going wrong.
Alex Day
Greenhorn

Joined: Feb 10, 2013
Posts: 5
Okay here is the original assignment:
1. Decryption
Given are:
8 characters of ciphertext: 2C, E6, DD, A4, 98, CA, BA, B9
Note: This is again exactly one block size.
Key bytes: FE, DC, BA, 98, 76, 54, 32, 10
What is the decoded plaintext? Specify it as a character string (using ASCII).
Note: If everything worked fine, the plaintext should be really plain text, i.e., perfectly
readable.


2. Finding the Key
Given are:
Plaintext in ASCII: "Plaintxt "
The corresponding ciphertext: 4A, C4, 55, 3D, B3, 37, CA, B3
The first four bytes of the Key: 01, 23, 45, 67
What is the complete key used in this encryption?
Alex Day
Greenhorn

Joined: Feb 10, 2013
Posts: 5
and the given 2 digit bytes are in hex format
Richard Tookey
Ranch Hand

Joined: Aug 27, 2012
Posts: 1067
    
  10


The first task has two errors. One I have already pointed out (you need to understand what the toString() method of an array actually does) and the other is a result of your carelessness in typing.

Your solution to the second task is totally wrong since it should not involve wrapped keys. You have been given the first 4 bytes of a DES key and seem to be required to iterate through all possible values for the remaining 4 bytes of the key until you get a ciphertext match. Note - a DES key uses the least significant bit of each byte as an odd parity bit but the JCE does not actually care what value it is so you only have to worry about the first 7 bits if each of the 4 remaining bytes of the key.

I wish the admin people would stop messing around with the site! This is the third time I have had to type this response!
Alex Day
Greenhorn

Joined: Feb 10, 2013
Posts: 5
Okay I caught my mistake in the last keybyte entry and switched it to B9 and changed my print statement to look like this, but I don't think that's right becuase its giving me 18 numbers now when I run it. Was I previously printing out the arrays location in memory or something like that?
try
{

cipher = Cipher.getInstance("DES/ECB/NoPadding");


cipher.init(Cipher.DECRYPT_MODE, key);


pt = cipher.doFinal(ct);



for(int i = 0; i < pt.length-1; i++)
{
System.out.print(pt[i]);
}

}
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

DES and most other ciphers do not work on Java Strings. They work on arrays of octets. You can usually use an array of bytes, but the answer is never going to be a String object.
Alex Day
Greenhorn

Joined: Feb 10, 2013
Posts: 5
I got the first problem figured out so now just working on the 2nd, thanks for the help
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: DES Decryption and Key Finding