/** * Creates the RijndaelECB enum value. iv is null. * @param bitmask * @param keyType The type of key the alg requires */ private CryptByteBufferType(int bitmask, KeyType keyType){ this.bitmask = bitmask; this.keyType = keyType; this.cipherName = keyType.alg; this.blockSize = keyType.keySize; this.ivSize = null; algName = name(); isStreamCipher = false; }
/** * Creates the RijndaelPCFB enum value. * @param bitmask * @param ivSize Size of the iv * @param keyType The type of key the alg requires */ private CryptByteBufferType(int bitmask, int ivSize, KeyType keyType){ this.bitmask = bitmask; this.keyType = keyType; this.cipherName = keyType.alg; this.blockSize = keyType.keySize; this.ivSize = ivSize; algName = name(); isStreamCipher = true; }
/** * Creates the RijndaelECB128 enum value. iv is null. * and sets the non-standard blocksize. * @param bitmask * @param keyType The type of key the alg requires * @param blockSize The blocksize the alg uses */ private CryptByteBufferType(int bitmask, KeyType keyType, int blockSize){ this.bitmask = bitmask; this.ivSize = null; this.keyType = keyType; this.cipherName = keyType.alg; this.blockSize = blockSize; algName = name(); isStreamCipher = false; }
@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 testDecryptByteArrayIntIntNullInput() 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.decryptCopy(nullArray, 0, plainText[i].length()); fail("CryptByteBufferType: "+type.name()+": Expected IllegalArgumentException or " + "NullPointerException"); }catch(NullPointerException e) { } catch(IllegalArgumentException e){ } } }
@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){ } } }
@Test public void testDecryptByteArrayIntIntLengthOutOfBounds() 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.decryptCopy(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 testDecryptByteArrayIntIntOffsetOutOfBounds() 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.decryptCopy(Hex.decode(plainText[i]), -3, plainText[i].length()-3); fail("CryptByteBufferType: "+type.name()+": Expected IllegalArgumentException or " + "ArrayIndexOutOfBoundsException"); }catch(IllegalArgumentException e) { } catch (IndexOutOfBoundsException e){ } } }
ByteBuffer decipheredtext2 = crypt.decryptCopy(ciphertext2); ByteBuffer decipheredtext3 = crypt.decryptCopy(ciphertext3); assertTrue("CryptByteBufferType: "+type.name(), plain.equals(decipheredtext1)); assertTrue("CryptByteBufferType: "+type.name(), plain.equals(decipheredtext2)); assertTrue("CryptByteBufferType: "+type.name(), plain.equals(decipheredtext3));
@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)); } }
@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){} } }
@Test public void testDecryptByteArrayNullInput() 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.decryptCopy(nullArray); fail("CryptByteBufferType: "+type.name()+": Expected NullPointerException"); }catch(NullPointerException e){} } }
@Test public void testSuccessfulRoundTripOutOfPlaceOffset() throws GeneralSecurityException { int inHeader = 5; int inFooter = 5; int outHeader = 33; int outFooter = 33; 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[inHeader+originalPlaintext.length+inFooter]; System.arraycopy(originalPlaintext, 0, buffer, inHeader, originalPlaintext.length); byte[] copyBuffer = buffer.clone(); byte[] outBuffer = new byte[outHeader + originalPlaintext.length + outFooter]; crypt.encrypt(buffer, inFooter, originalPlaintext.length, outBuffer, outHeader); assertTrue(Arrays.equals(buffer, copyBuffer)); copyBuffer = outBuffer.clone(); crypt.decrypt(outBuffer, outHeader, originalPlaintext.length, buffer, inFooter); assertTrue(Arrays.equals(copyBuffer, outBuffer)); assertArrayEquals("CryptByteBufferType: "+type.name(), originalPlaintext, Arrays.copyOfRange(buffer, inFooter, inFooter+originalPlaintext.length)); } }
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 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 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); } }