Cryptography Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 4.00
 This site Web DES Algorithm - Operation Modes and JCE SUN Implementation Part:   1  2  3  (Continued from previous part...) ``` KeySpec ks = new DESKeySpec(theKey); SecretKeyFactory kf = SecretKeyFactory.getInstance("DES"); SecretKey ky = kf.generateSecret(ks); Cipher cf = Cipher.getInstance(algorithm); if (theIVp == null) { cf.init(Cipher.ENCRYPT_MODE, ky); } else { AlgorithmParameterSpec aps = new IvParameterSpec(theIVp); cf.init(Cipher.ENCRYPT_MODE, ky, aps); } byte[] theCph = cf.doFinal(theMsg); System.out.println("Key : "+bytesToHex(theKey)); if (theIVp != null) { System.out.println("IV : "+bytesToHex(theIVp)); } 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

Dr. Herong Yang, updated in 2007
Cryptography Tutorials - Herong's Tutorial Notes - DES Algorithm - Operation Modes and JCE SUN Implementation