/*
* I found out that the Java API includes a Des encrypter.
*/
import java.security.spec.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import java.util.Scanner;
class Des {
public static void main(String[] a) {
Scanner sc = new Scanner(System.in);
String input;
String key;
System.out.println("Enter Your Message (8 Characters Long)");
input = sc.nextLine();
System.out.println("Enter your key (8 characters long)");
key = sc.nextLine();
if (input.length() != 8 || key.length() != 8) {
System.out.println("I Said 8 Characters");
return;
}
try {
byte[] theKey = null;
byte[] theMsg = null;
/*
* Assuming 1 Byte = 8 bits.
*/
theKey = key.getBytes();
theMsg = input.getBytes();
KeySpec ks = new DESKeySpec(theKey);
SecretKeyFactory kf = SecretKeyFactory.getInstance("DES");
SecretKey ky = kf.generateSecret(ks);
Cipher cf = Cipher.getInstance("DES/ECB/NoPadding");
cf.init(Cipher.ENCRYPT_MODE, ky);
byte[] theCph = cf.doFinal(theMsg);
String cipher = new String(theCph);
System.out.println("Key : " + key);
System.out.println("Message : " + input);
System.out.println("\nString of the Cipher ");
System.out.println("\tCipher : " + cipher);
System.out.println("Hexidecimal of the Cipher ");
System.out.println("\tCipher : " + getHexString(theCph));
} catch (Exception e) {
e.printStackTrace(); // Didn't want to fuss with exception checking.
return;
}
}
public static String getHexString(byte[] b) throws Exception {
String result = "";
for (int i = 0; i < b.length; i++) {
result += Integer.toString((b[i] & 0xff) + 0x100, 16).substring(1);
}
return result;
}
}