本文整理汇总了Java中org.bouncycastle.crypto.generators.PKCS5S2ParametersGenerator类的典型用法代码示例。如果您正苦于以下问题:Java PKCS5S2ParametersGenerator类的具体用法?Java PKCS5S2ParametersGenerator怎么用?Java PKCS5S2ParametersGenerator使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
PKCS5S2ParametersGenerator类属于org.bouncycastle.crypto.generators包,在下文中一共展示了PKCS5S2ParametersGenerator类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getRecipientOperator
import org.bouncycastle.crypto.generators.PKCS5S2ParametersGenerator; //导入依赖的package包/类
protected RecipientOperator getRecipientOperator(Recipient recipient)
throws CMSException, IOException
{
PasswordRecipient pbeRecipient = (PasswordRecipient)recipient;
AlgorithmIdentifier kekAlg = AlgorithmIdentifier.getInstance(info.getKeyEncryptionAlgorithm());
AlgorithmIdentifier kekAlgParams = AlgorithmIdentifier.getInstance(kekAlg.getParameters());
byte[] passwordBytes = getPasswordBytes(pbeRecipient.getPasswordConversionScheme(),
pbeRecipient.getPassword());
PBKDF2Params params = PBKDF2Params.getInstance(info.getKeyDerivationAlgorithm().getParameters());
PKCS5S2ParametersGenerator gen = new PKCS5S2ParametersGenerator();
gen.init(passwordBytes, params.getSalt(), params.getIterationCount().intValue());
int keySize = ((Integer)KEYSIZES.get(kekAlgParams.getAlgorithm())).intValue();
byte[] derivedKey = ((KeyParameter)gen.generateDerivedParameters(keySize)).getKey();
return pbeRecipient.getRecipientOperator(kekAlgParams, messageAlgorithm, derivedKey, info.getEncryptedKey().getOctets());
}
示例2: compareModes
import org.bouncycastle.crypto.generators.PKCS5S2ParametersGenerator; //导入依赖的package包/类
@Test
public void compareModes() {
BlockCipher engine = new AESEngine();
int blockSize = engine.getBlockSize();
BlockCipher ref = new SICBlockCipher(engine); // reference implementation
BlockCipher uut = new CtrBlockCipher(engine); // unit under test
PBEParametersGenerator gen = new PKCS5S2ParametersGenerator();
byte[] salt = new byte[blockSize]; // used as salt and cipher input
new SecureRandom().nextBytes(salt);
gen.init("top secret".getBytes(), salt, 1);
ParametersWithIV
param = (ParametersWithIV) gen.generateDerivedParameters(
blockSize * 8,
blockSize * 8);
ref.init(true, param);
uut.init(true, param);
assertModes(ref, uut);
ref.init(false, param);
uut.init(false, param);
assertModes(ref, uut);
}
示例3: internalSetUserPassword
import org.bouncycastle.crypto.generators.PKCS5S2ParametersGenerator; //导入依赖的package包/类
private void internalSetUserPassword(SUser user, String newPassword) throws TException {
BouncyCastleUtil.ensureInitialized();
byte[] newSalt = new byte[SALT_LENGTH_BYTES];
if (useStrongRandom) {
try {
SecureRandom.getInstanceStrong().nextBytes(newSalt);
} catch (NoSuchAlgorithmException e) {
logger.error("Internal error when calculating new salt for new password", e);
throw new TException("Internal error.", e);
}
} else {
// use non-string random.
ThreadLocalRandom.current().nextBytes(newSalt);
}
PKCS5S2ParametersGenerator pbkdf2sha256 = new PKCS5S2ParametersGenerator(new SHA256Digest());
pbkdf2sha256.init(newPassword.getBytes(Charset.forName("UTF-8")), newSalt, PBKDF2_ITERATIONS);
byte[] newHash = ((KeyParameter) pbkdf2sha256.generateDerivedParameters(HASH_LENGTH_BYTES * 8)).getKey();
user.setPassword(new SPassword());
user.getPassword().setHash(newHash);
user.getPassword().setSalt(newSalt);
}
示例4: getEncoded
import org.bouncycastle.crypto.generators.PKCS5S2ParametersGenerator; //导入依赖的package包/类
byte[] getEncoded(String algorithmOid)
{
PKCS5S2ParametersGenerator gen = new PKCS5S2ParametersGenerator();
gen.init(PBEParametersGenerator.PKCS5PasswordToUTF8Bytes(this.getPassword()), this.getSalt(), this.getIterationCount());
return ((KeyParameter)gen.generateDerivedParameters(CMSEnvelopedHelper.INSTANCE.getKeySize(algorithmOid))).getKey();
}
示例5: getEncoded
import org.bouncycastle.crypto.generators.PKCS5S2ParametersGenerator; //导入依赖的package包/类
byte[] getEncoded(String algorithmOid)
{
PKCS5S2ParametersGenerator gen = new PKCS5S2ParametersGenerator();
gen.init(PBEParametersGenerator.PKCS5PasswordToBytes(this.getPassword()), this.getSalt(), this.getIterationCount());
return ((KeyParameter)gen.generateDerivedParameters(CMSEnvelopedHelper.INSTANCE.getKeySize(algorithmOid))).getKey();
}
示例6: generateSecretKeyForPKCS5Scheme2
import org.bouncycastle.crypto.generators.PKCS5S2ParametersGenerator; //导入依赖的package包/类
public 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);
}
示例7: generateAes128CtrDerivedKey
import org.bouncycastle.crypto.generators.PKCS5S2ParametersGenerator; //导入依赖的package包/类
private static byte[] generateAes128CtrDerivedKey(
byte[] password, byte[] salt, int c, String prf) throws CipherException {
if (!prf.equals("hmac-sha256")) {
throw new CipherException("Unsupported prf:" + prf);
}
// Java 8 supports this, but you have to convert the password to a character array, see
// http://stackoverflow.com/a/27928435/3211687
PKCS5S2ParametersGenerator gen = new PKCS5S2ParametersGenerator(new SHA256Digest());
gen.init(password, salt, c);
return ((KeyParameter) gen.generateDerivedParameters(256)).getKey();
}
示例8: calculateDerivedKey
import org.bouncycastle.crypto.generators.PKCS5S2ParametersGenerator; //导入依赖的package包/类
protected byte[] calculateDerivedKey(int schemeID, AlgorithmIdentifier derivationAlgorithm, int keySize)
throws CMSException
{
PBKDF2Params params = PBKDF2Params.getInstance(derivationAlgorithm.getParameters());
byte[] encodedPassword = (schemeID == PasswordRecipient.PKCS5_SCHEME2) ? PBEParametersGenerator.PKCS5PasswordToBytes(password) : PBEParametersGenerator.PKCS5PasswordToUTF8Bytes(password);
PKCS5S2ParametersGenerator gen = new PKCS5S2ParametersGenerator();
gen.init(encodedPassword, params.getSalt(), params.getIterationCount().intValue());
return ((KeyParameter)gen.generateDerivedParameters(keySize)).getKey();
}
示例9: calculateDerivedKey
import org.bouncycastle.crypto.generators.PKCS5S2ParametersGenerator; //导入依赖的package包/类
public byte[] calculateDerivedKey(int schemeID, AlgorithmIdentifier derivationAlgorithm, int keySize)
throws CMSException
{
PBKDF2Params params = PBKDF2Params.getInstance(derivationAlgorithm.getParameters());
byte[] encodedPassword = (schemeID == PasswordRecipient.PKCS5_SCHEME2) ? PBEParametersGenerator.PKCS5PasswordToBytes(password) : PBEParametersGenerator.PKCS5PasswordToUTF8Bytes(password);
PKCS5S2ParametersGenerator gen = new PKCS5S2ParametersGenerator();
gen.init(encodedPassword, params.getSalt(), params.getIterationCount().intValue());
return ((KeyParameter)gen.generateDerivedParameters(keySize)).getKey();
}
示例10: encrypt
import org.bouncycastle.crypto.generators.PKCS5S2ParametersGenerator; //导入依赖的package包/类
/**
* Encrypt and encrypt the plain text.
*
* @param secret Password to be encrypted.
* @param cipherKey Password use for encryption.
* @return Encrypted value.
* @throws Exception If an error occurred while encrypting.
*/
private static String encrypt(String secret, char[] cipherKey) throws EncryptingException {
try {
// Change char array to byte array.
ByteBuffer buf = StandardCharsets.UTF_8.encode(CharBuffer.wrap(cipherKey));
byte[] secretKey = new byte[buf.limit()];
buf.get(secretKey);
PKCS5S2ParametersGenerator gen = new PKCS5S2ParametersGenerator(new SHA256Digest());
gen.init(secretKey, SALT.getBytes(), KEY_DERIVATION_ITERATION_COUNT);
byte[] key = ((KeyParameter) gen.generateDerivedParameters(KEY_SIZE)).getKey();
SecretKey secretKeySpec = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance(ALGORITHM);
// Create an initialization vector with Cipher's block size.
byte[] iv = new byte[cipher.getBlockSize()];
IvParameterSpec ivParams = new IvParameterSpec(iv);
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParams);
// Encrypt the password.
byte[] encryptedVal = cipher.doFinal(secret.getBytes(StandardCharsets.UTF_8));
// Encode the password.
byte[] encodedVal = new Base64().encode(encryptedVal);
return new String(encodedVal, StandardCharsets.UTF_8);
} catch (NoSuchAlgorithmException | InvalidKeyException | InvalidAlgorithmParameterException |
NoSuchPaddingException | BadPaddingException | IllegalBlockSizeException ex) {
throw new EncryptingException("Error while encrypting", ex);
}
}
示例11: decrypt
import org.bouncycastle.crypto.generators.PKCS5S2ParametersGenerator; //导入依赖的package包/类
/**
* Decrypt and decrypt the encrypted values.
*
* @param encryptedSecret encrypted value.
* @param cipherKey password used for encryption.
* @return
* @throws SSOAgentException If an error occurred.
*/
public static String decrypt(String encryptedSecret, char[] cipherKey) throws SSOAgentException {
try {
// Change char array to byte array.
ByteBuffer buf = StandardCharsets.UTF_8.encode(CharBuffer.wrap(cipherKey));
byte[] secretKey = new byte[buf.limit()];
buf.get(secretKey);
PKCS5S2ParametersGenerator gen = new PKCS5S2ParametersGenerator(new SHA256Digest());
gen.init(secretKey, SALT.getBytes(StandardCharsets.UTF_8), KEY_DERIVATION_ITERATION_COUNT);
byte[] dk = ((KeyParameter) gen.generateDerivedParameters(KEY_SIZE)).getKey();
SecretKey key = new SecretKeySpec(dk, "AES");
Cipher cipher = Cipher.getInstance(ALGORITHM);
// Create an initialization vector with Cipher's block size.
byte[] iv = new byte[cipher.getBlockSize()];
IvParameterSpec ivParams = new IvParameterSpec(iv);
cipher.init(Cipher.DECRYPT_MODE, key, ivParams);
// Decode the encrypted value.
byte[] decodedValue = new Base64().decode(encryptedSecret.getBytes(StandardCharsets.UTF_8));
// Decrypt the encrypted value and get the plain text password.
byte[] decryptedValue = cipher.doFinal(decodedValue);
return new String(decryptedValue, StandardCharsets.UTF_8);
} catch (Exception ex) {
throw new SSOAgentException("Error while decoding the encrypted value.", ex);
}
}
示例12: generateSecretKeyForPKCS5Scheme2
import org.bouncycastle.crypto.generators.PKCS5S2ParametersGenerator; //导入依赖的package包/类
public static KeyParameter generateSecretKeyForPKCS5Scheme2(String algorithm, char[] password, byte[] salt, int iterationCount)
{
PBEParametersGenerator paramsGen = new PKCS5S2ParametersGenerator(new SHA1Digest());
paramsGen.init(PBEParametersGenerator.PKCS5PasswordToBytes(password), salt, iterationCount);
return (KeyParameter)paramsGen.generateDerivedParameters(PEMUtilities.getKeySize(algorithm));
}
示例13: readInventoryFromBytes
import org.bouncycastle.crypto.generators.PKCS5S2ParametersGenerator; //导入依赖的package包/类
@Override
public Inventory readInventoryFromBytes(byte[] source, UserSecurityProvider usp) throws BaseBunkrException
{
try
{
if (this.encryptionAlgorithm == Encryption.NONE)
throw new IllegalArgumentException("PBKDF2Descriptor requires an active encryption mode");
PKCS5S2ParametersGenerator g = new PKCS5S2ParametersGenerator(new SHA256Digest());
g.init(usp.getHashedPassword(), this.pbkdf2Salt, this.pbkdf2Iterations);
ParametersWithIV kp = (ParametersWithIV) g.generateDerivedParameters(
this.encryptionAlgorithm.keyByteLength * 8,
this.encryptionAlgorithm.ivByteLength * 8);
byte[] decryptedInv = SimpleBlockCipher.decrypt(
this.encryptionAlgorithm,
source,
((KeyParameter) kp.getParameters()).getKey(),
kp.getIV()
);
return InventoryJSON.decode(new String(decryptedInv));
}
catch (IllegalPasswordException | CryptoException e)
{
throw new BaseBunkrException(e);
}
}
示例14: writeInventoryToBytes
import org.bouncycastle.crypto.generators.PKCS5S2ParametersGenerator; //导入依赖的package包/类
@Override
public byte[] writeInventoryToBytes(Inventory source, UserSecurityProvider usp) throws BaseBunkrException
{
try
{
byte[] inventoryJsonBytes = InventoryJSON.encode(source).getBytes();
if (this.encryptionAlgorithm == Encryption.NONE)
throw new IllegalArgumentException("PBKDF2Descriptor requires an active encryption mode");
// first refresh the salt
RandomMaker.fill(this.pbkdf2Salt);
PKCS5S2ParametersGenerator g = new PKCS5S2ParametersGenerator(new SHA256Digest());
g.init(usp.getHashedPassword(), this.pbkdf2Salt, this.pbkdf2Iterations);
ParametersWithIV kp = (ParametersWithIV) g.generateDerivedParameters(
this.encryptionAlgorithm.keyByteLength * 8,
this.encryptionAlgorithm.ivByteLength * 8);
// encrypt the inventory
byte[] encryptedInv = SimpleBlockCipher.encrypt(
this.encryptionAlgorithm,
inventoryJsonBytes,
((KeyParameter) kp.getParameters()).getKey(),
kp.getIV()
);
Arrays.fill(inventoryJsonBytes, (byte) 0);
return encryptedInv;
}
catch (IllegalPasswordException | CryptoException e)
{
throw new BaseBunkrException(e);
}
}
示例15: deriveKey
import org.bouncycastle.crypto.generators.PKCS5S2ParametersGenerator; //导入依赖的package包/类
private static byte[] deriveKey(char[] password, byte[] salt) throws UnsupportedEncodingException {
// generate key using PBKDF2
PKCS5S2ParametersGenerator gen = new PKCS5S2ParametersGenerator(new SHA512Digest());
gen.init(new String(password).getBytes("UTF-8"), salt, PBKDF2_ITERATIONS);
return ((KeyParameter) gen.generateDerivedParameters(AES_KEY_SIZE_BITS)).getKey();
}