private boolean verifyHeader(byte[] fullHeader) throws IOException, InvalidKeyException { byte[] footer = Arrays.copyOfRange(fullHeader, 0, fullHeader.length-VERSION_AND_MAGIC_LENGTH); int offset = 0; headerEncIV = new byte[type.encryptType.ivSize]; System.arraycopy(footer, offset, headerEncIV, 0, headerEncIV.length); offset += headerEncIV.length; int keySize = type.encryptKey.keySize >> 3; byte[] encryptedKey = new byte[keySize]; System.arraycopy(footer, offset, encryptedKey, 0, keySize); offset += keySize; try { CryptByteBuffer crypt = new CryptByteBuffer(type.encryptType, headerEncKey, headerEncIV); unencryptedBaseKey = KeyGenUtils.getSecretKey(type.encryptKey, crypt.decryptCopy(encryptedKey)); } catch (InvalidKeyException e) { throw new IOException("Error reading encryption keys from header."); } catch (InvalidAlgorithmParameterException e) { throw new IOException("Error reading encryption keys from header."); } byte[] mac = new byte[type.macLen]; System.arraycopy(footer, offset, mac, 0, type.macLen); byte[] ver = ByteBuffer.allocate(4).putInt(version).array(); MessageAuthCode authcode = new MessageAuthCode(type.macType, headerMacKey); return authcode.verifyData(mac, headerEncIV, unencryptedBaseKey.getEncoded(), ver); }
/** Decrypt the specified section of the provided byte[] in-place */ public void decrypt(byte[] input, int offset, int len) { decrypt(input, offset, len, input, offset); }
/** Encrypt the specified section of the provided byte[] in-place */ public void encrypt(byte[] input, int offset, int len) { encrypt(input, offset, len, input, offset); }
@Test public void testSuccessfulRoundTripByteArray() throws GeneralSecurityException { for(int i = 0; i < cipherTypes.length; i++){ CryptByteBufferType type = cipherTypes[i]; CryptByteBuffer crypt; if(ivs[i] == null){ crypt = new CryptByteBuffer(type, keys[i]); } else { crypt = new CryptByteBuffer(type, keys[i], ivs[i]); } byte[] decipheredtext = crypt.decryptCopy(crypt.encryptCopy(Hex.decode(plainText[i]))); assertArrayEquals("CryptByteBufferType: "+type.name(), Hex.decode(plainText[i]), decipheredtext); } }
@Test public void testSuccessfulRoundTripInPlace() throws GeneralSecurityException { for(int i = 0; i < cipherTypes.length; i++){ CryptByteBufferType type = cipherTypes[i]; CryptByteBuffer crypt; if(ivs[i] == null){ crypt = new CryptByteBuffer(type, keys[i]); } else { crypt = new CryptByteBuffer(type, keys[i], ivs[i]); } byte[] buffer = Hex.decode(plainText[i]); byte[] plaintextCopy = buffer.clone(); crypt.encrypt(buffer, 0, buffer.length); assertTrue(!Arrays.equals(buffer, plaintextCopy)); crypt.decrypt(buffer, 0, buffer.length); assertArrayEquals("CryptByteBufferType: "+type.name(), plaintextCopy, buffer); } }
@Test public void testRoundOneByte() throws GeneralSecurityException { for(int i = 0; i < cipherTypes.length; i++){ CryptByteBufferType type = cipherTypes[i]; CryptByteBuffer crypt1; CryptByteBuffer crypt2; if(!type.isStreamCipher) continue; if(ivs[i] == null){ crypt1 = new CryptByteBuffer(type, keys[i]); crypt2 = new CryptByteBuffer(type, keys[i]); } else { crypt1 = new CryptByteBuffer(type, keys[i], ivs[i]); crypt2 = new CryptByteBuffer(type, keys[i], ivs[i]); } byte[] origPlaintext = Hex.decode(plainText[i]); byte[] origCiphertext = crypt1.encryptCopy(origPlaintext); // Now encrypt one byte at a time. byte[] buf = origPlaintext.clone(); for(int j=0;j<buf.length;j++) { crypt2.encrypt(buf, j, 1); assertEquals(buf[j], origCiphertext[j]); } for(int j=0;j<buf.length;j++) { crypt2.decrypt(buf, j, 1); assertEquals(buf[j], origPlaintext[j]); } } }
CryptByteBuffer crypt = new CryptByteBuffer(type.encryptType, headerEncKey, headerEncIV); encryptedKey = crypt.encryptCopy(unencryptedBaseKey.getEncoded()); } catch (InvalidKeyException e) { throw new GeneralSecurityException("Something went wrong with key generation. please "
@Test public void testSetIVIvParameterSpec() throws InvalidKeyException, InvalidAlgorithmParameterException { int i = 4; CryptByteBuffer crypt = new CryptByteBuffer(cipherTypes[i], keys[i], ivs[i]); crypt.genIV(); crypt.setIV(new IvParameterSpec(ivs[i])); assertArrayEquals(ivs[i], crypt.getIV().getIV()); }
@Test public void testOverlappingDecode() throws GeneralSecurityException { for(int i = 0; i < cipherTypes.length; i++){ CryptByteBufferType type = cipherTypes[i]; CryptByteBuffer crypt, crypt2; if(ivs[i] == null){ crypt = new CryptByteBuffer(type, keys[i]); crypt2 = new CryptByteBuffer(type, keys[i]); } else { crypt = new CryptByteBuffer(type, keys[i], ivs[i]); crypt2 = new CryptByteBuffer(type, keys[i], ivs[i]); } byte[] originalPlaintext = Hex.decode(plainText[i]); byte[] originalCiphertext = crypt2.encryptCopy(originalPlaintext); byte[] buf = new byte[originalPlaintext.length+1]; System.arraycopy(originalCiphertext, 0, buf, 0, originalCiphertext.length); crypt.decrypt(buf, 0, originalCiphertext.length, buf, 1); assertArrayEquals(originalPlaintext, Arrays.copyOfRange(buf, 1, buf.length)); } }
@Test public void testOverlappingEncode() throws GeneralSecurityException { for(int i = 0; i < cipherTypes.length; i++){ CryptByteBufferType type = cipherTypes[i]; CryptByteBuffer crypt, crypt2; if(ivs[i] == null){ crypt = new CryptByteBuffer(type, keys[i]); crypt2 = new CryptByteBuffer(type, keys[i]); } else { crypt = new CryptByteBuffer(type, keys[i], ivs[i]); crypt2 = new CryptByteBuffer(type, keys[i], ivs[i]); } byte[] originalPlaintext = Hex.decode(plainText[i]); byte[] originalCiphertext = crypt2.encryptCopy(originalPlaintext); byte[] buf = new byte[originalPlaintext.length+1]; System.arraycopy(originalPlaintext, 0, buf, 0, originalPlaintext.length); crypt.encrypt(buf, 0, originalPlaintext.length, buf, 1); assertArrayEquals(originalCiphertext, Arrays.copyOfRange(buf, 1, buf.length)); } }
@Test public void testGenIV() throws InvalidKeyException, InvalidAlgorithmParameterException { int i = 4; CryptByteBuffer crypt = new CryptByteBuffer(cipherTypes[i], keys[i], ivs[i]); assertNotNull(crypt.genIV()); }
@Test public void testSetIVIvParameterSpecNullInput() throws InvalidKeyException, InvalidAlgorithmParameterException { IvParameterSpec nullInput = null; int i = 4; CryptByteBuffer crypt = new CryptByteBuffer(cipherTypes[i], keys[i], ivs[i]); try{ crypt.setIV(nullInput); fail("Expected InvalidAlgorithmParameterException"); } catch (InvalidAlgorithmParameterException e){} }
@Test public void testGetIV() throws InvalidKeyException, InvalidAlgorithmParameterException { int i = 4; CryptByteBuffer crypt = new CryptByteBuffer(cipherTypes[i], keys[i], ivs[i]); assertArrayEquals(crypt.getIV().getIV(), ivs[i]); }
/** * Encrypts the provided byte[]. If you are using a RijndaelECB * alg then the length of input must equal the block size. * @param input The byte[] to be encrypted * @return The encrypted data. The original data will be unchanged. */ public byte[] encryptCopy(byte[] input){ return encryptCopy(input, 0, input.length); }
/** * Decrypts the provided byte[]. If you are using a RijndaelECB * alg then the length of input must equal the block size. * @param input The byte[] to be decrypted * @return The decrypted plaintext bytes. */ public byte[] decryptCopy(byte[] input){ return decryptCopy(input, 0, input.length); }
byte[] plain = Hex.decode(plainText[i]); if(ivs[i] == null){ crypt = new CryptByteBuffer(type, keys[i]); } else { crypt = new CryptByteBuffer(type, keys[i], ivs[i]); byte[] ciphertext = crypt.encryptCopy(plain); byte[] ciphertext2 = crypt.encryptCopy(plain); byte[] ciphertext3 = crypt.encryptCopy(plain); crypt = new CryptByteBuffer(type, keys[i]); } else { crypt = new CryptByteBuffer(type, keys[i], ivs[i]); byte[] decipheredtext = crypt.decryptCopy(ciphertext); assertArrayEquals("CryptByteBufferType: "+type.name(), plain, decipheredtext); decipheredtext = crypt.decryptCopy(ciphertext2); assertArrayEquals("CryptByteBufferType2: "+type.name(), plain, decipheredtext); decipheredtext = crypt.decryptCopy(ciphertext3); assertArrayEquals("CryptByteBufferType3: "+type.name(), plain, decipheredtext);
@Test public void testSuccessfulRoundTripInPlaceOffset() throws GeneralSecurityException { int header = 5; int footer = 5; for(int i = 0; i < cipherTypes.length; i++){ CryptByteBufferType type = cipherTypes[i]; CryptByteBuffer crypt; if(ivs[i] == null){ crypt = new CryptByteBuffer(type, keys[i]); } else { crypt = new CryptByteBuffer(type, keys[i], ivs[i]); } byte[] originalPlaintext = Hex.decode(plainText[i]); byte[] buffer = new byte[header+originalPlaintext.length+footer]; byte[] copyBuffer = buffer.clone(); System.arraycopy(originalPlaintext, 0, buffer, header, originalPlaintext.length); crypt.encrypt(buffer, footer, originalPlaintext.length); assertTrue(!Arrays.equals(buffer, copyBuffer)); crypt.decrypt(buffer, footer, originalPlaintext.length); assertArrayEquals("CryptByteBufferType: "+type.name(), originalPlaintext, Arrays.copyOfRange(buffer, footer, footer+originalPlaintext.length)); } }
crypt1 = new CryptByteBuffer(type, keys[i]); crypt2 = new CryptByteBuffer(type, keys[i]); } else { crypt1 = new CryptByteBuffer(type, keys[i], ivs[i]); crypt2 = new CryptByteBuffer(type, keys[i], ivs[i]); byte[] origCiphertext = crypt1.encryptCopy(origPlaintext); int r = buf.length - j; int copy = 1 + (r == 1 ? 0 : random.nextInt(r-1)); crypt2.encrypt(buf, j, copy); j += copy; int r = buf.length - j; int copy = 1 + (r == 1 ? 0 : random.nextInt(r-1)); crypt2.decrypt(buf, j, copy); j += copy;
CryptByteBuffer crypt = new CryptByteBuffer(type.encryptType, headerEncKey, headerEncIV); encryptedKey = crypt.encryptCopy(unencryptedBaseKey.getEncoded()); } catch (InvalidKeyException e) { throw new GeneralSecurityException("Something went wrong with key generation. please "