public Map<String, String> unwrap(String in, int maxAgeSec) throws BlobCrypterException { try { byte[] bin = Base64.decodeBase64(in.getBytes("UTF-8")); byte[] hmac = new byte[Crypto.HMAC_SHA1_LEN]; byte[] cipherText = new byte[bin.length-Crypto.HMAC_SHA1_LEN]; System.arraycopy(bin, 0, cipherText, 0, cipherText.length); System.arraycopy(bin, cipherText.length, hmac, 0, hmac.length); Crypto.hmacSha1Verify(hmacKey, cipherText, hmac); byte[] plain = Crypto.aes128cbcDecrypt(cipherKey, cipherText); Map<String, String> out = deserialize(plain); checkTimestamp(out, maxAgeSec); return out; } catch (GeneralSecurityException e) { throw new BlobCrypterException("Invalid token signature", e); } catch (ArrayIndexOutOfBoundsException e) { throw new BlobCrypterException("Invalid token format", e); } catch (NegativeArraySizeException e) { throw new BlobCrypterException("Invalid token format", e); } catch (UnsupportedEncodingException e) { throw new BlobCrypterException(e); } }
public Map<String, String> unwrap(String in, int maxAgeSec) throws BlobCrypterException { try { byte[] bin = Base64.decodeBase64(in.getBytes("UTF-8")); byte[] hmac = new byte[Crypto.HMAC_SHA1_LEN]; byte[] cipherText = new byte[bin.length-Crypto.HMAC_SHA1_LEN]; System.arraycopy(bin, 0, cipherText, 0, cipherText.length); System.arraycopy(bin, cipherText.length, hmac, 0, hmac.length); Crypto.hmacSha1Verify(hmacKey, cipherText, hmac); byte[] plain = Crypto.aes128cbcDecrypt(cipherKey, cipherText); Map<String, String> out = deserialize(plain); checkTimestamp(out, maxAgeSec); return out; } catch (GeneralSecurityException e) { throw new BlobCrypterException("Invalid token signature", e); } catch (ArrayIndexOutOfBoundsException e) { throw new BlobCrypterException("Invalid token format", e); } catch (NegativeArraySizeException e) { throw new BlobCrypterException("Invalid token format", e); } catch (UnsupportedEncodingException e) { throw new BlobCrypterException(e); } }
public Map<String, String> unwrap(String in) throws BlobCrypterException { try { byte[] bin = Base64.decodeBase64(CharsetUtil.getUtf8Bytes(in)); byte[] hmac = new byte[hmacType.getLength()]; byte[] cipherText = new byte[bin.length-hmacType.getLength()]; System.arraycopy(bin, 0, cipherText, 0, cipherText.length); System.arraycopy(bin, cipherText.length, hmac, 0, hmac.length); Crypto.hmacShaVerify(hmacKey, cipherText, hmac, hmacType.getName()); byte[] plain = Crypto.aes128cbcDecrypt(cipherKey, cipherText); Map<String, String> out = deserialize(plain); return out; } catch (GeneralSecurityException e) { throw new BlobCrypterException("Invalid token signature", e); } catch (ArrayIndexOutOfBoundsException e) { throw new BlobCrypterException("Invalid token format", e); } catch (NegativeArraySizeException e) { throw new BlobCrypterException("Invalid token format", e); } }
public Map<String, String> unwrap(String in) throws BlobCrypterException { try { byte[] bin = Base64.decodeBase64(CharsetUtil.getUtf8Bytes(in)); byte[] hmac = new byte[hmacType.getLength()]; byte[] cipherText = new byte[bin.length-hmacType.getLength()]; System.arraycopy(bin, 0, cipherText, 0, cipherText.length); System.arraycopy(bin, cipherText.length, hmac, 0, hmac.length); Crypto.hmacShaVerify(hmacKey, cipherText, hmac, hmacType.getName()); byte[] plain = Crypto.aes128cbcDecrypt(cipherKey, cipherText); Map<String, String> out = deserialize(plain); return out; } catch (GeneralSecurityException e) { throw new BlobCrypterException("Invalid token signature", e); } catch (ArrayIndexOutOfBoundsException e) { throw new BlobCrypterException("Invalid token format", e); } catch (NegativeArraySizeException e) { throw new BlobCrypterException("Invalid token format", e); } }
public Map<String, String> unwrap(String in) throws BlobCrypterException { try { byte[] bin = Base64.decodeBase64(CharsetUtil.getUtf8Bytes(in)); byte[] hmac = new byte[hmacType.getLength()]; byte[] cipherText = new byte[bin.length-hmacType.getLength()]; System.arraycopy(bin, 0, cipherText, 0, cipherText.length); System.arraycopy(bin, cipherText.length, hmac, 0, hmac.length); Crypto.hmacShaVerify(hmacKey, cipherText, hmac, hmacType.getName()); byte[] plain = Crypto.aes128cbcDecrypt(cipherKey, cipherText); Map<String, String> out = deserialize(plain); return out; } catch (GeneralSecurityException e) { throw new BlobCrypterException("Invalid token signature", e); } catch (ArrayIndexOutOfBoundsException e) { throw new BlobCrypterException("Invalid token format", e); } catch (NegativeArraySizeException e) { throw new BlobCrypterException("Invalid token format", e); } }
@Test public void testAes128Cbc() throws Exception { byte[] key = Crypto.getRandomBytes(Crypto.CIPHER_KEY_LEN); for (byte i=0; i < 50; i++) { byte[] orig = new byte[i]; for (byte j=0; j < i; j++) { orig[j] = j; } byte[] cipherText = Crypto.aes128cbcEncrypt(key, orig); byte[] plainText = Crypto.aes128cbcDecrypt(key, cipherText); assertArrayEquals("Array of length " + i, orig, plainText); } }
@Test public void testAes128Cbc() throws Exception { byte[] key = Crypto.getRandomBytes(Crypto.CIPHER_KEY_LEN); for (byte i=0; i < 50; i++) { byte[] orig = new byte[i]; for (byte j=0; j < i; j++) { orig[j] = j; } byte[] cipherText = Crypto.aes128cbcEncrypt(key, orig); byte[] plainText = Crypto.aes128cbcDecrypt(key, cipherText); assertArrayEquals("Array of length " + i, orig, plainText); } }
@Test public void testAes128Cbc() throws Exception { byte[] key = Crypto.getRandomBytes(Crypto.CIPHER_KEY_LEN); for (byte i=0; i < 50; i++) { byte[] orig = new byte[i]; for (byte j=0; j < i; j++) { orig[j] = j; } byte[] cipherText = Crypto.aes128cbcEncrypt(key, orig); byte[] plainText = Crypto.aes128cbcDecrypt(key, cipherText); assertArrayEquals("Array of length " + i, orig, plainText); } }
@Test public void testAes128Cbc() throws Exception { byte[] key = Crypto.getRandomBytes(Crypto.CIPHER_KEY_LEN); for (byte i=0; i < 50; i++) { byte[] orig = new byte[i]; for (byte j=0; j < i; j++) { orig[j] = j; } byte[] cipherText = Crypto.aes128cbcEncrypt(key, orig); byte[] plainText = Crypto.aes128cbcDecrypt(key, cipherText); assertArrayEquals("Array of length " + i, orig, plainText); } }
@Test public void testAes128Cbc() throws Exception { byte[] key = Crypto.getRandomBytes(Crypto.CIPHER_KEY_LEN); for (byte i=0; i < 50; i++) { byte[] orig = new byte[i]; for (byte j=0; j < i; j++) { orig[j] = j; } byte[] cipherText = Crypto.aes128cbcEncrypt(key, orig); byte[] plainText = Crypto.aes128cbcDecrypt(key, cipherText); assertArrayEquals("Array of length " + i, orig, plainText); } }