怎么把OpenSSL数字签名转换成ASN1? [英] How to convert OpenSSL digital signature to ASN1?

查看:597
本文介绍了怎么把OpenSSL数字签名转换成ASN1?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用openssl库,我创建了文件的数字签名.

Using the openssl library I have created a digital signature of a file.

如果使用openssl命令,我可以看到:

I can see that if I use the openssl command:

openssl rsautl -verify -inkey pubkey.pem -pubin -asn1parse -in sigfile

我得到类似以下内容的好输出:

I get a nice output of something like:

0:d=0  hl=2 l=  49 cons: SEQUENCE          
2:d=1  hl=2 l=  13 cons:  SEQUENCE          
4:d=2  hl=2 l=   9 prim:   OBJECT            :sha256
15:d=2  hl=2 l=   0 prim:   NULL              
17:d=1  hl=2 l=  32 prim:  OCTET STRING      
  0000 - c9 8c 24 b6 77 ef f4 48-60 af ea 6f 49 3b ba ec   ..$.w..H`..oI;..
  0010 - 5b b1 c4 cb b2 09 c6 fc-2b bb 47 f6 6f f2 ad 31   [.......+.G.o..1

如何以编程方式将签名文件转换为可以解析的ASN1?

How can I programmatically convert my signature file into some ASN1 that I can then parse?

推荐答案

在@pedrofb的帮助下,我设法提出了以下解决方案:

With some help from @pedrofb I managed to come up with the following solution:

// Get key from cert
CertificateFactory fact = CertificateFactory.getInstance("X.509", new org.bouncycastle.jce.provider.BouncyCastleProvider());
X509Certificate cer = (X509Certificate) fact.generateCertificate(new FileInputStream("/home/administrator/Downloads/cert_1.txt"));
PublicKey key = cer.getPublicKey();

// or read key in from pem file
PublicKey publicKey = ManifestUtils.publicKeyFromPemFile(new FileReader("/home/administrator/Downloads/publickey.txt"));

// Decrypt the signature
Cipher asymmetricCipher
            = Cipher.getInstance("RSA/ECB/PKCS1Padding", new org.bouncycastle.jce.provider.BouncyCastleProvider());
asymmetricCipher.init(Cipher.DECRYPT_MODE, publicKey);
byte[] plainText = asymmetricCipher.doFinal(
            IOUtils.toByteArray(new FileInputStream("/home/administrator/Downloads/signature.sign")));

// print as hex
System.out.println(Hex.encodeHexString(plainText));

// Print the ans1 nicely - ish
ASN1InputStream input = new ASN1InputStream(plainText);
ASN1Primitive p;
while ((p = input.readObject()) != null) {
    System.out.println(ASN1Dump.dumpAsString(p));
}

这篇关于怎么把OpenSSL数字签名转换成ASN1?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆