use of org.bouncycastle.crypto.generators.PKCS5S2ParametersGenerator in project samourai-wallet-android by Samourai-Wallet.
the class AESUtil method decryptWithSetMode.
public static String decryptWithSetMode(String ciphertext, CharSequenceX password, int iterations, int mode, @Nullable BlockCipherPadding padding) throws InvalidCipherTextException, UnsupportedEncodingException, DecryptionException {
final int AESBlockSize = 4;
byte[] cipherdata = Base64.decodeBase64(ciphertext.getBytes());
// Separate 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);
BlockCipher cipherMode;
if (mode == MODE_CBC) {
cipherMode = new CBCBlockCipher(new AESEngine());
} else {
// mode == MODE_OFB
cipherMode = new OFBBlockCipher(new AESEngine(), 128);
}
BufferedBlockCipher cipher;
if (padding != null) {
cipher = new PaddedBufferedBlockCipher(cipherMode, padding);
} else {
cipher = new BufferedBlockCipher(cipherMode);
}
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);
len += cipher.doFinal(buf, len);
// remove padding
byte[] out = new byte[len];
System.arraycopy(buf, 0, out, 0, len);
// return string representation of decoded bytes
String result = new String(out, "UTF-8");
if (result.isEmpty()) {
throw new DecryptionException("Decrypted string is empty.");
}
return result;
}
use of org.bouncycastle.crypto.generators.PKCS5S2ParametersGenerator in project openmeetings by apache.
the class SHA256Implementation method hash.
private static String hash(String str, byte[] salt, int iter) {
PKCS5S2ParametersGenerator gen = new PKCS5S2ParametersGenerator(new SHA256Digest());
gen.init(str.getBytes(StandardCharsets.UTF_8), salt, iter);
byte[] dk = ((KeyParameter) gen.generateDerivedParameters(KEY_LENGTH)).getKey();
return Base64.encodeBase64String(dk);
}
use of org.bouncycastle.crypto.generators.PKCS5S2ParametersGenerator in project jruby-openssl by jruby.
the class PKCS5 method generatePBEKey.
private static RubyString generatePBEKey(final Ruby runtime, final char[] pass, final byte[] salt, final int iter, final int keySize) {
PBEParametersGenerator generator = new PKCS5S2ParametersGenerator();
generator.init(PBEParametersGenerator.PKCS5PasswordToBytes(pass), salt, iter);
CipherParameters params = generator.generateDerivedParameters(keySize * 8);
return StringHelper.newString(runtime, ((KeyParameter) params).getKey());
}
use of org.bouncycastle.crypto.generators.PKCS5S2ParametersGenerator in project jruby-openssl by jruby.
the class PEMInputOutput method extractPBES2CipherParams.
private static CipherParameters extractPBES2CipherParams(char[] password, PBES2Parameters pbeParams) {
PBKDF2Params pbkdfParams = PBKDF2Params.getInstance(pbeParams.getKeyDerivationFunc().getParameters());
int keySize = 192;
if (pbkdfParams.getKeyLength() != null) {
keySize = pbkdfParams.getKeyLength().intValue() * 8;
}
int iterationCount = pbkdfParams.getIterationCount().intValue();
byte[] salt = pbkdfParams.getSalt();
PBEParametersGenerator generator = new PKCS5S2ParametersGenerator();
generator.init(PBEParametersGenerator.PKCS5PasswordToBytes(password), salt, iterationCount);
return generator.generateDerivedParameters(keySize);
}
use of org.bouncycastle.crypto.generators.PKCS5S2ParametersGenerator 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;
}
Aggregations