本文整理汇总了Java中org.spongycastle.asn1.ASN1Integer类的典型用法代码示例。如果您正苦于以下问题:Java ASN1Integer类的具体用法?Java ASN1Integer怎么用?Java ASN1Integer使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ASN1Integer类属于org.spongycastle.asn1包,在下文中一共展示了ASN1Integer类的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: toASN1
import org.spongycastle.asn1.ASN1Integer; //导入依赖的package包/类
/**
* Output this ECKey as an ASN.1 encoded private key, as understood by
* OpenSSL or used by Bitcoin Core in its wallet storage format.
*
* @throws eu.bittrade.crypto.core.ECKey.MissingPrivateKeyException
* if the private key is missing or encrypted.
*/
public byte[] toASN1() {
try {
byte[] privKeyBytes = getPrivKeyBytes();
ByteArrayOutputStream baos = new ByteArrayOutputStream(400);
// ASN1_SEQUENCE(EC_PRIVATEKEY) = {
// ASN1_SIMPLE(EC_PRIVATEKEY, version, LONG),
// ASN1_SIMPLE(EC_PRIVATEKEY, privateKey, ASN1_OCTET_STRING),
// ASN1_EXP_OPT(EC_PRIVATEKEY, parameters, ECPKPARAMETERS, 0),
// ASN1_EXP_OPT(EC_PRIVATEKEY, publicKey, ASN1_BIT_STRING, 1)
// } ASN1_SEQUENCE_END(EC_PRIVATEKEY)
DERSequenceGenerator seq = new DERSequenceGenerator(baos);
seq.addObject(new ASN1Integer(1)); // version
seq.addObject(new DEROctetString(privKeyBytes));
seq.addObject(new DERTaggedObject(0, CURVE_PARAMS.toASN1Primitive()));
seq.addObject(new DERTaggedObject(1, new DERBitString(getPubKey())));
seq.close();
return baos.toByteArray();
} catch (IOException e) {
throw new RuntimeException(e); // Cannot happen, writing to memory
// stream.
}
}
示例2: toASN1
import org.spongycastle.asn1.ASN1Integer; //导入依赖的package包/类
/**
* Output this ECKey as an ASN.1 encoded private key, as understood by OpenSSL or used by the
* BitCoin reference
* implementation in its wallet storage format.
*/
public byte[] toASN1() {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream(400);
DERSequenceGenerator seq = new DERSequenceGenerator(baos);
seq.addObject(new ASN1Integer(1)); // version
seq.addObject(new DEROctetString(priv.toByteArray()));
seq.addObject(new DERTaggedObject(0, SECNamedCurves.getByName("secp256k1")
.toASN1Primitive()));
seq.addObject(new DERTaggedObject(1, new DERBitString(getPubKey())));
seq.close();
return baos.toByteArray();
} catch (IOException e) {
throw new RuntimeException(e); // Cannot happen, writing to memory stream.
}
}
示例3: buildPublicKey
import org.spongycastle.asn1.ASN1Integer; //导入依赖的package包/类
static PublicKey buildPublicKey(byte[] rawBytes) throws CryptoException {
try {
//FIXME replacing X509EncodedKeySpec because of problem with 8.1
//Since 8.1 Bouncycastle cryptography was replaced with implementation from Conscrypt
//https://developer.android.com/about/versions/oreo/android-8.1.html
//either it's a bug in Conscrypt, our public key DER structure or use of X509EncodedKeySpec changed
//alternative needed as this adds expensive Spongycastle dependence
ASN1InputStream bIn = new ASN1InputStream(new ByteArrayInputStream(rawBytes));
SubjectPublicKeyInfo info = SubjectPublicKeyInfo
.getInstance(new ASN1InputStream(bIn.readObject().getEncoded()).readObject());
DLSequence dlSequence = (DLSequence) ASN1Primitive.fromByteArray(info.getPublicKeyData().getBytes());
BigInteger modulus = ((ASN1Integer) dlSequence.getObjectAt(0)).getPositiveValue();
BigInteger exponent = ((ASN1Integer) dlSequence.getObjectAt(1)).getPositiveValue();
RSAPublicKeySpec spec = new RSAPublicKeySpec(modulus, exponent);
KeyFactory kf = getRSAKeyFactory();
return kf.generatePublic(spec);
} catch (InvalidKeySpecException | IOException e) {
throw new CryptoException(e);
}
}
示例4: toASN1
import org.spongycastle.asn1.ASN1Integer; //导入依赖的package包/类
/**
* Output this ECKey as an ASN.1 encoded private key, as understood by OpenSSL or used by the BitCoin reference
* implementation in its wallet storage format.
*/
public byte[] toASN1() {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream(400);
// ASN1_SEQUENCE(EC_PRIVATEKEY) = {
// ASN1_SIMPLE(EC_PRIVATEKEY, version, LONG),
// ASN1_SIMPLE(EC_PRIVATEKEY, privateKey, ASN1_OCTET_STRING),
// ASN1_EXP_OPT(EC_PRIVATEKEY, parameters, ECPKPARAMETERS, 0),
// ASN1_EXP_OPT(EC_PRIVATEKEY, publicKey, ASN1_BIT_STRING, 1)
// } ASN1_SEQUENCE_END(EC_PRIVATEKEY)
DERSequenceGenerator seq = new DERSequenceGenerator(baos);
seq.addObject(new ASN1Integer(1)); // version
seq.addObject(new DEROctetString(priv.toByteArray()));
seq.addObject(new DERTaggedObject(0, SECNamedCurves.getByName("secp256k1").toASN1Primitive()));
seq.addObject(new DERTaggedObject(1, new DERBitString(getPubKey())));
seq.close();
return baos.toByteArray();
} catch (IOException e) {
throw new RuntimeException(e); // Cannot happen, writing to memory stream.
}
}
示例5: extractPrivateKeyFromASN1
import org.spongycastle.asn1.ASN1Integer; //导入依赖的package包/类
private static BigInteger extractPrivateKeyFromASN1(byte[] asn1privkey) {
// To understand this code, see the definition of the ASN.1 format for EC private keys in the OpenSSL source
// code in ec_asn1.c:
//
// ASN1_SEQUENCE(EC_PRIVATEKEY) = {
// ASN1_SIMPLE(EC_PRIVATEKEY, version, LONG),
// ASN1_SIMPLE(EC_PRIVATEKEY, privateKey, ASN1_OCTET_STRING),
// ASN1_EXP_OPT(EC_PRIVATEKEY, parameters, ECPKPARAMETERS, 0),
// ASN1_EXP_OPT(EC_PRIVATEKEY, publicKey, ASN1_BIT_STRING, 1)
// } ASN1_SEQUENCE_END(EC_PRIVATEKEY)
//
try {
ASN1InputStream decoder = new ASN1InputStream(asn1privkey);
DLSequence seq = (DLSequence) decoder.readObject();
checkArgument(seq.size() == 4, "Input does not appear to be an ASN.1 OpenSSL EC private key");
checkArgument(((ASN1Integer) seq.getObjectAt(0)).getValue().equals(BigInteger.ONE),
"Input is of wrong version");
Object obj = seq.getObjectAt(1);
byte[] bits = ((ASN1OctetString) obj).getOctets();
decoder.close();
return new BigInteger(1, bits);
} catch (IOException e) {
throw new RuntimeException(e); // Cannot happen, reading from memory stream.
}
}
示例6: derByteStream
import org.spongycastle.asn1.ASN1Integer; //导入依赖的package包/类
protected ByteArrayOutputStream derByteStream() throws IOException {
// Usually 70-72 bytes.
ByteArrayOutputStream bos = new ByteArrayOutputStream(72);
DERSequenceGenerator seq = new DERSequenceGenerator(bos);
seq.addObject(new ASN1Integer(r));
seq.addObject(new ASN1Integer(s));
seq.close();
return bos;
}
示例7: getEncoded
import org.spongycastle.asn1.ASN1Integer; //导入依赖的package包/类
/**
* DER - From Big Integer rs to byte[]
* UAF_ALG_SIGN_SECP256K1_ECDSA_SHA256_DER 0x06
* DER [ITU-X690-2008] encoded ECDSA signature [RFC5480] on the secp256k1 curve.
* I.e. a DER encoded SEQUENCE { r INTEGER, s INTEGER }
* @param signature
* @return
* @throws IOException
*/
public static byte[] getEncoded(BigInteger[] sigs)
throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream(72);
DERSequenceGenerator seq = new DERSequenceGenerator(bos);
seq.addObject(new ASN1Integer(sigs[0]));
seq.addObject(new ASN1Integer(sigs[1]));
seq.close();
return bos.toByteArray();
}
示例8: decodeToBigIntegerArray
import org.spongycastle.asn1.ASN1Integer; //导入依赖的package包/类
/**
* DER - From byte[] to Big Integer rs
* UAF_ALG_SIGN_SECP256K1_ECDSA_SHA256_DER 0x06
* DER [ITU-X690-2008] encoded ECDSA signature [RFC5480] on the secp256k1 curve.
* I.e. a DER encoded SEQUENCE { r INTEGER, s INTEGER }
* @param signature
* @return
* @throws IOException
*/
public static BigInteger[] decodeToBigIntegerArray(byte[] signature) throws IOException {
ASN1InputStream decoder = new ASN1InputStream(signature);
DLSequence seq = (DLSequence) decoder.readObject();
ASN1Integer r = (ASN1Integer) seq.getObjectAt(0);
ASN1Integer s = (ASN1Integer) seq.getObjectAt(1);
decoder.close();
BigInteger[] ret = new BigInteger[2];
ret[0] = r.getPositiveValue();
ret[1] = s.getPositiveValue();
return ret;
}
示例9: derByteStream
import org.spongycastle.asn1.ASN1Integer; //导入依赖的package包/类
protected ByteArrayOutputStream derByteStream() throws IOException {
// Usually 70-ic_launcher bytes.
ByteArrayOutputStream bos = new ByteArrayOutputStream(72);
DERSequenceGenerator seq = new DERSequenceGenerator(bos);
seq.addObject(new ASN1Integer(r));
seq.addObject(new ASN1Integer(s));
seq.close();
return bos;
}
示例10: signTransaction
import org.spongycastle.asn1.ASN1Integer; //导入依赖的package包/类
public boolean signTransaction(BigInteger privateKey) {
ECPrivateKeyParameters privateKeyParams = new ECPrivateKeyParameters(privateKey, EC_PARAMS);
ECDSASigner signer = new ECDSASigner();
signer.init(true, privateKeyParams);
byte[] hashedTransaction = Bitcoin.hash256(getBytes());
BigInteger[] rawSignature = signer.generateSignature(hashedTransaction);
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream(72);
DERSequenceGenerator derGen = new DERSequenceGenerator(baos);
derGen.addObject(new ASN1Integer(rawSignature[0]));
derGen.addObject(new ASN1Integer(rawSignature[1]));
derGen.close();
byte[] ecdsaSignature = baos.toByteArray();
byte[] publicKey = Bitcoin.generatePublicKey(privateKey);
String signature = Bitcoin.encodeBase58(ecdsaSignature) + " " + Bitcoin.encodeBase58(publicKey);
for (int i = 0; i < creations.size(); i++) {
creations.get(i).setSignature(signature);
}
for (int i = 0; i < inputs.size(); i++) {
inputs.get(i).setSignature(signature);
}
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
示例11: signTransaction
import org.spongycastle.asn1.ASN1Integer; //导入依赖的package包/类
public boolean signTransaction(BigInteger privateKey) {
ECPrivateKeyParameters privateKeyParams = new ECPrivateKeyParameters(privateKey, Bitcoin.EC_PARAMS);
ECDSASigner signer = new ECDSASigner();
signer.init(true, privateKeyParams);
byte[] hashedTransaction = Bitcoin.hash256(getBytes());
BigInteger[] rawSignature = signer.generateSignature(hashedTransaction);
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream(72);
DERSequenceGenerator derGen = new DERSequenceGenerator(baos);
derGen.addObject(new ASN1Integer(rawSignature[0]));
derGen.addObject(new ASN1Integer(rawSignature[1]));
derGen.close();
byte[] ecdsaSignature = baos.toByteArray();
byte[] publicKey = Bitcoin.generatePublicKey(privateKey);
String signature = Bitcoin.encodeBase58(ecdsaSignature) + " " + Bitcoin.encodeBase58(publicKey);
for (Creation creation : creations) {
creation.setSignature(signature);
}
for (Input input : inputs) {
input.setSignature(signature);
}
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
示例12: extractKeyFromASN1
import org.spongycastle.asn1.ASN1Integer; //导入依赖的package包/类
private static ECKey extractKeyFromASN1(byte[] asn1privkey) {
// To understand this code, see the definition of the ASN.1 format for
// EC private keys in the OpenSSL source
// code in ec_asn1.c:
//
// ASN1_SEQUENCE(EC_PRIVATEKEY) = {
// ASN1_SIMPLE(EC_PRIVATEKEY, version, LONG),
// ASN1_SIMPLE(EC_PRIVATEKEY, privateKey, ASN1_OCTET_STRING),
// ASN1_EXP_OPT(EC_PRIVATEKEY, parameters, ECPKPARAMETERS, 0),
// ASN1_EXP_OPT(EC_PRIVATEKEY, publicKey, ASN1_BIT_STRING, 1)
// } ASN1_SEQUENCE_END(EC_PRIVATEKEY)
//
try {
ASN1InputStream decoder = new ASN1InputStream(asn1privkey);
DLSequence seq = (DLSequence) decoder.readObject();
checkArgument(decoder.readObject() == null, "Input contains extra bytes");
decoder.close();
checkArgument(seq.size() == 4, "Input does not appear to be an ASN.1 OpenSSL EC private key");
checkArgument(((ASN1Integer) seq.getObjectAt(0)).getValue().equals(BigInteger.ONE),
"Input is of wrong version");
byte[] privbits = ((ASN1OctetString) seq.getObjectAt(1)).getOctets();
BigInteger privkey = new BigInteger(1, privbits);
ASN1TaggedObject pubkey = (ASN1TaggedObject) seq.getObjectAt(3);
checkArgument(pubkey.getTagNo() == 1, "Input has 'publicKey' with bad tag number");
byte[] pubbits = ((DERBitString) pubkey.getObject()).getBytes();
checkArgument(pubbits.length == 33 || pubbits.length == 65, "Input has 'publicKey' with invalid length");
int encoding = pubbits[0] & 0xFF;
// Only allow compressed(2,3) and uncompressed(4), not infinity(0)
// or hybrid(6,7)
checkArgument(encoding >= 2 && encoding <= 4, "Input has 'publicKey' with invalid encoding");
// Now sanity check to ensure the pubkey bytes match the privkey.
boolean compressed = (pubbits.length == 33);
ECKey key = new ECKey(privkey, null, compressed);
if (!Arrays.equals(key.getPubKey(), pubbits))
throw new IllegalArgumentException("Public key in ASN.1 structure does not match private key.");
return key;
} catch (IOException e) {
throw new RuntimeException(e); // Cannot happen, reading from memory
// stream.
}
}