本文整理汇总了Java中org.bouncycastle.crypto.generators.PKCS5S2ParametersGenerator.init方法的典型用法代码示例。如果您正苦于以下问题:Java PKCS5S2ParametersGenerator.init方法的具体用法?Java PKCS5S2ParametersGenerator.init怎么用?Java PKCS5S2ParametersGenerator.init使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.bouncycastle.crypto.generators.PKCS5S2ParametersGenerator
的用法示例。
在下文中一共展示了PKCS5S2ParametersGenerator.init方法的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: 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);
}
示例3: 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();
}
示例4: 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();
}
示例5: 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();
}
示例6: 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();
}
示例7: 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();
}
示例8: 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);
}
}
示例9: 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);
}
}
示例10: 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);
}
}
示例11: 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);
}
}
示例12: 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();
}
示例13: testPBKDF
import org.bouncycastle.crypto.generators.PKCS5S2ParametersGenerator; //导入方法依赖的package包/类
@Test
public void testPBKDF() throws Exception {
Security.addProvider(new BouncyCastleProvider());
Random r = new Random();
char[] cPassword = "ThePa55wordToU5e".toCharArray();
byte[] salt = new byte[64];
r.nextBytes(salt);
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
KeySpec keyspec = new PBEKeySpec(cPassword, salt, 5000, 256);
Key key = factory.generateSecret(keyspec);
byte[] k1 = key.getEncoded();
factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1", "BC");
key = factory.generateSecret(keyspec);
byte[] k2 = key.getEncoded();
assertArrayEquals(k1, k2);
PKCS5S2ParametersGenerator gen = new PKCS5S2ParametersGenerator(new SHA1Digest());
gen.init(new String(cPassword).getBytes("UTF-8"), salt, 5000);
byte[] k3 = ((KeyParameter) gen.generateDerivedParameters(256)).getKey();
assertArrayEquals(k1, k3);
assertArrayEquals(k2, k3);
}
示例14: encode
import org.bouncycastle.crypto.generators.PKCS5S2ParametersGenerator; //导入方法依赖的package包/类
public String encode(String password, byte[] salt, int rounds)
throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeySpecException {
PKCS5S2ParametersGenerator generator = new PKCS5S2ParametersGenerator();
generator.init(PBEParametersGenerator.PKCS5PasswordToBytes(
password.toCharArray()),
salt,
rounds);
return format("%s:%s:%d", encode(salt),
encode(((KeyParameter)generator.generateDerivedParameters(s_keylen)).getKey()), rounds);
}
示例15: main
import org.bouncycastle.crypto.generators.PKCS5S2ParametersGenerator; //导入方法依赖的package包/类
public static void main(
String[] args)
{
HMac gMac = new HMac(new GOST3411Digest(GOST28147Engine.getSBox("D-Test")));
gMac.init(new KeyParameter(PKCS5S1ParametersGenerator.PKCS5PasswordToUTF8Bytes("Boss".toCharArray())));
byte[] iBuf = new byte[4];
byte[] data = Hex.decode("b5d78fa546ba645c");
gMac.update(data, 0, data.length);
byte[] mac = new byte[gMac.getMacSize()];
int pos = 3;
while (++iBuf[pos] == 0)
{
--pos;
}
gMac.update(iBuf, 0, iBuf.length);
gMac.doFinal(mac, 0);
System.err.println(mac.length + " " + new String(Hex.encode(mac)));
PKCS5S2ParametersGenerator pGen = new PKCS5S2ParametersGenerator(new GOST3411Digest());
pGen.init(PKCS5S1ParametersGenerator.PKCS5PasswordToUTF8Bytes("1".toCharArray()), data, 2048);
KeyParameter kp = (KeyParameter)pGen.generateDerivedMacParameters(256);
System.err.println(kp.getKey().length + " " + new String(Hex.encode(kp.getKey())));
runTest(new GOST3411DigestTest());
}