@Test public void testAsBinaryDecoder() throws DecoderException { final BinaryDecoder decoder = (BinaryDecoder) RareBinaryDecoderProxy.newInstance(); try { decoder.decode((byte[]) null); Assert.fail("passed.decode((byte[]) null)"); } catch (final NullPointerException npe) { // expected } final byte[] expected = new byte[0]; final byte[] actual = decoder.decode(expected); Assert.assertEquals(actual, expected); }
@Test public void testAsBinaryDecoder() throws DecoderException { final BinaryDecoder decoder = (BinaryDecoder) HexBinaryDecoderProxy.newInstance(); try { decoder.decode((byte[]) null); Assert.fail("decode((byte[]) null)"); } catch (final NullPointerException npe) { // expected } final byte[] encoded = Tests.encodedBytes(); final byte[] decoded = decoder.decode(encoded); }
protected static WebArchive getDefaultDeployment(TestContext context) { context.setAppEngineWebXmlFile("appengine-web-with-logging-properties.xml"); WebArchive war = getTckDeployment(context); war.addClasses(LoggingTestBase.class, TestBase.class) // classes for Base64.isBase64() .addClasses(Base64.class, BaseNCodec.class) .addClasses(BinaryEncoder.class, Encoder.class) .addClasses(BinaryDecoder.class, Decoder.class) .addClasses(EncoderException.class, DecoderException.class) .addAsWebInfResource("currentTimeUsec.jsp") .addAsWebInfResource("doNothing.jsp") .addAsWebInfResource("storeTestData.jsp") .addAsWebInfResource("throwException.jsp") .addAsWebInfResource("log4j-test.properties") .addAsWebInfResource("logging-all.properties"); return war; }
/** * Tests to make sure Base64's implementation of the org.apache.commons.codec.BinaryDecoder * interface is behaving identical to commons-codec-1.3.jar. * * @throws DecoderException problem */ @Test public void testBinaryDecoder() throws DecoderException { final BinaryDecoder dec = new Base64(); for (int i = 0; i < STRINGS.length; i++) { if (STRINGS[i] != null) { final byte[] base64 = utf8(STRINGS[i]); final byte[] binary = BYTES[i]; final boolean b = Arrays.equals(binary, dec.decode(base64)); assertTrue("BinaryDecoder test-" + i, b); } } }
private <TCoder extends BinaryEncoder & BinaryDecoder> void doTest(String plain, String key, String iv, TCoder coder) { AESCipher cipher = new AESCipher(); if (coder != null) { cipher.setCoder(coder); } cipher.setKey(key, iv); // Encrypt. String encrypted = cipher.encrypt(plain); if (iv == null) { byte[] iv2 = cipher.getCipher().getIV(); cipher.setKey(key, iv2); } // Decrypt. String decrypted = cipher.decrypt(encrypted); if (DEBUG) { System.out.println("----------"); System.out.println("codec = " + ((coder != null) ? coder.getClass().getSimpleName() : "default")); System.out.println("plain = " + plain); System.out.println("encrypted = " + encrypted); System.out.println("decrypted = " + decrypted); } assertNotEquals(plain, encrypted); assertEquals(plain, decrypted); }
protected static WebArchive getHelperDeployment() { WebArchive war = getTckDeployment(); war.addClass(DatastoreHelperTestBase.class) .addClasses(Base64.class, BaseNCodec.class) .addClasses(BinaryEncoder.class, Encoder.class) .addClasses(BinaryDecoder.class, Decoder.class) .addClasses(EncoderException.class, DecoderException.class); return war; }
/** * Constructor with a cipher, an encoder and a decoder. * * @param cipher * A cipher. If {@code null} is given, {@link #setCipher(Cipher)} * must be called later with a valid cipher. * * @param encoder * An encoder used in {@link #encrypt(String)} and * {@link #encrypt(byte[])} to encode an encrypted byte array. * If {@code null} is given, {@link Base64} is used as the default * encoder. * * @param decoder * A decoder used in {@link #decrypt(String)} and * {@link #decrypt(byte[])} to decode an encoded input byte array. * If {@code null} is given, {@link Base64} is used as the default * decoder. */ public CodecCipher(Cipher cipher, BinaryEncoder encoder, BinaryDecoder decoder) { this.cipher = cipher; this.encoder = encoder; this.decoder = decoder; }
/** * Set a decoder. * * @param decoder * A decoder used by {@link #decrypt(String)} and * {@link #decrypt(byte[])}. * * @return * {@code this} object. */ public CodecCipher setDecoder(BinaryDecoder decoder) { this.decoder = decoder; return this; }
/** * Set a coder. * * @param coder * A coder which works as both an encoder and a decoder. * If {@code null} is given, {@link Base64} is used as the * default coder. * * @return * {@code this} object. */ public <TCoder extends BinaryEncoder & BinaryDecoder> CodecCipher setCoder(TCoder coder) { this.encoder = coder; this.decoder = coder; return this; }
/** * Constructor with an encoder and a decoder. * * <p> * This constructor just performs {@link CodecCipher#CodecCipher(String, * BinaryEncoder, BinaryDecoder) super("AES/CBC/PKCS5Padding", encoder, decoder)}. * </p> * * @param encoder * An encoder used in {@link #encrypt(String) encrypt(String)} and * {@link #encrypt(byte[]) encrypt(byte[])} to encode an encrypted byte array. * If {@code null} is given, {@link Base64} is used as the default * encoder. * * @param decoder * A decoder used in {@link #decrypt(String) decrypt(String)} and * {@link #decrypt(byte[]) decrypt(byte[])} to decode an encoded input byte array. * If {@code null} is given, {@link Base64} is used as the default * decoder. */ public AESCipher(BinaryEncoder encoder, BinaryDecoder decoder) { super(TRANSFORMATION, encoder, decoder); }
/** * Constructor with a transformation, an encoder and a decoder. * * <p> * This constructor just performs {@link CodecCipher#CodecCipher(String, * BinaryEncoder, BinaryDecoder) super(transformation, encoder, decoder)}. * </p> * * @param encoder * An encoder used in {@link #encrypt(String) encrypt(String)} and * {@link #encrypt(byte[]) encrypt(byte[])} to encode an encrypted byte array. * If {@code null} is given, {@link Base64} is used as the default * encoder. * * @param decoder * A decoder used in {@link #decrypt(String) decrypt(String)} and * {@link #decrypt(byte[]) decrypt(byte[])} to decode an encoded input byte array. * If {@code null} is given, {@link Base64} is used as the default * decoder. * * @since 1.3 */ public AESCipher(String transformation, BinaryEncoder encoder, BinaryDecoder decoder) { super(transformation, encoder, decoder); }
/** * Constructor with a coder. * * <p> * This constructor just performs {@code super("AES/CBC/PKCS5Padding", coder)}. * </p> * * @param coder * A coder which works as both an encoder and a decoder. * If {@code null} is given, {@link Base64} is used as the * default coder. */ public <TCoder extends BinaryEncoder & BinaryDecoder> AESCipher(TCoder coder) { super(TRANSFORMATION, coder); }
/** * Constructor with a transformation and a coder. * * <p> * This constructor just performs {@code super(transformation, coder)}. * </p> * * @param coder * A coder which works as both an encoder and a decoder. * If {@code null} is given, {@link Base64} is used as the * default coder. * * @since 1.3 */ public <TCoder extends BinaryEncoder & BinaryDecoder> AESCipher(String transformation, TCoder coder) { super(transformation, coder); }
/** * Constructor with a cipher and a coder. * * <p> * This constructor is an alias of {@link #CodecCipher(Cipher, BinaryEncoder, BinaryDecoder) * CodecCipher(cipher, coder, coder)}. * </p> * * @param cipher * A cipher. If {@code null} is given, {@link #setCipher(Cipher)} * must be called later with a valid cipher. * * @param coder * A coder which works as both an encoder and a decoder. * If {@code null} is given, {@link Base64} is used as the * default coder. */ public <TCoder extends BinaryEncoder & BinaryDecoder> CodecCipher(Cipher cipher, TCoder coder) { this(cipher, coder, coder); }
/** * Constructor with a cipher. * * <p> * This constructor is an alias of {@link #CodecCipher(Cipher, BinaryEncoder, BinaryDecoder) * CodecCipher(cipher, (BinaryEncoder)null, (BinaryDecoder)null)}. * </p> * * @param cipher * A cipher. If {@code null} is given, {@link #setCipher(Cipher)} * must be called later with a valid cipher. */ public CodecCipher(Cipher cipher) { this(cipher, (BinaryEncoder)null, (BinaryDecoder)null); }
/** * Constructor with a transformation, an encoder and a decoder. * * @param transformation * A transformation in the form of either <code>"<i>algorithm/mode/padding</i>"</code> * or <code>"<i>algorithm</i>"</code>. For example, <code>"AES/CBC/PKCS5Padding"</code>. * * @param encoder * An encoder used in {@link #encrypt(String)} and * {@link #encrypt(byte[])} to encode an encrypted byte array. * If {@code null} is given, {@link Base64} is used as the default * encoder. * * @param decoder * A decoder used in {@link #decrypt(String)} and * {@link #decrypt(byte[])} to decode an encoded input byte array. * If {@code null} is given, {@link Base64} is used as the default * decoder. * * @throws IllegalArgumentException * The given transformation is not supported. This exception wraps * the original exception (such as {@link java.security.NoSuchAlgorithmException * NoSuchAlgorithmException} and {@link javax.crypto.NoSuchPaddingException * NoSuchPaddingException}) as the cause. * * @see <a href="http://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#Cipher" * >Cipher (Encryption) Algorithms</a> * * @see StandardCipherTransformations */ public CodecCipher(String transformation, BinaryEncoder encoder, BinaryDecoder decoder) throws IllegalArgumentException { this(getCipherInstance(transformation), encoder, decoder); }
/** * Constructor with a transformation and a coder. * * <p> * This constructor is an alias of {@link #CodecCipher(String, BinaryEncoder, BinaryDecoder) * CodecCipher(transformation, coder, coder)}. * </p> * * @param transformation * A transformation in the form of either <code>"<i>algorithm/mode/padding</i>"</code> * or <code>"<i>algorithm</i>"</code>. For example, <code>"AES/CBC/PKCS5Padding"</code>. * * @param coder * A coder which works as both an encoder and a decoder. * If {@code null} is given, {@link Base64} is used as the * default coder. * * @throws IllegalArgumentException * The given transformation is not supported. This exception wraps * the original exception (such as {@link java.security.NoSuchAlgorithmException * NoSuchAlgorithmException} and {@link javax.crypto.NoSuchPaddingException * NoSuchPaddingException}) as the cause. * * @see <a href="http://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#Cipher" * >Cipher (Encryption) Algorithms</a> * * @see StandardCipherTransformations */ public <TCoder extends BinaryEncoder & BinaryDecoder> CodecCipher(String transformation, TCoder coder) throws IllegalArgumentException { this(transformation, coder, coder); }
/** * Constructor with a transformation. * * <p> * This constructor is an alias of {@link #CodecCipher(String, BinaryEncoder, BinaryDecoder) * CodecCipher(transformation, (BinaryEncoder)null, (BinaryDecoder)null)}. * </p> * * @param transformation * A transformation in the form of either <code>"<i>algorithm/mode/padding</i>"</code> * or <code>"<i>algorithm</i>"</code>. For example, <code>"AES/CBC/PKCS5Padding"</code>. * * @throws IllegalArgumentException * The given transformation is not supported. This exception wraps * the original exception (such as {@link java.security.NoSuchAlgorithmException * NoSuchAlgorithmException} and {@link javax.crypto.NoSuchPaddingException * NoSuchPaddingException}) as the cause. * * @see <a href="http://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#Cipher" * >Cipher (Encryption) Algorithms</a> * * @see StandardCipherTransformations */ public <TCoder extends BinaryEncoder & BinaryDecoder> CodecCipher(String transformation) throws IllegalArgumentException { this(transformation, (BinaryEncoder)null, (BinaryDecoder)null); }
/** * Constructor with a transformation, a provider, an encoder and a decoder. * * @param transformation * A transformation in the form of either <code>"<i>algorithm/mode/padding</i>"</code> * or <code>"<i>algorithm</i>"</code>. For example, <code>"AES/CBC/PKCS5Padding"</code>. * * @param provider * A name of provider which provides the implementation of the * transformation. * * @param encoder * An encoder used in {@link #encrypt(String)} and * {@link #encrypt(byte[])} to encode an encrypted byte array. * If {@code null} is given, {@link Base64} is used as the default * encoder. * * @param decoder * A decoder used in {@link #decrypt(String)} and * {@link #decrypt(byte[])} to decode an encoded input byte array. * If {@code null} is given, {@link Base64} is used as the default * decoder. * * @throws IllegalArgumentException * The given transformation is not supported. This exception wraps * the original exception (such as {@link java.security.NoSuchAlgorithmException * NoSuchAlgorithmException}, {@link javax.crypto.NoSuchPaddingException * NoSuchPaddingException} and {@link java.security.NoSuchProviderException * NoSuchProviderException}) as the cause. * * @see <a href="http://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#Cipher" * >Cipher (Encryption) Algorithms</a> * * @see StandardCipherTransformations */ public CodecCipher(String transformation, String provider, BinaryEncoder encoder, BinaryDecoder decoder) throws IllegalArgumentException { this(getCipherInstance(transformation, provider), encoder, decoder); }
/** * Constructor with a transformation, a provider and a coder. * * <p> * This constructor is an alias of {@link #CodecCipher(String, String, BinaryEncoder, BinaryDecoder) * CodecCipher(transformation, provider, coder, coder)}. * </p> * * @param transformation * A transformation in the form of either <code>"<i>algorithm/mode/padding</i>"</code> * or <code>"<i>algorithm</i>"</code>. For example, <code>"AES/CBC/PKCS5Padding"</code>. * * @param provider * A name of provider which provides the implementation of the * transformation. * * @param coder * A coder which works as both an encoder and a decoder. * If {@code null} is given, {@link Base64} is used as the * default coder. * * @throws IllegalArgumentException * The given transformation is not supported. This exception wraps * the original exception (such as {@link java.security.NoSuchAlgorithmException * NoSuchAlgorithmException}, {@link javax.crypto.NoSuchPaddingException * NoSuchPaddingException} and {@link java.security.NoSuchProviderException * NoSuchProviderException}) as the cause. * * @see <a href="http://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#Cipher" * >Cipher (Encryption) Algorithms</a> * * @see StandardCipherTransformations */ public <TCoder extends BinaryEncoder & BinaryDecoder> CodecCipher(String transformation, String provider, TCoder coder) throws IllegalArgumentException { this(transformation, provider, coder, coder); }
/** * Constructor with a transformation and a provider. * * <p> * This constructor is an alias of {@link #CodecCipher(String, String, BinaryEncoder, BinaryDecoder) * CodecCipher(transformation, provider, (BinaryEncoder)null, (BinaryDecoder)null)}. * </p> * * @param transformation * A transformation in the form of either <code>"<i>algorithm/mode/padding</i>"</code> * or <code>"<i>algorithm</i>"</code>. For example, <code>"AES/CBC/PKCS5Padding"</code>. * * @param provider * A name of provider which provides the implementation of the * transformation. * * @throws IllegalArgumentException * The given transformation is not supported. This exception wraps * the original exception (such as {@link java.security.NoSuchAlgorithmException * NoSuchAlgorithmException}, {@link javax.crypto.NoSuchPaddingException * NoSuchPaddingException} and {@link java.security.NoSuchProviderException * NoSuchProviderException}) as the cause. * * @see <a href="http://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#Cipher" * >Cipher (Encryption) Algorithms</a> * * @see StandardCipherTransformations */ public CodecCipher(String transformation, String provider) throws IllegalArgumentException { this(transformation, provider, (BinaryEncoder)null, (BinaryDecoder)null); }
/** * Constructor with a transformation, a provider, an encoder and a decoder. * * @param transformation * A transformation in the form of either <code>"<i>algorithm/mode/padding</i>"</code> * or <code>"<i>algorithm</i>"</code>. For example, <code>"AES/CBC/PKCS5Padding"</code>. * * @param provider * A provider which provides the implementation of the transformation. * * @param encoder * An encoder used in {@link #encrypt(String)} and * {@link #encrypt(byte[])} to encode an encrypted byte array. * If {@code null} is given, {@link Base64} is used as the default * encoder. * * @param decoder * A decoder used in {@link #decrypt(String)} and * {@link #decrypt(byte[])} to decode an encoded input byte array. * If {@code null} is given, {@link Base64} is used as the default * decoder. * * @throws IllegalArgumentException * The given transformation is not supported. This exception wraps * the original exception (such as {@link java.security.NoSuchAlgorithmException * NoSuchAlgorithmException} and {@link javax.crypto.NoSuchPaddingException * NoSuchPaddingException}) as the cause. * * @see <a href="http://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#Cipher" * >Cipher (Encryption) Algorithms</a> * * @see StandardCipherTransformations */ public CodecCipher(String transformation, Provider provider, BinaryEncoder encoder, BinaryDecoder decoder) throws IllegalArgumentException { this(getCipherInstance(transformation, provider), encoder, decoder); }
/** * Constructor with a transformation, a provider and a coder. * * <p> * This constructor is an alias of {@link #CodecCipher(String, Provider, BinaryEncoder, BinaryDecoder) * CodecCipher(transformation, provider, coder, coder)}. * </p> * * @param transformation * A transformation in the form of either <code>"<i>algorithm/mode/padding</i>"</code> * or <code>"<i>algorithm</i>"</code>. For example, <code>"AES/CBC/PKCS5Padding"</code>. * * @param provider * A provider which provides the implementation of the transformation. * * @param coder * A coder which works as both an encoder and a decoder. * If {@code null} is given, {@link Base64} is used as the * default coder. * * @throws IllegalArgumentException * The given transformation is not supported. This exception wraps * the original exception (such as {@link java.security.NoSuchAlgorithmException * NoSuchAlgorithmException} and {@link javax.crypto.NoSuchPaddingException * NoSuchPaddingException}) as the cause. * * @see <a href="http://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#Cipher" * >Cipher (Encryption) Algorithms</a> * * @see StandardCipherTransformations */ public <TCoder extends BinaryEncoder & BinaryDecoder> CodecCipher(String transformation, Provider provider, TCoder coder) throws IllegalArgumentException { this(transformation, provider, coder, coder); }
/** * Constructor with a transformation and a provider. * * <p> * This constructor is an alias of {@link #CodecCipher(String, Provider, BinaryEncoder, BinaryDecoder) * CodecCipher(transformation, provider, (BinaryEncoder)null, (BinaryDecoder)null)}. * </p> * * @param transformation * A transformation in the form of either <code>"<i>algorithm/mode/padding</i>"</code> * or <code>"<i>algorithm</i>"</code>. For example, <code>"AES/CBC/PKCS5Padding"</code>. * * @param provider * A provider which provides the implementation of the transformation. * * @throws IllegalArgumentException * The given transformation is not supported. This exception wraps * the original exception (such as {@link java.security.NoSuchAlgorithmException * NoSuchAlgorithmException} and {@link javax.crypto.NoSuchPaddingException * NoSuchPaddingException}) as the cause. * * @see <a href="http://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#Cipher" * >Cipher (Encryption) Algorithms</a> * * @see StandardCipherTransformations */ public CodecCipher(String transformation, Provider provider) throws IllegalArgumentException { this(transformation, provider, (BinaryEncoder)null, (BinaryDecoder)null); }
/** * Get the decoder which this instance internally holds. * * @return * The internal decoder. This may be {@code null}, and * in such a case, {@link #decrypt(String)} and * {@link #decrypt(byte[])} use {@link Base64}. */ public BinaryDecoder getDecoder() { return decoder; }