use of javax.crypto.spec.SecretKeySpec in project OpenAttestation by OpenAttestation.
the class Diagnostic method tryMacWithPassword.
private static void tryMacWithPassword(String algorithmName, String message, String password) {
try {
SecretKeySpec key = new SecretKeySpec(password.getBytes(), algorithmName);
// a string like "HmacSHA256"
Mac mac = Mac.getInstance(algorithmName, "BC");
mac.init(key);
byte[] digest = mac.doFinal(message.getBytes());
System.out.println("Created " + algorithmName + " digest of length " + digest.length);
} catch (NoSuchProviderException e) {
System.err.println("Cannot use provider: BC: " + e.toString());
} catch (NoSuchAlgorithmException e) {
System.err.println("Cannot use algorithm: " + algorithmName + ": " + e.toString());
} catch (InvalidKeyException e) {
System.err.println("Cannot use key: " + password + ": " + e.toString());
}
}
use of javax.crypto.spec.SecretKeySpec in project Signal-Android by WhisperSystems.
the class AsymmetricMasterCipher method getDigestedBytes.
private byte[] getDigestedBytes(byte[] secretBytes, int iteration) {
try {
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(new SecretKeySpec(secretBytes, "HmacSHA256"));
return mac.doFinal(Conversions.intToByteArray(iteration));
} catch (NoSuchAlgorithmException | java.security.InvalidKeyException e) {
throw new AssertionError(e);
}
}
use of javax.crypto.spec.SecretKeySpec in project Signal-Android by WhisperSystems.
the class MasterSecretUtil method generateMasterSecret.
public static MasterSecret generateMasterSecret(Context context, String passphrase) {
try {
byte[] encryptionSecret = generateEncryptionSecret();
byte[] macSecret = generateMacSecret();
byte[] masterSecret = Util.combine(encryptionSecret, macSecret);
byte[] encryptionSalt = generateSalt();
int iterations = generateIterationCount(passphrase, encryptionSalt);
byte[] encryptedMasterSecret = encryptWithPassphrase(encryptionSalt, iterations, masterSecret, passphrase);
byte[] macSalt = generateSalt();
byte[] encryptedAndMacdMasterSecret = macWithPassphrase(macSalt, iterations, encryptedMasterSecret, passphrase);
save(context, "encryption_salt", encryptionSalt);
save(context, "mac_salt", macSalt);
save(context, "passphrase_iterations", iterations);
save(context, "master_secret", encryptedAndMacdMasterSecret);
save(context, "passphrase_initialized", true);
return new MasterSecret(new SecretKeySpec(encryptionSecret, "AES"), new SecretKeySpec(macSecret, "HmacSHA1"));
} catch (GeneralSecurityException e) {
Log.w("keyutil", e);
return null;
}
}
use of javax.crypto.spec.SecretKeySpec 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.spec.SecretKeySpec 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;
}
Aggregations