use of javax.crypto.BadPaddingException in project mobile-center-sdk-android by Microsoft.
the class CryptoTest method failsToEncrypt.
@Test
public void failsToEncrypt() throws Exception {
CryptoUtils cryptoUtils = new CryptoUtils(mContext, mCryptoFactory, Build.VERSION_CODES.KITKAT);
when(mCipher.doFinal(any(byte[].class))).thenThrow(new BadPaddingException());
String data = "anythingThatWouldMakeTheCipherFailForSomeReason";
String encryptedData = cryptoUtils.encrypt(data);
assertEquals(data, encryptedData);
CryptoUtils.DecryptedData decryptedData = cryptoUtils.decrypt(encryptedData, false);
assertEquals(data, decryptedData.getDecryptedData());
assertNull(decryptedData.getNewEncryptedData());
decryptedData = cryptoUtils.decrypt(encryptedData, true);
assertEquals(data, decryptedData.getDecryptedData());
assertNull(decryptedData.getNewEncryptedData());
}
use of javax.crypto.BadPaddingException in project cassandra by apache.
the class EncryptionUtils method decrypt.
/**
* Decrypt the input data, as well as manage sizing of the {@code outputBuffer}; if the buffer is not big enough,
* deallocate current, and allocate a large enough buffer.
*
* @return the byte buffer that was actaully written to; it may be the {@code outputBuffer} if it had enough capacity,
* or it may be a new, larger instance. Callers should capture the return buffer (if calling multiple times).
*/
public static ByteBuffer decrypt(ReadableByteChannel channel, ByteBuffer outputBuffer, boolean allowBufferResize, Cipher cipher) throws IOException {
ByteBuffer metadataBuffer = reusableBuffers.get();
if (metadataBuffer.capacity() < ENCRYPTED_BLOCK_HEADER_SIZE) {
metadataBuffer = ByteBufferUtil.ensureCapacity(metadataBuffer, ENCRYPTED_BLOCK_HEADER_SIZE, true);
reusableBuffers.set(metadataBuffer);
}
metadataBuffer.position(0).limit(ENCRYPTED_BLOCK_HEADER_SIZE);
channel.read(metadataBuffer);
if (metadataBuffer.remaining() < ENCRYPTED_BLOCK_HEADER_SIZE)
throw new IllegalStateException("could not read encrypted blocked metadata header");
int encryptedLength = metadataBuffer.getInt();
// this is the length of the compressed data
int plainTextLength = metadataBuffer.getInt();
outputBuffer = ByteBufferUtil.ensureCapacity(outputBuffer, Math.max(plainTextLength, encryptedLength), allowBufferResize);
outputBuffer.position(0).limit(encryptedLength);
channel.read(outputBuffer);
ByteBuffer dupe = outputBuffer.duplicate();
dupe.clear();
try {
cipher.doFinal(outputBuffer, dupe);
} catch (ShortBufferException | IllegalBlockSizeException | BadPaddingException e) {
throw new IOException("failed to decrypt commit log block", e);
}
dupe.position(0).limit(plainTextLength);
return dupe;
}
use of javax.crypto.BadPaddingException in project otter by alibaba.
the class AESUtils method decrypt.
/**
* 解密byte数据
*
* @param encrypted
* @return
* @throws AESException
*/
public byte[] decrypt(byte[] encrypted) throws AESException {
try {
SecretKeySpec skeySpec = new SecretKeySpec(secretKey, ENCRYPTION_ALGORITHM);
Cipher cipher = Cipher.getInstance(ENCRYPTION_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
return cipher.doFinal(encrypted);
} catch (NoSuchAlgorithmException e) {
throw new AESException(e);
} catch (NoSuchPaddingException e) {
throw new AESException(e);
} catch (InvalidKeyException e) {
throw new AESException(e);
} catch (IllegalBlockSizeException e) {
throw new AESException(e);
} catch (BadPaddingException e) {
throw new AESException(e);
}
}
use of javax.crypto.BadPaddingException in project remusic by aa112901.
the class Aes method encrypt.
private static String encrypt(String content, String password) throws Exception {
try {
// String data = "Test String";
// String key = "1234567812345678";
String iv = "0102030405060708";
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
int blockSize = cipher.getBlockSize();
byte[] dataBytes = content.getBytes();
int plaintextLength = dataBytes.length;
if (plaintextLength % blockSize != 0) {
plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize));
}
byte[] plaintext = new byte[plaintextLength];
System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);
SecretKeySpec keyspec = new SecretKeySpec(password.getBytes(), "AES");
IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());
cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
byte[] encrypted = cipher.doFinal(plaintext);
return Base64Encoder.encode(encrypted);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return null;
}
use of javax.crypto.BadPaddingException in project remusic by aa112901.
the class Aes method encrypt.
private static byte[] encrypt(byte[] content, byte[] keyBytes) {
byte[] encryptedText = null;
if (!isInited) {
init();
}
/**
*类 SecretKeySpec
*可以使用此类来根据一个字节数组构造一个 SecretKey,
*而无须通过一个(基于 provider 的)SecretKeyFactory。
*此类仅对能表示为一个字节数组并且没有任何与之相关联的钥参数的原始密钥有用
*构造方法根据给定的字节数组构造一个密钥。
*此构造方法不检查给定的字节数组是否指定了一个算法的密钥。
*/
Key key = new SecretKeySpec(keyBytes, "AES");
try {
// 用密钥初始化此 cipher。
cipher.init(Cipher.ENCRYPT_MODE, key);
} catch (InvalidKeyException e) {
e.printStackTrace();
}
try {
//按单部分操作加密或解密数据,或者结束一个多部分操作。(不知道神马意思)
encryptedText = cipher.doFinal(content);
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return encryptedText;
}
Aggregations