use of org.bouncycastle.asn1.DERSequence in project keystore-explorer by kaikramer.
the class OpenSslPvkUtil method get.
/**
* OpenSSL encode a private key.
*
* @return The encoding
* @param privateKey
* The private key
* @throws CryptoException
* Problem encountered while getting the encoded private key
*/
public static byte[] get(PrivateKey privateKey) throws CryptoException {
// DER encoding for each key type is a sequence
ASN1EncodableVector vec = new ASN1EncodableVector();
if (privateKey instanceof ECPrivateKey) {
try {
ECPrivateKey ecPrivKey = (ECPrivateKey) privateKey;
org.bouncycastle.asn1.sec.ECPrivateKey keyStructure = EccUtil.convertToECPrivateKeyStructure(ecPrivKey);
return keyStructure.toASN1Primitive().getEncoded();
} catch (IOException e) {
throw new CryptoException(res.getString("NoDerEncodeOpenSslPrivateKey.exception.message"), e);
}
} else if (privateKey instanceof RSAPrivateCrtKey) {
RSAPrivateCrtKey rsaPrivateKey = (RSAPrivateCrtKey) privateKey;
vec.add(new ASN1Integer(VERSION));
vec.add(new ASN1Integer(rsaPrivateKey.getModulus()));
vec.add(new ASN1Integer(rsaPrivateKey.getPublicExponent()));
vec.add(new ASN1Integer(rsaPrivateKey.getPrivateExponent()));
vec.add(new ASN1Integer(rsaPrivateKey.getPrimeP()));
vec.add(new ASN1Integer(rsaPrivateKey.getPrimeQ()));
vec.add(new ASN1Integer(rsaPrivateKey.getPrimeExponentP()));
vec.add(new ASN1Integer(rsaPrivateKey.getPrimeExponentQ()));
vec.add(new ASN1Integer(rsaPrivateKey.getCrtCoefficient()));
} else {
DSAPrivateKey dsaPrivateKey = (DSAPrivateKey) privateKey;
DSAParams dsaParams = dsaPrivateKey.getParams();
BigInteger primeModulusP = dsaParams.getP();
BigInteger primeQ = dsaParams.getQ();
BigInteger generatorG = dsaParams.getG();
BigInteger secretExponentX = dsaPrivateKey.getX();
// Derive public key from private key parts, ie Y = G^X mod P
BigInteger publicExponentY = generatorG.modPow(secretExponentX, primeModulusP);
vec.add(new ASN1Integer(VERSION));
vec.add(new ASN1Integer(primeModulusP));
vec.add(new ASN1Integer(primeQ));
vec.add(new ASN1Integer(generatorG));
vec.add(new ASN1Integer(publicExponentY));
vec.add(new ASN1Integer(secretExponentX));
}
DERSequence derSequence = new DERSequence(vec);
try {
return derSequence.getEncoded();
} catch (IOException ex) {
throw new CryptoException(res.getString("NoDerEncodeOpenSslPrivateKey.exception.message"), ex);
}
}
use of org.bouncycastle.asn1.DERSequence in project keystore-explorer by kaikramer.
the class Spkac method encodeRsaPublicKeyAsBitString.
private byte[] encodeRsaPublicKeyAsBitString(RSAPublicKey rsaPublicKey) throws SpkacException {
try {
ASN1EncodableVector vec = new ASN1EncodableVector();
vec.add(new ASN1Integer(rsaPublicKey.getModulus()));
vec.add(new ASN1Integer(rsaPublicKey.getPublicExponent()));
DERSequence derSequence = new DERSequence(vec);
return derSequence.getEncoded(ASN1Encoding.DER);
} catch (Exception ex) {
throw new SpkacException(res.getString("NoEncodeRsaPublicKey.exception.message"), ex);
}
}
use of org.bouncycastle.asn1.DERSequence in project nuls by nuls-io.
the class SM2Utils method encrypt.
public static byte[] encrypt(byte[] publicKey, byte[] data) throws IOException {
if (publicKey == null || publicKey.length == 0) {
return null;
}
if (data == null || data.length == 0) {
return null;
}
byte[] source = new byte[data.length];
System.arraycopy(data, 0, source, 0, data.length);
Cipher cipher = new Cipher();
SM2 sm2 = SM2.Instance();
ECPoint userKey = sm2.ecc_curve.decodePoint(publicKey);
ECPoint c1 = cipher.initEnc(sm2, userKey);
cipher.encrypt(source);
byte[] c3 = new byte[32];
cipher.dofinal(c3);
DERInteger x = new DERInteger(c1.getX().toBigInteger());
DERInteger y = new DERInteger(c1.getY().toBigInteger());
DEROctetString derDig = new DEROctetString(c3);
DEROctetString derEnc = new DEROctetString(source);
ASN1EncodableVector v = new ASN1EncodableVector();
v.add(x);
v.add(y);
v.add(derDig);
v.add(derEnc);
DERSequence seq = new DERSequence(v);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DEROutputStream dos = new DEROutputStream(bos);
dos.writeObject(seq);
return bos.toByteArray();
}
use of org.bouncycastle.asn1.DERSequence in project xipki by xipki.
the class XmlX509CertprofileUtil method createPolicyQualifiers.
private static ASN1Sequence createPolicyQualifiers(List<CertificatePolicyQualifier> qualifiers) {
ParamUtil.requireNonNull("qualifiers", qualifiers);
List<PolicyQualifierInfo> qualifierInfos = new ArrayList<>(qualifiers.size());
for (CertificatePolicyQualifier qualifier : qualifiers) {
PolicyQualifierInfo qualifierInfo;
if (qualifier.getCpsUri() != null) {
qualifierInfo = new PolicyQualifierInfo(qualifier.getCpsUri());
} else if (qualifier.getUserNotice() != null) {
UserNotice userNotice = new UserNotice(null, qualifier.getUserNotice());
qualifierInfo = new PolicyQualifierInfo(PKCSObjectIdentifiers.id_spq_ets_unotice, userNotice);
} else {
qualifierInfo = null;
}
if (qualifierInfo != null) {
qualifierInfos.add(qualifierInfo);
}
// PolicyQualifierId qualifierId
}
return new DERSequence(qualifierInfos.toArray(new PolicyQualifierInfo[0]));
}
use of org.bouncycastle.asn1.DERSequence in project xipki by xipki.
the class XmlX509CertprofileUtil method buildPolicyConstrains.
// method buildGeneralSubtree
public static ASN1Sequence buildPolicyConstrains(PolicyConstraints type) throws CertprofileException {
ParamUtil.requireNonNull("type", type);
Integer requireExplicitPolicy = type.getRequireExplicitPolicy();
if (requireExplicitPolicy != null && requireExplicitPolicy < 0) {
throw new CertprofileException("negative requireExplicitPolicy is not allowed: " + requireExplicitPolicy);
}
Integer inhibitPolicyMapping = type.getInhibitPolicyMapping();
if (inhibitPolicyMapping != null && inhibitPolicyMapping < 0) {
throw new CertprofileException("negative inhibitPolicyMapping is not allowed: " + inhibitPolicyMapping);
}
if (requireExplicitPolicy == null && inhibitPolicyMapping == null) {
return null;
}
final boolean explicit = false;
ASN1EncodableVector vec = new ASN1EncodableVector();
if (requireExplicitPolicy != null) {
vec.add(new DERTaggedObject(explicit, 0, new ASN1Integer(BigInteger.valueOf(requireExplicitPolicy))));
}
if (inhibitPolicyMapping != null) {
vec.add(new DERTaggedObject(explicit, 1, new ASN1Integer(BigInteger.valueOf(inhibitPolicyMapping))));
}
return new DERSequence(vec);
}
Aggregations