use of org.gudy.bouncycastle.asn1.ASN1InputStream in project aion by aionnetwork.
the class ECDSASignature method decodeFromDER.
public static ECDSASignature decodeFromDER(byte[] bytes) {
ASN1InputStream decoder = null;
try {
decoder = new ASN1InputStream(bytes);
DLSequence seq = (DLSequence) decoder.readObject();
if (seq == null) {
throw new RuntimeException("Reached past end of ASN.1 stream.");
}
ASN1Integer r, s;
try {
r = (ASN1Integer) seq.getObjectAt(0);
s = (ASN1Integer) seq.getObjectAt(1);
} catch (ClassCastException e) {
throw new IllegalArgumentException(e);
}
// http://r6.ca/blog/20111119T211504Z.html
return new ECDSASignature(r.getPositiveValue(), s.getPositiveValue());
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (decoder != null) {
try {
decoder.close();
} catch (IOException x) {
}
}
}
}
use of org.gudy.bouncycastle.asn1.ASN1InputStream in project nuls by nuls-io.
the class SM2Utils method decrypt.
public static byte[] decrypt(byte[] privateKey, byte[] encryptedData) throws IOException {
if (privateKey == null || privateKey.length == 0) {
return null;
}
if (encryptedData == null || encryptedData.length == 0) {
return null;
}
byte[] enc = new byte[encryptedData.length];
System.arraycopy(encryptedData, 0, enc, 0, encryptedData.length);
SM2 sm2 = SM2.Instance();
BigInteger userD = new BigInteger(1, privateKey);
ByteArrayInputStream bis = new ByteArrayInputStream(enc);
ASN1InputStream dis = new ASN1InputStream(bis);
DERObject derObj = dis.readObject();
ASN1Sequence asn1 = (ASN1Sequence) derObj;
DERInteger x = (DERInteger) asn1.getObjectAt(0);
DERInteger y = (DERInteger) asn1.getObjectAt(1);
ECPoint c1 = sm2.ecc_curve.createPoint(x.getValue(), y.getValue(), true);
Cipher cipher = new Cipher();
cipher.initDec(userD, c1);
DEROctetString data = (DEROctetString) asn1.getObjectAt(3);
enc = data.getOctets();
cipher.decrypt(enc);
byte[] c3 = new byte[32];
cipher.dofinal(c3);
return enc;
}
use of org.gudy.bouncycastle.asn1.ASN1InputStream in project BiglyBT by BiglySoftware.
the class PEMReader method readRSAPublicKey.
private PublicKey readRSAPublicKey(String endMarker) throws IOException {
ByteArrayInputStream bAIS = new ByteArrayInputStream(readBytes(endMarker));
ASN1InputStream ais = new ASN1InputStream(bAIS);
Object asnObject = ais.readObject();
ASN1Sequence sequence = (ASN1Sequence) asnObject;
RSAPublicKeyStructure rsaPubStructure = new RSAPublicKeyStructure(sequence);
RSAPublicKeySpec keySpec = new RSAPublicKeySpec(rsaPubStructure.getModulus(), rsaPubStructure.getPublicExponent());
try {
KeyFactory keyFact = KeyFactory.getInstance("RSA", provider);
return keyFact.generatePublic(keySpec);
} catch (NoSuchProviderException e) {
throw new IOException("can't find provider " + provider);
} catch (Exception e) {
throw new IOException("problem extracting key: " + e.toString());
}
}
use of org.gudy.bouncycastle.asn1.ASN1InputStream in project BiglyBT by BiglySoftware.
the class PrincipalUtil method getIssuerX509Principal.
/**
* return the issuer of the given CRL as an X509PrincipalObject.
*/
public static X509Principal getIssuerX509Principal(X509CRL crl) throws CRLException {
try {
ByteArrayInputStream bIn = new ByteArrayInputStream(crl.getTBSCertList());
ASN1InputStream aIn = new ASN1InputStream(bIn);
TBSCertList tbsCertList = new TBSCertList((ASN1Sequence) aIn.readObject());
return new X509Principal(tbsCertList.getIssuer());
} catch (IOException e) {
throw new CRLException(e.toString());
}
}
use of org.gudy.bouncycastle.asn1.ASN1InputStream in project BiglyBT by BiglySoftware.
the class PrincipalUtil method getIssuerX509Principal.
/**
* return the issuer of the given cert as an X509PrincipalObject.
*/
public static X509Principal getIssuerX509Principal(X509Certificate cert) throws CertificateEncodingException {
try {
ByteArrayInputStream bIn = new ByteArrayInputStream(cert.getTBSCertificate());
ASN1InputStream aIn = new ASN1InputStream(bIn);
TBSCertificateStructure tbsCert = new TBSCertificateStructure((ASN1Sequence) aIn.readObject());
return new X509Principal(tbsCert.getIssuer());
} catch (IOException e) {
throw new CertificateEncodingException(e.toString());
}
}
Aggregations