use of javax.crypto.spec.SecretKeySpec in project android_frameworks_base by ParanoidAndroid.
the class ContainerEncryptionParamsTest method testEquals_EncKey_Failure.
public void testEquals_EncKey_Failure() throws Exception {
ContainerEncryptionParams params1 = new ContainerEncryptionParams(ENC_ALGORITHM, ENC_PARAMS, ENC_KEY, MAC_ALGORITHM, null, MAC_KEY, MAC_TAG, AUTHENTICATED_START, ENCRYPTED_START, DATA_END);
ContainerEncryptionParams params2 = new ContainerEncryptionParams(new String(ENC_ALGORITHM), new IvParameterSpec(IV_BYTES.clone()), new SecretKeySpec("BLAHBLAH".getBytes(), "RAW"), new String(MAC_ALGORITHM), null, new SecretKeySpec(MAC_KEY_BYTES.clone(), "RAW"), MAC_TAG, AUTHENTICATED_START, ENCRYPTED_START, DATA_END);
assertFalse(params1.equals(params2));
}
use of javax.crypto.spec.SecretKeySpec in project android_frameworks_base by ParanoidAndroid.
the class AndroidKeyStoreTest method testKeyStore_KeyOperations_Wrap_Encrypted_Success.
public void testKeyStore_KeyOperations_Wrap_Encrypted_Success() throws Exception {
setupPassword();
mKeyStore.load(null, null);
setupKey();
// Test key usage
Entry e = mKeyStore.getEntry(TEST_ALIAS_1, null);
assertNotNull(e);
assertTrue(e instanceof PrivateKeyEntry);
PrivateKeyEntry privEntry = (PrivateKeyEntry) e;
PrivateKey privKey = privEntry.getPrivateKey();
assertNotNull(privKey);
PublicKey pubKey = privEntry.getCertificate().getPublicKey();
Cipher c = Cipher.getInstance("RSA/ECB/PKCS1Padding");
c.init(Cipher.WRAP_MODE, pubKey);
byte[] expectedKey = new byte[] { 0x00, 0x05, (byte) 0xAA, (byte) 0x0A5, (byte) 0xFF, 0x55, 0x0A };
SecretKey expectedSecret = new SecretKeySpec(expectedKey, "AES");
byte[] wrappedExpected = c.wrap(expectedSecret);
c.init(Cipher.UNWRAP_MODE, privKey);
SecretKey actualSecret = (SecretKey) c.unwrap(wrappedExpected, "AES", Cipher.SECRET_KEY);
assertEquals(Arrays.toString(expectedSecret.getEncoded()), Arrays.toString(actualSecret.getEncoded()));
}
use of javax.crypto.spec.SecretKeySpec in project sonarqube by SonarSource.
the class AesCipher method loadSecretFileFromFile.
@VisibleForTesting
Key loadSecretFileFromFile(@Nullable String path) throws IOException {
if (StringUtils.isBlank(path)) {
throw new IllegalStateException("Secret key not found. Please set the property " + CoreProperties.ENCRYPTION_SECRET_KEY_PATH);
}
File file = new File(path);
if (!file.exists() || !file.isFile()) {
throw new IllegalStateException("The property " + CoreProperties.ENCRYPTION_SECRET_KEY_PATH + " does not link to a valid file: " + path);
}
String s = FileUtils.readFileToString(file);
if (StringUtils.isBlank(s)) {
throw new IllegalStateException("No secret key in the file: " + path);
}
return new SecretKeySpec(Base64.decodeBase64(StringUtils.trim(s)), CRYPTO_KEY);
}
use of javax.crypto.spec.SecretKeySpec in project RxCache by VictorAlbertos.
the class BuiltInEncryptor method generateSecretKey.
private SecretKeySpec generateSecretKey(String key) throws Exception {
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(key.getBytes("UTF-8"));
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
keyGenerator.init(KEY_LENGTH, secureRandom);
SecretKey secretKey = keyGenerator.generateKey();
return new SecretKeySpec(secretKey.getEncoded(), "AES");
}
use of javax.crypto.spec.SecretKeySpec in project RxCache by VictorAlbertos.
the class BuiltInEncryptor method initCiphers.
private void initCiphers(String key) {
try {
SecretKeySpec secretKey = generateSecretKey(key);
encryptCipher = Cipher.getInstance("AES");
encryptCipher.init(Cipher.ENCRYPT_MODE, secretKey);
decryptCipher = Cipher.getInstance("AES");
decryptCipher.init(Cipher.DECRYPT_MODE, secretKey);
} catch (Exception e) {
e.printStackTrace();
}
}
Aggregations