public PasswordRecipientInfoGenerator setSaltAndIterationCount(byte[] salt, int iterationCount) { this.keyDerivationAlgorithm = new AlgorithmIdentifier(PKCSObjectIdentifiers.id_PBKDF2, new PBKDF2Params(salt, iterationCount)); return this; }
byte[] calculateDerivedKey(int schemeID, char[] password, AlgorithmIdentifier derivationAlgorithm, int keySize) throws CMSException { PBKDF2Params params = PBKDF2Params.getInstance(derivationAlgorithm.getParameters()); try { SecretKeyFactory keyFact; if (schemeID == PasswordRecipient.PKCS5_SCHEME2) { keyFact = helper.createSecretKeyFactory("PBKDF2with8BIT"); } else { keyFact = helper.createSecretKeyFactory((String)PBKDF2_ALG_NAMES.get(params.getPrf())); } SecretKey key = keyFact.generateSecret(new PBEKeySpec(password, params.getSalt(), params.getIterationCount().intValue(), keySize)); return key.getEncoded(); } catch (GeneralSecurityException e) { throw new CMSException("Unable to calculate derived key from password: " + e.getMessage(), e); } }
protected byte[] engineGetEncoded() { try { return params.getEncoded(ASN1Encoding.DER); } catch (IOException e) { throw new RuntimeException("Oooops! " + e.toString()); } }
protected AlgorithmParameterSpec localEngineGetParameterSpec( Class paramSpec) throws InvalidParameterSpecException { if (paramSpec == PBEParameterSpec.class) { return new PBEParameterSpec(params.getSalt(), params.getIterationCount().intValue()); } throw new InvalidParameterSpecException("unknown parameter spec passed to PBKDF2 PBE parameters object."); }
byte[] calculateDerivedKey(int schemeID, char[] password, AlgorithmIdentifier derivationAlgorithm, int keySize) throws CMSException { PBKDF2Params params = PBKDF2Params.getInstance(derivationAlgorithm.getParameters()); try { SecretKeyFactory keyFact; if (schemeID == PasswordRecipient.PKCS5_SCHEME2) { keyFact = helper.createSecretKeyFactory("PBKDF2with8BIT"); } else { keyFact = helper.createSecretKeyFactory("PBKDF2"); } SecretKey key = keyFact.generateSecret(new PBEKeySpec(password, params.getSalt(), params.getIterationCount().intValue(), keySize)); return key.getEncoded(); } catch (GeneralSecurityException e) { throw new CMSException("Unable to calculate dervied key from password: " + e.getMessage(), e); } }
PBKDF2Params defParams = (PBKDF2Params)func.getParameters(); int iterationCount = defParams.getIterationCount().intValue(); byte[] salt = defParams.getSalt(); if (PEMUtilities.isHmacSHA1(defParams.getPrf())) key = PEMUtilities.generateSecretKeyForPKCS5Scheme2(helper, oid, password, salt, iterationCount, defParams.getPrf());
protected void engineInit( byte[] params, String format) throws IOException { if (this.isASN1FormatString(format)) { engineInit(params); return; } throw new IOException("Unknown parameters format in PWRIKEK parameters object"); }
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(); }
protected AlgorithmParameterSpec localEngineGetParameterSpec( Class paramSpec) throws InvalidParameterSpecException { if (paramSpec == PBEParameterSpec.class) { return new PBEParameterSpec(params.getSalt(), params.getIterationCount().intValue()); } throw new InvalidParameterSpecException("unknown parameter spec passed to PKCS12 PBE parameters object."); }
protected void engineInit( byte[] params, String format) throws IOException { if (this.isASN1FormatString(format)) { engineInit(params); return; } throw new IOException("Unknown parameters format in PBKDF2 parameters object"); }
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); try { PKCS5S2ParametersGenerator gen = new PKCS5S2ParametersGenerator(EnvelopedDataHelper.getPRF(params.getPrf())); gen.init(encodedPassword, params.getSalt(), params.getIterationCount().intValue()); return ((KeyParameter)gen.generateDerivedParameters(keySize)).getKey(); } catch (Exception e) { throw new CMSException("exception creating derived key: " + e.getMessage(), e); } }
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(); }
public static PBKDF2Params getInstance( Object obj) { if (obj instanceof PBKDF2Params) { return (PBKDF2Params)obj; } if (obj != null) { return new PBKDF2Params(ASN1Sequence.getInstance(obj)); } return null; }
protected AlgorithmParameterSpec localEngineGetParameterSpec( Class paramSpec) throws InvalidParameterSpecException { if (paramSpec == PBEParameterSpec.class) { return new PBEParameterSpec(params.getSalt(), params.getIterationCount().intValue()); } throw new InvalidParameterSpecException("unknown parameter spec passed to PKCS12 PBE parameters object."); }
protected void engineInit( byte[] params, String format) throws IOException { if (isASN1FormatString(format)) { engineInit(params); return; } throw new IOException("Unknown parameters format in PWRIKEK parameters object"); }
protected byte[] engineGetEncoded() { try { return params.getEncoded(ASN1Encoding.DER); } catch (IOException e) { throw new RuntimeException("Oooops! " + e.toString()); } }
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); try { PKCS5S2ParametersGenerator gen = new PKCS5S2ParametersGenerator(EnvelopedDataHelper.getPRF(params.getPrf())); gen.init(encodedPassword, params.getSalt(), params.getIterationCount().intValue()); return ((KeyParameter)gen.generateDerivedParameters(keySize)).getKey(); } catch (Exception e) { throw new CMSException("exception creating derived key: " + e.getMessage(), e); } }
/** * Create PBKDF2Params from the passed in object, * * @param obj either PBKDF2Params or an ASN1Sequence. * @return a PBKDF2Params instance. */ public static PBKDF2Params getInstance( Object obj) { if (obj instanceof PBKDF2Params) { return (PBKDF2Params)obj; } if (obj != null) { return new PBKDF2Params(ASN1Sequence.getInstance(obj)); } return null; }
PBKDF2Params defParams = (PBKDF2Params)func.getParameters(); int iterationCount = defParams.getIterationCount().intValue(); byte[] salt = defParams.getSalt();
public PBES2Algorithms( ASN1Sequence obj) { super(obj); Enumeration e = obj.getObjects(); objectId = (ASN1ObjectIdentifier)e.nextElement(); ASN1Sequence seq = (ASN1Sequence)e.nextElement(); e = seq.getObjects(); ASN1Sequence funcSeq = (ASN1Sequence)e.nextElement(); if (funcSeq.getObjectAt(0).equals(id_PBKDF2)) { func = new KeyDerivationFunc(id_PBKDF2, PBKDF2Params.getInstance(funcSeq.getObjectAt(1))); } else { func = new KeyDerivationFunc(funcSeq); } scheme = new EncryptionScheme((ASN1Sequence)e.nextElement()); }