use of org.bouncycastle.crypto.PBEParametersGenerator in project vsDiaryWriter by shilongdai.
the class BCPCKDF2Generator method generate.
@Override
public byte[] generate(String password, int length) {
PBEParametersGenerator gen = new PKCS12ParametersGenerator(digester);
gen.init(PBEParametersGenerator.PKCS12PasswordToBytes(password.toCharArray()), salt, iteration);
KeyParameter param = (KeyParameter) gen.generateDerivedParameters(length);
return param.getKey();
}
use of org.bouncycastle.crypto.PBEParametersGenerator in project sentinel-android by Samourai-Wallet.
the class AESUtil method decrypt.
// AES 256 PBKDF2 CBC iso10126 decryption
// 16 byte IV must be prepended to ciphertext - Compatible with crypto-js
public static String decrypt(String ciphertext, CharSequenceX password, int iterations) {
final int AESBlockSize = 4;
byte[] cipherdata = Base64.decodeBase64(ciphertext.getBytes());
// Seperate the IV and cipher data
byte[] iv = copyOfRange(cipherdata, 0, AESBlockSize * 4);
byte[] input = copyOfRange(cipherdata, AESBlockSize * 4, cipherdata.length);
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(false, params);
// create a temporary buffer to decode into (includes padding)
byte[] buf = new byte[cipher.getOutputSize(input.length)];
int len = cipher.processBytes(input, 0, input.length, buf, 0);
try {
len += cipher.doFinal(buf, len);
} catch (InvalidCipherTextException icte) {
icte.printStackTrace();
return null;
}
// remove padding
byte[] out = new byte[len];
System.arraycopy(buf, 0, out, 0, len);
// return string representation of decoded bytes
String ret = null;
try {
ret = new String(out, "UTF-8");
} catch (UnsupportedEncodingException uee) {
uee.printStackTrace();
return null;
}
return ret;
}
use of org.bouncycastle.crypto.PBEParametersGenerator 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.PBEParametersGenerator in project XobotOS by xamarin.
the class JDKKeyStore method engineStore.
public void engineStore(OutputStream stream, char[] password) throws IOException {
DataOutputStream dOut = new DataOutputStream(stream);
byte[] salt = new byte[STORE_SALT_SIZE];
int iterationCount = MIN_ITERATIONS + (random.nextInt() & 0x3ff);
random.nextBytes(salt);
dOut.writeInt(STORE_VERSION);
dOut.writeInt(salt.length);
dOut.write(salt);
dOut.writeInt(iterationCount);
// BEGIN android-changed
HMac hMac = new HMac(new OpenSSLDigest.SHA1());
MacOutputStream mOut = new MacOutputStream(dOut, hMac);
PBEParametersGenerator pbeGen = new PKCS12ParametersGenerator(new OpenSSLDigest.SHA1());
// END android-changed
byte[] passKey = PBEParametersGenerator.PKCS12PasswordToBytes(password);
pbeGen.init(passKey, salt, iterationCount);
hMac.init(pbeGen.generateDerivedMacParameters(hMac.getMacSize()));
for (int i = 0; i != passKey.length; i++) {
passKey[i] = 0;
}
saveStore(mOut);
byte[] mac = new byte[hMac.getMacSize()];
hMac.doFinal(mac, 0);
dOut.write(mac);
dOut.close();
}
use of org.bouncycastle.crypto.PBEParametersGenerator 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);
}
Aggregations