use of javax.crypto.IllegalBlockSizeException in project Signal-Android by WhisperSystems.
the class DecryptingPartInputStream method readFinal.
private int readFinal(byte[] buffer, int offset, int length) throws IOException {
try {
int flourish = cipher.doFinal(buffer, offset);
//mac.update(buffer, offset, flourish);
byte[] ourMac = mac.doFinal();
byte[] theirMac = new byte[mac.getMacLength()];
readFully(theirMac);
if (!Arrays.equals(ourMac, theirMac))
throw new IOException("MAC doesn't match! Potential tampering?");
done = true;
return flourish;
} catch (IllegalBlockSizeException e) {
Log.w(TAG, e);
throw new IOException("Illegal block size exception!");
} catch (ShortBufferException e) {
Log.w(TAG, e);
throw new IOException("Short buffer exception!");
} catch (BadPaddingException e) {
Log.w(TAG, e);
throw new IOException("Bad padding exception!");
}
}
use of javax.crypto.IllegalBlockSizeException in project remusic by aa112901.
the class AESTools method encrpty.
public static String encrpty(String paramString) {
MessageDigest messageDigest = null;
try {
messageDigest = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
messageDigest.update(INPUT.getBytes());
byte[] stringBytes = messageDigest.digest();
StringBuilder stringBuilder = new StringBuilder(stringBytes.length * 2);
for (int i = 0; i < stringBytes.length; i++) {
stringBuilder.append(CHARS[((stringBytes[i] & 0xF0) >>> 4)]);
stringBuilder.append(CHARS[(stringBytes[i] & 0xF)]);
}
String str = stringBuilder.toString();
SecretKeySpec localSecretKeySpec = new SecretKeySpec(str.substring(str.length() / 2).getBytes(), "AES");
Cipher localCipher;
try {
localCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
localCipher.init(1, localSecretKeySpec, new IvParameterSpec(IV.getBytes()));
return URLEncoder.encode(new String(BytesHandler.getChars(localCipher.doFinal(paramString.getBytes()))), "utf-8");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (InvalidAlgorithmParameterException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return "";
}
use of javax.crypto.IllegalBlockSizeException in project remusic by aa112901.
the class Aes method decrypt.
private static byte[] decrypt(byte[] content, String password) {
try {
byte[] keyStr = getKey(password);
SecretKeySpec key = new SecretKeySpec(keyStr, "AES");
//algorithmStr
Cipher cipher = Cipher.getInstance(algorithmStr);
// ΚΌ
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] result = cipher.doFinal(content);
//
return result;
} 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.IllegalBlockSizeException in project robovm by robovm.
the class AlgorithmParameterSymmetricHelper method test.
@Override
public void test(AlgorithmParameters parameters) {
KeyGenerator generator = null;
try {
generator = KeyGenerator.getInstance(algorithmName);
} catch (NoSuchAlgorithmException e) {
Assert.fail(e.getMessage());
}
generator.init(keySize);
Key key = generator.generateKey();
Cipher cipher = null;
try {
String transformation = algorithmName;
if (blockmode != null) {
transformation += "/" + blockmode;
}
cipher = Cipher.getInstance(transformation);
} catch (NoSuchAlgorithmException e) {
Assert.fail(e.getMessage());
} catch (NoSuchPaddingException e) {
Assert.fail(e.getMessage());
}
try {
cipher.init(Cipher.ENCRYPT_MODE, key, parameters);
} catch (InvalidKeyException e) {
Assert.fail(e.getMessage());
} catch (InvalidAlgorithmParameterException e) {
Assert.fail(e.getMessage());
}
byte[] bs = null;
try {
bs = cipher.doFinal(plainData.getBytes());
} catch (IllegalBlockSizeException e) {
Assert.fail(e.getMessage());
} catch (BadPaddingException e) {
Assert.fail(e.getMessage());
}
try {
cipher.init(Cipher.DECRYPT_MODE, key, parameters);
} catch (InvalidKeyException e) {
Assert.fail(e.getMessage());
} catch (InvalidAlgorithmParameterException e) {
Assert.fail(e.getMessage());
}
byte[] decrypted = null;
try {
decrypted = cipher.doFinal(bs);
} catch (IllegalBlockSizeException e) {
Assert.fail(e.getMessage());
} catch (BadPaddingException e) {
Assert.fail(e.getMessage());
}
Assert.assertTrue(Arrays.equals(plainData.getBytes(), decrypted));
}
use of javax.crypto.IllegalBlockSizeException in project robovm by robovm.
the class OpenSSLCipherRSA method engineDoFinal.
@Override
protected byte[] engineDoFinal(byte[] input, int inputOffset, int inputLen) throws IllegalBlockSizeException, BadPaddingException {
if (input != null) {
engineUpdate(input, inputOffset, inputLen);
}
if (inputTooLarge) {
throw new IllegalBlockSizeException("input must be under " + buffer.length + " bytes");
}
final byte[] tmpBuf;
if (bufferOffset != buffer.length) {
if (padding == NativeCrypto.RSA_NO_PADDING) {
tmpBuf = new byte[buffer.length];
System.arraycopy(buffer, 0, tmpBuf, buffer.length - bufferOffset, bufferOffset);
} else {
tmpBuf = Arrays.copyOf(buffer, bufferOffset);
}
} else {
tmpBuf = buffer;
}
byte[] output = new byte[buffer.length];
int resultSize;
if (encrypting) {
if (usingPrivateKey) {
resultSize = NativeCrypto.RSA_private_encrypt(tmpBuf.length, tmpBuf, output, key.getPkeyContext(), padding);
} else {
resultSize = NativeCrypto.RSA_public_encrypt(tmpBuf.length, tmpBuf, output, key.getPkeyContext(), padding);
}
} else {
try {
if (usingPrivateKey) {
resultSize = NativeCrypto.RSA_private_decrypt(tmpBuf.length, tmpBuf, output, key.getPkeyContext(), padding);
} else {
resultSize = NativeCrypto.RSA_public_decrypt(tmpBuf.length, tmpBuf, output, key.getPkeyContext(), padding);
}
} catch (SignatureException e) {
IllegalBlockSizeException newE = new IllegalBlockSizeException();
newE.initCause(e);
throw newE;
}
}
if (!encrypting && resultSize != output.length) {
output = Arrays.copyOf(output, resultSize);
}
bufferOffset = 0;
return output;
}
Aggregations