Java examples for Security:DES
Converting a 56-bit Value to a DES Key
import javax.crypto.SecretKey; import javax.crypto.spec.DESKeySpec; import javax.crypto.spec.SecretKeySpec; public class Main { public static void m() { // Get the 56-bit value byte[] raw = new byte[] { 0x01, 0x72, 0x43, 0x3E, 0x1C, 0x7A, 0x55 }; byte[] keyBytes = addParity(raw); // You can check that the parity has been set properly try {// w w w .ja v a2s. com boolean b = DESKeySpec.isParityAdjusted(keyBytes, 0); } catch (java.security.InvalidKeyException e) { // The DES is invalid } // Convert the bytes into a SecretKey suitable for use by Cipher SecretKey key = new SecretKeySpec(keyBytes, "DES"); } public static byte[] addParity(byte[] in) { byte[] result = new byte[8]; // Keeps track of the bit position in the result int resultIx = 1; // Used to keep track of the number of 1 bits in each 7-bit chunk int bitCount = 0; // Process each of the 56 bits for (int i = 0; i < 56; i++) { // Get the bit at bit position i boolean bit = (in[6 - i / 8] & (1 << (i % 8))) > 0; // If set, set the corresponding bit in the result if (bit) { result[7 - resultIx / 8] |= (1 << (resultIx % 8)) & 0xFF; bitCount++; } // Set the parity bit after every 7 bits if ((i + 1) % 7 == 0) { if (bitCount % 2 == 0) { // Set low-order bit (parity bit) if bit count is even result[7 - resultIx / 8] |= 1; } resultIx++; bitCount = 0; } resultIx++; } return result; } }