use of com.github.zhenwei.pkix.operator.jcajce.JceGenericKey in project LinLong-Java by zhenwei1108.
the class JceKEKAuthenticatedRecipient method getRecipientOperator.
public RecipientOperator getRecipientOperator(AlgorithmIdentifier keyEncryptionAlgorithm, final AlgorithmIdentifier contentMacAlgorithm, byte[] encryptedContentEncryptionKey) throws CMSException {
final Key secretKey = extractSecretKey(keyEncryptionAlgorithm, contentMacAlgorithm, encryptedContentEncryptionKey);
final Mac dataMac = contentHelper.createContentMac(secretKey, contentMacAlgorithm);
return new RecipientOperator(new MacCalculator() {
public AlgorithmIdentifier getAlgorithmIdentifier() {
return contentMacAlgorithm;
}
public GenericKey getKey() {
return new JceGenericKey(contentMacAlgorithm, secretKey);
}
public OutputStream getOutputStream() {
return new MacOutputStream(dataMac);
}
public byte[] getMac() {
return dataMac.doFinal();
}
});
}
use of com.github.zhenwei.pkix.operator.jcajce.JceGenericKey in project LinLong-Java by zhenwei1108.
the class JceOpenSSLPKCS8EncryptorBuilder method build.
public OutputEncryptor build() throws OperatorCreationException {
final AlgorithmIdentifier algID;
if (random == null) {
random = new SecureRandom();
}
try {
this.cipher = helper.createCipher(algOID.getId());
if (PEMUtilities.isPKCS5Scheme2(algOID)) {
this.paramGen = helper.createAlgorithmParameterGenerator(algOID.getId());
}
} catch (GeneralSecurityException e) {
throw new OperatorCreationException(algOID + " not available: " + e.getMessage(), e);
}
if (PEMUtilities.isPKCS5Scheme2(algOID)) {
salt = new byte[PEMUtilities.getSaltSize(prf.getAlgorithm())];
random.nextBytes(salt);
params = paramGen.generateParameters();
try {
EncryptionScheme scheme = new EncryptionScheme(algOID, ASN1Primitive.fromByteArray(params.getEncoded()));
KeyDerivationFunc func = new KeyDerivationFunc(PKCSObjectIdentifiers.id_PBKDF2, new PBKDF2Params(salt, iterationCount, prf));
ASN1EncodableVector v = new ASN1EncodableVector();
v.add(func);
v.add(scheme);
algID = new AlgorithmIdentifier(PKCSObjectIdentifiers.id_PBES2, PBES2Parameters.getInstance(new DERSequence(v)));
} catch (IOException e) {
throw new OperatorCreationException(e.getMessage(), e);
}
try {
if (PEMUtilities.isHmacSHA1(prf)) {
key = PEMUtilities.generateSecretKeyForPKCS5Scheme2(helper, algOID.getId(), password, salt, iterationCount);
} else {
key = PEMUtilities.generateSecretKeyForPKCS5Scheme2(helper, algOID.getId(), password, salt, iterationCount, prf);
}
cipher.init(Cipher.ENCRYPT_MODE, key, params);
} catch (GeneralSecurityException e) {
throw new OperatorCreationException(e.getMessage(), e);
}
} else if (PEMUtilities.isPKCS12(algOID)) {
ASN1EncodableVector v = new ASN1EncodableVector();
salt = new byte[20];
random.nextBytes(salt);
v.add(new DEROctetString(salt));
v.add(new ASN1Integer(iterationCount));
algID = new AlgorithmIdentifier(algOID, PKCS12PBEParams.getInstance(new DERSequence(v)));
try {
cipher.init(Cipher.ENCRYPT_MODE, new PKCS12KeyWithParameters(password, salt, iterationCount));
} catch (GeneralSecurityException e) {
throw new OperatorCreationException(e.getMessage(), e);
}
} else {
throw new OperatorCreationException("unknown algorithm: " + algOID, null);
}
return new OutputEncryptor() {
public AlgorithmIdentifier getAlgorithmIdentifier() {
return algID;
}
public OutputStream getOutputStream(OutputStream encOut) {
return new CipherOutputStream(encOut, cipher);
}
public GenericKey getKey() {
return new JceGenericKey(algID, key);
}
};
}
use of com.github.zhenwei.pkix.operator.jcajce.JceGenericKey in project LinLong-Java by zhenwei1108.
the class JceKeyTransAuthenticatedRecipient method getRecipientOperator.
public RecipientOperator getRecipientOperator(AlgorithmIdentifier keyEncryptionAlgorithm, final AlgorithmIdentifier contentMacAlgorithm, byte[] encryptedContentEncryptionKey) throws CMSException {
final Key secretKey = extractSecretKey(keyEncryptionAlgorithm, contentMacAlgorithm, encryptedContentEncryptionKey);
final Mac dataMac = contentHelper.createContentMac(secretKey, contentMacAlgorithm);
return new RecipientOperator(new MacCalculator() {
public AlgorithmIdentifier getAlgorithmIdentifier() {
return contentMacAlgorithm;
}
public GenericKey getKey() {
return new JceGenericKey(contentMacAlgorithm, secretKey);
}
public OutputStream getOutputStream() {
return new MacOutputStream(dataMac);
}
public byte[] getMac() {
return dataMac.doFinal();
}
});
}
use of com.github.zhenwei.pkix.operator.jcajce.JceGenericKey in project LinLong-Java by zhenwei1108.
the class JcePasswordAuthenticatedRecipient method getRecipientOperator.
public RecipientOperator getRecipientOperator(AlgorithmIdentifier keyEncryptionAlgorithm, final AlgorithmIdentifier contentMacAlgorithm, byte[] derivedKey, byte[] encryptedContentEncryptionKey) throws CMSException {
final Key secretKey = extractSecretKey(keyEncryptionAlgorithm, contentMacAlgorithm, derivedKey, encryptedContentEncryptionKey);
final Mac dataMac = helper.createContentMac(secretKey, contentMacAlgorithm);
return new RecipientOperator(new MacCalculator() {
public AlgorithmIdentifier getAlgorithmIdentifier() {
return contentMacAlgorithm;
}
public GenericKey getKey() {
return new JceGenericKey(contentMacAlgorithm, secretKey);
}
public OutputStream getOutputStream() {
return new MacOutputStream(dataMac);
}
public byte[] getMac() {
return dataMac.doFinal();
}
});
}
use of com.github.zhenwei.pkix.operator.jcajce.JceGenericKey in project LinLong-Java by zhenwei1108.
the class JceKTSKeyTransAuthenticatedRecipient method getRecipientOperator.
public RecipientOperator getRecipientOperator(AlgorithmIdentifier keyEncryptionAlgorithm, final AlgorithmIdentifier contentMacAlgorithm, byte[] encryptedContentEncryptionKey) throws CMSException {
final Key secretKey = extractSecretKey(keyEncryptionAlgorithm, contentMacAlgorithm, encryptedContentEncryptionKey);
final Mac dataMac = contentHelper.createContentMac(secretKey, contentMacAlgorithm);
return new RecipientOperator(new MacCalculator() {
public AlgorithmIdentifier getAlgorithmIdentifier() {
return contentMacAlgorithm;
}
public GenericKey getKey() {
return new JceGenericKey(contentMacAlgorithm, secretKey);
}
public OutputStream getOutputStream() {
return new MacOutputStream(dataMac);
}
public byte[] getMac() {
return dataMac.doFinal();
}
});
}
Aggregations