use of org.bouncycastle.crypto.generators.PKCS5S2ParametersGenerator in project sentinel-android by Samourai-Wallet.
the class AESUtil method encrypt.
public static String encrypt(String cleartext, CharSequenceX password, int iterations) {
final int AESBlockSize = 4;
if (password == null) {
return null;
}
// Use secure random to generate a 16 byte iv
SecureRandom random = new SecureRandom();
byte[] iv = new byte[AESBlockSize * 4];
random.nextBytes(iv);
byte[] clearbytes = null;
try {
clearbytes = cleartext.getBytes("UTF-8");
} catch (UnsupportedEncodingException uee) {
uee.printStackTrace();
return null;
}
PBEParametersGenerator generator = new PKCS5S2ParametersGenerator();
generator.init(PBEParametersGenerator.PKCS5PasswordToUTF8Bytes(password.toString().toCharArray()), iv, iterations);
KeyParameter keyParam = (KeyParameter) generator.generateDerivedParameters(256);
CipherParameters params = new ParametersWithIV(keyParam, iv);
// setup AES cipher in CBC mode with PKCS7 padding
BlockCipherPadding padding = new ISO10126d2Padding();
BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), padding);
cipher.reset();
cipher.init(true, params);
byte[] outBuf = cipherData(cipher, clearbytes);
// Append to IV to the output
int len1 = iv.length;
int len2 = outBuf.length;
byte[] ivAppended = new byte[len1 + len2];
System.arraycopy(iv, 0, ivAppended, 0, len1);
System.arraycopy(outBuf, 0, ivAppended, len1, len2);
byte[] raw = Base64.encodeBase64(ivAppended);
String ret = new String(raw);
return ret;
}
use of org.bouncycastle.crypto.generators.PKCS5S2ParametersGenerator in project XobotOS by xamarin.
the class PEMUtilities method generateSecretKeyForPKCS5Scheme2.
static SecretKey generateSecretKeyForPKCS5Scheme2(String algorithm, char[] password, byte[] salt, int iterationCount) {
PBEParametersGenerator generator = new PKCS5S2ParametersGenerator();
generator.init(PBEParametersGenerator.PKCS5PasswordToBytes(password), salt, iterationCount);
return new SecretKeySpec(((KeyParameter) generator.generateDerivedParameters(PEMUtilities.getKeySize(algorithm))).getKey(), algorithm);
}
use of org.bouncycastle.crypto.generators.PKCS5S2ParametersGenerator in project nifi by apache.
the class PBKDF2CipherProvider method getInitializedCipher.
protected Cipher getInitializedCipher(EncryptionMethod encryptionMethod, String password, byte[] salt, byte[] iv, int keyLength, boolean encryptMode) throws Exception {
if (encryptionMethod == null) {
throw new IllegalArgumentException("The encryption method must be specified");
}
if (!encryptionMethod.isCompatibleWithStrongKDFs()) {
throw new IllegalArgumentException(encryptionMethod.name() + " is not compatible with PBKDF2");
}
String algorithm = encryptionMethod.getAlgorithm();
final String cipherName = CipherUtility.parseCipherFromAlgorithm(algorithm);
if (!CipherUtility.isValidKeyLength(keyLength, cipherName)) {
throw new IllegalArgumentException(String.valueOf(keyLength) + " is not a valid key length for " + cipherName);
}
if (StringUtils.isEmpty(password)) {
throw new IllegalArgumentException("Encryption with an empty password is not supported");
}
if (salt == null || salt.length < DEFAULT_SALT_LENGTH) {
throw new IllegalArgumentException("The salt must be at least " + DEFAULT_SALT_LENGTH + " bytes. To generate a salt, use PBKDF2CipherProvider#generateSalt()");
}
PKCS5S2ParametersGenerator gen = new PKCS5S2ParametersGenerator(this.prf);
gen.init(password.getBytes(StandardCharsets.UTF_8), salt, getIterationCount());
byte[] dk = ((KeyParameter) gen.generateDerivedParameters(keyLength)).getKey();
SecretKey tempKey = new SecretKeySpec(dk, algorithm);
KeyedCipherProvider keyedCipherProvider = new AESKeyedCipherProvider();
return keyedCipherProvider.getCipher(encryptionMethod, tempKey, iv, encryptMode);
}
use of org.bouncycastle.crypto.generators.PKCS5S2ParametersGenerator in project cxf by apache.
the class PbesHmacAesWrapKeyEncryptionAlgorithm method createDerivedKey.
static byte[] createDerivedKey(String keyAlgoJwt, int keySize, byte[] password, byte[] saltInput, int pbesCount) {
byte[] saltValue = createSaltValue(keyAlgoJwt, saltInput);
Digest digest = null;
int macSigSize = PBES_HMAC_MAP.get(keyAlgoJwt);
if (macSigSize == 256) {
digest = new SHA256Digest();
} else if (macSigSize == 384) {
digest = new SHA384Digest();
} else {
digest = new SHA512Digest();
}
PKCS5S2ParametersGenerator gen = new PKCS5S2ParametersGenerator(digest);
gen.init(password, saltValue, pbesCount);
return ((KeyParameter) gen.generateDerivedParameters(keySize * 8)).getKey();
}
use of org.bouncycastle.crypto.generators.PKCS5S2ParametersGenerator in project OsmAnd-tools by osmandapp.
the class SigningUtils method pinToKey.
/**
* Step (0) to (3): Converts secret PIN to AES key
* @param pin Secret PIN
* @return AES key for next steps
*/
static byte[] pinToKey(String pin) {
int iterations = 1024;
byte[] pinBytes = PBEParametersGenerator.PKCS5PasswordToUTF8Bytes(pin.toCharArray());
byte[] salt = PBEParametersGenerator.PKCS5PasswordToUTF8Bytes("".toCharArray());
PKCS5S2ParametersGenerator generator = new PKCS5S2ParametersGenerator(new SHA256Digest());
generator.init(pinBytes, salt, iterations);
KeyParameter params = (KeyParameter) generator.generateDerivedParameters(128);
byte[] intResult = PBEParametersGenerator.PKCS5PasswordToUTF8Bytes(toHex(params.getKey()).toCharArray());
generator = new PKCS5S2ParametersGenerator(new SHA256Digest());
generator.init(intResult, salt, iterations);
params = (KeyParameter) generator.generateDerivedParameters(256);
return params.getKey();
}
Aggregations