use of javax.crypto.spec.IvParameterSpec in project jdk8u_jdk by JetBrains.
the class Dynamic method runTest.
protected boolean runTest(String algo, String mo, String pad) throws Exception {
boolean result = true;
try {
byte[] plainText = new byte[160000];
new Random().nextBytes(plainText);
String transformation = algo + "/" + mo + "/" + pad;
ci = Cipher.getInstance(transformation, SUNJCE);
KeyGenerator kg = KeyGenerator.getInstance(algo, SUNJCE);
if (keyStrength > Cipher.getMaxAllowedKeyLength(transformation)) {
// skip if this key length is larger than what's
// configured in the jce jurisdiction policy files
System.out.println(keyStrength + " is larger than what's configured " + "in the jce jurisdiction policy files");
return result;
}
kg.init(keyStrength);
key = kg.generateKey();
if (!mo.equalsIgnoreCase("GCM")) {
ci.init(Cipher.ENCRYPT_MODE, key, aps);
} else {
ci.init(Cipher.ENCRYPT_MODE, key);
}
byte[] cipherText = new byte[ci.getOutputSize(plainText.length)];
int offset = ci.update(plainText, 0, plainText.length, cipherText, 0);
ci.doFinal(cipherText, offset);
if (!mo.equalsIgnoreCase("ECB")) {
iv = ci.getIV();
aps = new IvParameterSpec(iv);
} else {
aps = null;
}
if (!mo.equalsIgnoreCase("GCM")) {
ci.init(Cipher.DECRYPT_MODE, key, aps);
} else {
ci.init(Cipher.DECRYPT_MODE, key, ci.getParameters());
}
byte[] recoveredText = new byte[ci.getOutputSize(cipherText.length)];
int len = ci.doFinal(cipherText, 0, cipherText.length, recoveredText);
byte[] tmp = new byte[len];
for (int i = 0; i < len; i++) {
tmp[i] = recoveredText[i];
}
result = Arrays.equals(plainText, tmp);
} catch (NoSuchAlgorithmException nsaEx) {
nsaEx.printStackTrace();
// CFB7 and OFB150 are negative test,SunJCE not support this
// algorithm
result = mo.equalsIgnoreCase("CFB7") || mo.equalsIgnoreCase("OFB150");
}
return result;
}
use of javax.crypto.spec.IvParameterSpec in project jdk8u_jdk by JetBrains.
the class DESCipherWrapper method execute.
public void execute(int edMode, byte[] inputText) throws InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, ShortBufferException, NoSuchAlgorithmException {
AlgorithmParameterSpec aps = null;
try {
if (!mode.equalsIgnoreCase("ECB")) {
aps = new IvParameterSpec(iv);
}
ci.init(edMode, key, aps);
// Generate a resultText using a single-part enc/dec
resultText = ci.doFinal(inputText);
// Generate outputText for each multi-part en/de-cryption
/* Combination #1:
update(byte[], int, int)
doFinal(byte[], int, int)
*/
byte[] part11 = ci.update(inputText, 0, inputText.length);
byte[] part12 = ci.doFinal();
byte[] outputText1 = new byte[part11.length + part12.length];
System.arraycopy(part11, 0, outputText1, 0, part11.length);
System.arraycopy(part12, 0, outputText1, part11.length, part12.length);
List<byte[]> outputTexts = new ArrayList<>(4);
outputTexts.add(outputText1);
/* Combination #2:
update(byte[], int, int)
doFinal(byte[], int, int, byte[], int)
*/
byte[] part21 = ci.update(inputText, 0, inputText.length - 5);
byte[] part22 = new byte[ci.getOutputSize(inputText.length)];
int len2 = ci.doFinal(inputText, inputText.length - 5, 5, part22, 0);
byte[] outputText2 = new byte[part21.length + len2];
System.arraycopy(part21, 0, outputText2, 0, part21.length);
System.arraycopy(part22, 0, outputText2, part21.length, len2);
outputTexts.add(outputText2);
/* Combination #3:
update(byte[], int, int, byte[], int)
doFinal(byte[], int, int)
*/
byte[] part31 = new byte[ci.getOutputSize(inputText.length)];
int len3 = ci.update(inputText, 0, inputText.length - 8, part31, 0);
byte[] part32 = ci.doFinal(inputText, inputText.length - 8, 8);
byte[] outputText3 = new byte[len3 + part32.length];
System.arraycopy(part31, 0, outputText3, 0, len3);
System.arraycopy(part32, 0, outputText3, len3, part32.length);
outputTexts.add(outputText3);
/* Combination #4:
update(byte[], int, int, byte[], int)
doFinal(byte[], int, int, byte[], int)
*/
byte[] part41 = new byte[ci.getOutputSize(inputText.length)];
int len4 = ci.update(inputText, 0, inputText.length - 8, part41, 0);
int rest4 = ci.doFinal(inputText, inputText.length - 8, 8, part41, len4);
byte[] outputText4 = new byte[len4 + rest4];
System.arraycopy(part41, 0, outputText4, 0, outputText4.length);
outputTexts.add(outputText4);
// Compare results
for (int k = 0; k < outputTexts.size(); k++) {
if (!Arrays.equals(resultText, outputTexts.get(k))) {
out.println(" Testing: " + algo + "/" + mode + "/" + pad);
throw new RuntimeException("Compare value of resultText and combination " + k + " are not same. Test failed.");
}
}
if (keyStrength > Cipher.getMaxAllowedKeyLength(algo)) {
throw new RuntimeException("Expected exception uncaught, keyStrength " + keyStrength);
}
} catch (InvalidKeyException ex) {
if (keyStrength <= Cipher.getMaxAllowedKeyLength(algo)) {
out.println("Unexpected exception in " + algo + "/" + mode + "/" + pad + " , KeySize " + keyStrength);
throw ex;
}
out.println("Caught InvalidKeyException as expected");
}
}
use of javax.crypto.spec.IvParameterSpec in project android_frameworks_base by DirtyUnicorns.
the class CryptoHelper method decryptBundle.
@Nullable
/* default */
Bundle decryptBundle(@NonNull Bundle bundle) throws GeneralSecurityException {
Preconditions.checkNotNull(bundle, "Cannot decrypt null bundle.");
byte[] iv = bundle.getByteArray(KEY_IV);
byte[] encryptedBytes = bundle.getByteArray(KEY_CIPHER);
byte[] mac = bundle.getByteArray(KEY_MAC);
if (!verifyMac(encryptedBytes, iv, mac)) {
Log.w(TAG, "Escrow mac mismatched!");
return null;
}
IvParameterSpec ivSpec = new IvParameterSpec(iv);
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, mEncryptionKey, ivSpec);
byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
Parcel decryptedParcel = Parcel.obtain();
decryptedParcel.unmarshall(decryptedBytes, 0, decryptedBytes.length);
decryptedParcel.setDataPosition(0);
Bundle decryptedBundle = new Bundle();
decryptedBundle.readFromParcel(decryptedParcel);
decryptedParcel.recycle();
return decryptedBundle;
}
use of javax.crypto.spec.IvParameterSpec in project UltimateAndroid by cymcsg.
the class TripleDES method decrypt.
/**
* Decrypt the message with TripleDES
*
* @param message
* @return
* @throws Exception
*/
public static String decrypt(byte[] message) throws Exception {
byte[] values = Base64decodingByte(message, 0);
final MessageDigest md = MessageDigest.getInstance("SHA-1");
final byte[] digestOfPassword = md.digest(token.getBytes("utf-8"));
final byte[] keyBytes = copyOf(digestOfPassword, 24);
for (int j = 0, k = 16; j < 8; ) {
keyBytes[k++] = keyBytes[j++];
}
final SecretKey key = new SecretKeySpec(keyBytes, "DESede");
String s1 = "12345678";
byte[] bytes = s1.getBytes();
final IvParameterSpec iv = new IvParameterSpec(bytes);
final Cipher decipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
decipher.init(Cipher.DECRYPT_MODE, key, iv);
final byte[] plainText = decipher.doFinal(values);
return new String(plainText, "UTF-8");
}
use of javax.crypto.spec.IvParameterSpec in project UltimateAndroid by cymcsg.
the class TripleDES method decrypt.
/**
* Decrypt the message with TripleDES
*
* @param message
* @return
* @throws Exception
*/
public static String decrypt(String message) throws Exception {
if (message == null || message == "")
return "";
byte[] values = Base64decoding(message, 0);
final MessageDigest md = MessageDigest.getInstance("SHA-1");
final byte[] digestOfPassword = md.digest(token.getBytes("utf-8"));
final byte[] keyBytes = copyOf(digestOfPassword, 24);
for (int j = 0, k = 16; j < 8; ) {
keyBytes[k++] = keyBytes[j++];
}
final SecretKey key = new SecretKeySpec(keyBytes, "DESede");
String s1 = "12345678";
byte[] bytes = s1.getBytes();
final IvParameterSpec iv = new IvParameterSpec(bytes);
final Cipher decipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
decipher.init(Cipher.DECRYPT_MODE, key, iv);
final byte[] plainText = decipher.doFinal(values);
return new String(plainText, "UTF-8");
}
Aggregations