File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

DES Decryption and Key Finding

 
Alex Day
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Pie
Posts: 1166
17
Java Linux Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and the given 2 digit bytes are in hex format
 
Richard Tookey
Bartender
Pie
Posts: 1166
17
Java Linux Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4660
5
Linux Mac OS X VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic