/** * initialise the key generator - if strength is set to zero * the key generated will be 64 bits in size, otherwise * strength can be 64 or 56 bits (if you don't count the parity bits). * * @param param the parameters to be used for key generation */ public void init( KeyGenerationParameters param) { super.init(param); if (strength == 0 || strength == (56 / 8)) { strength = DESParameters.DES_KEY_LENGTH; } else if (strength != DESParameters.DES_KEY_LENGTH) { throw new IllegalArgumentException("DES key must be " + (DESParameters.DES_KEY_LENGTH * 8) + " bits long."); } }
public byte[] generateKey() { byte[] newKey = new byte[DESParameters.DES_KEY_LENGTH]; do { random.nextBytes(newKey); DESParameters.setOddParity(newKey); } while (DESParameters.isWeakKey(newKey, 0)); return newKey; }
protected SecretKey engineGenerateSecret( String algorithm) { if (x == null) { throw new IllegalStateException("Diffie-Hellman not initialised."); } String algKey = Strings.toUpperCase(algorithm); byte[] res = bigIntToBytes(result); if (algorithms.containsKey(algKey)) { Integer length = (Integer)algorithms.get(algKey); byte[] key = new byte[length.intValue() / 8]; System.arraycopy(res, 0, key, 0, key.length); if (algKey.startsWith("DES")) { DESParameters.setOddParity(key); } return new SecretKeySpec(key, algorithm); } return new SecretKeySpec(res, algorithm); }
public void performTest() { byte[] k1In = { (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff }; byte[] k1Out = { (byte)0xfe, (byte)0xfe, (byte)0xfe, (byte)0xfe, (byte)0xfe, (byte)0xfe, (byte)0xfe, (byte)0xfe }; byte[] k2In = { (byte)0xef, (byte)0xcb, (byte)0xda, (byte)0x4f, (byte)0xaa, (byte)0x99, (byte)0x7f, (byte)0x63 }; byte[] k2Out = { (byte)0xef, (byte)0xcb, (byte)0xda, (byte)0x4f, (byte)0xab, (byte)0x98, (byte)0x7f, (byte)0x62 }; DESParameters.setOddParity(k1In); for (int i = 0; i != k1In.length; i++) { if (k1In[i] != k1Out[i]) { fail("Failed " + "got " + new String(Hex.encode(k1In)) + " expected " + new String(Hex.encode(k1Out))); } } DESParameters.setOddParity(k2In); for (int i = 0; i != k2In.length; i++) { if (k2In[i] != k2Out[i]) { fail("Failed " + "got " + new String(Hex.encode(k2In)) + " expected " + new String(Hex.encode(k2Out))); } } }
protected SecretKey engineGenerateSecret( KeySpec keySpec) throws InvalidKeySpecException { if (keySpec instanceof PBEKeySpec) { PBEKeySpec pbeSpec = (PBEKeySpec)keySpec; CipherParameters param; if (pbeSpec.getSalt() == null) { return new BCPBEKey(this.algName, this.algOid, scheme, digest, keySize, ivSize, pbeSpec, null); } if (forCipher) { param = PBE.Util.makePBEParameters(pbeSpec, scheme, digest, keySize, ivSize); } else { param = PBE.Util.makePBEMacParameters(pbeSpec, scheme, digest, keySize); } KeyParameter kParam; if (param instanceof ParametersWithIV) { kParam = (KeyParameter)((ParametersWithIV)param).getParameters(); } else { kParam = (KeyParameter)param; } DESParameters.setOddParity(kParam.getKey()); return new BCPBEKey(this.algName, this.algOid, scheme, digest, keySize, ivSize, pbeSpec, param); } throw new InvalidKeySpecException("Invalid KeySpec"); }
private byte[] generateKey() { SecureRandom random = new SecureRandom(); random.setSeed(UUID.randomUUID().toString().getBytes()); KeyGenerationParameters generationParameters = new KeyGenerationParameters(random, DESParameters.DES_KEY_LENGTH * 8); DESKeyGenerator generator = new DESKeyGenerator(); generator.init(generationParameters); return Hex.encode(generator.generateKey()); }