DES Algorithm - Java Implementation
Part:
1
2
3
4
(Continued from previous part...)
Test Cases of DES Encryption and Decryption
Once I got my DES implementation working, I created the following testing Java
program to performed several test cases so that I can validate the results
with other DES implementations:
/* CipherDesTest.java
* Copyright (c) 2002 by Dr. Herong Yang, http://www.herongyang.com/
*/
import java.io.*;
import herong.CipherDES;
class CipherDesTest {
public static void main(String[] a) {
if (a.length<1) {
System.out.println("Usage:");
System.out.println("java CipherDesTest 1");
return;
}
String test = a[0];
try {
byte[] theKey = null;
byte[] theMsg = null;
byte[] theExp = null;
if (test.equals("1")) {
theKey = hexToBytes("133457799BBCDFF1");
theMsg = hexToBytes("0123456789ABCDEF");
theExp = hexToBytes("85E813540F0AB405");
} else if (test.equals("2")) {
theKey = hexToBytes("38627974656B6579"); // "8bytekey"
theMsg = hexToBytes("6D6573736167652E"); // "message."
theExp = hexToBytes("7CF45E129445D451");
} else {
System.out.println("Usage:");
System.out.println("java CipherDesTest 1");
return;
}
byte[][] subKeys = CipherDES.getSubkeys(theKey);
byte[] theCph = CipherDES.cipher(theMsg,subKeys,"encrypt");
System.out.println("Key : "+bytesToHex(theKey));
System.out.println("Message : "+bytesToHex(theMsg));
System.out.println("Cipher : "+bytesToHex(theCph));
System.out.println("Expected: "+bytesToHex(theExp));
} catch (Exception e) {
e.printStackTrace();
return;
}
}
public static byte[] hexToBytes(String str) {
if (str==null) {
return null;
} else if (str.length() < 2) {
return null;
} else {
int len = str.length() / 2;
byte[] buffer = new byte[len];
for (int i=0; i<len; i++) {
buffer[i] = (byte) Integer.parseInt(
str.substring(i*2,i*2+2),16);
}
return buffer;
}
}
public static String bytesToHex(byte[] data) {
if (data==null) {
return null;
} else {
int len = data.length;
String str = "";
for (int i=0; i<len; i++) {
if ((data[i]&0xFF)<16) str = str + "0"
+ java.lang.Integer.toHexString(data[i]&0xFF);
else str = str
+ java.lang.Integer.toHexString(data[i]&0xFF);
}
return str.toUpperCase();
}
}
}
Note that this program, CipherDesTest.java, can be easily modified to add other testing cases.
To run CipherDesTest.java, you need compile CipherDES.java into a package called "herong", and run
them together.
Case 1 - Taken from The DES Algorithm Illustrated:
java -cp . CipherDesTest 1
Key : 133457799BBCDFF1
Message : 0123456789ABCDEF
Cipher : 85E813540F0AB405
Expected: 85E813540F0AB405
Case 2 - Modified from DES Source Code:
java -cp . CipherDesTest 2
Key : 38627974656B6579
Message : 6D6573736167652E
Cipher : 7CF45E129445D451
Expected: 7CF45E129445D451
Exercise: Improve this program to handle multiple input blocks.
Part:
1
2
3
4
|