/** * 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); }
/** * Encrypts the provided ByteBuffer, returning a new ByteBuffer. Only reads the bytes that are * actually readable, i.e. from position to limit, so equivalent to get()ing into a buffer, * encrypting that and returning. 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 A new ByteBuffer containing the ciphertext. It will have a backing array and its * arrayOffset() will be 0, its position will be 0 and its capacity will be the length of the * input data. */ public ByteBuffer encryptCopy(ByteBuffer input){ if(input.hasArray()) return ByteBuffer.wrap(encryptCopy(input.array(), input.arrayOffset() + input.position(), input.remaining())); else { return ByteBuffer.wrap(encryptCopy(Fields.copyToArray(input))); } }
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 "
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 testEncryptDirectByteBuffer() throws GeneralSecurityException { for(int i = 0; i < cipherTypes.length; i++){ CryptByteBufferType type = cipherTypes[i]; CryptByteBuffer crypt; byte[] origPlaintext = Hex.decode(plainText[i]); ByteBuffer plaintext = ByteBuffer.allocateDirect(origPlaintext.length); plaintext.clear(); plaintext.put(origPlaintext); plaintext.clear(); if(ivs[i] == null){ crypt = new CryptByteBuffer(type, keys[i]); } else { crypt = new CryptByteBuffer(type, keys[i], ivs[i]); } ByteBuffer ciphertext = crypt.encryptCopy(plaintext); plaintext.clear(); byte[] copyPlaintext = new byte[origPlaintext.length]; plaintext.get(copyPlaintext); assertTrue(Arrays.equals(origPlaintext, copyPlaintext)); // Plaintext not modified. plaintext.clear(); assertEquals(ciphertext.remaining(), origPlaintext.length); ByteBuffer deciphered = crypt.decryptCopy(ciphertext); assertTrue(deciphered.equals(plaintext)); byte[] data = new byte[origPlaintext.length]; deciphered.get(data); assertTrue(Arrays.equals(data, origPlaintext)); } }
ByteBuffer ciphertext = crypt.encryptCopy(plaintext); byte[] altCiphertext = crypt2.encryptCopy(origPlaintext); byte[] ciphertextBytes = new byte[origPlaintext.length]; ciphertext.get(ciphertextBytes);
@Test public void testEncryptByteArrayIntIntNullInput() 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[] nullArray = null; try{ crypt.encryptCopy(nullArray, 0, plainText[i].length()); fail("CryptByteBufferType: "+type.name()+": Expected IllegalArgumentException or " + "NullPointerException"); }catch(IllegalArgumentException e) { }catch(NullPointerException e){} } }
@Test public void testDecryptWrapByteBuffer() throws GeneralSecurityException { int header = 5; int footer = 5; for(int i = 0; i < cipherTypes.length; i++){ CryptByteBufferType type = cipherTypes[i]; CryptByteBuffer crypt; byte[] origPlaintext = Hex.decode(plainText[i]); byte[] buf = origPlaintext.clone(); if(ivs[i] == null){ crypt = new CryptByteBuffer(type, keys[i]); } else { crypt = new CryptByteBuffer(type, keys[i], ivs[i]); } ByteBuffer plaintext = ByteBuffer.wrap(buf); ByteBuffer ciphertext = crypt.encryptCopy(plaintext); assertTrue(Arrays.equals(buf, origPlaintext)); // Plaintext not modified. assertEquals(ciphertext.remaining(), origPlaintext.length); byte[] decryptBuf = new byte[header+origPlaintext.length+footer]; ciphertext.get(decryptBuf, header, origPlaintext.length); byte[] copyOfDecryptBuf = decryptBuf.clone(); ByteBuffer toDecipher = ByteBuffer.wrap(decryptBuf, header, origPlaintext.length); ByteBuffer deciphered = crypt.decryptCopy(toDecipher); assertTrue(Arrays.equals(decryptBuf, copyOfDecryptBuf)); assertTrue(deciphered.equals(plaintext)); byte[] data = new byte[origPlaintext.length]; deciphered.get(data); assertTrue(Arrays.equals(data, origPlaintext)); } }
@Test public void testEncryptByteArrayIntIntLengthOutOfBounds() 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]); } try{ crypt.encryptCopy(Hex.decode(plainText[i]), 0, plainText[i].length()+3); fail("CryptByteBufferType: "+type.name()+": Expected IllegalArgumentException or " + "ArrayIndexOutOfBoundsException"); }catch(IllegalArgumentException e) { } catch(IndexOutOfBoundsException e){} } }
@Test public void testEncryptByteArrayIntIntOffsetOutOfBounds() 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]); } try{ crypt.encryptCopy(Hex.decode(plainText[i]), -3, plainText[i].length()-3); fail("CryptByteBufferType: "+type.name()+": Expected IllegalArgumentException or " + "ArrayIndexOutOfBoundsException"); }catch(IllegalArgumentException e) { }catch(IndexOutOfBoundsException e){ } } }
crypt = new CryptByteBuffer(type, keys[i], ivs[i]); ByteBuffer ciphertext1 = crypt.encryptCopy(plain); ByteBuffer ciphertext2 = crypt.encryptCopy(plain); ByteBuffer ciphertext3 = crypt.encryptCopy(plain); assertEquals(ciphertext1.capacity(), len); assertEquals(ciphertext2.capacity(), len);
@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 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 testEncryptByteArrayNullInput() 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[] nullArray = null; try{ crypt.encryptCopy(nullArray); fail("CryptByteBufferType: "+type.name()+": Expected NullPointerException"); }catch(NullPointerException e){} } }
crypt = new CryptByteBuffer(type, keys[i], ivs[i]); byte[] ciphertext = crypt.encryptCopy(plain); byte[] ciphertext2 = crypt.encryptCopy(plain); byte[] ciphertext3 = crypt.encryptCopy(plain);
byte[] origCiphertext = crypt1.encryptCopy(origPlaintext);
@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 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]); } } }