How do you convert a JCE algorithm name into an AlgorithmIdentifier object?

烈酒焚心 提交于 2019-12-11 04:23:23

问题


I'm using BouncyCastle 1.54.

I have a JCE algorithm string - like "ECDSAwithSHA256" (for example).

I need an org.bouncycastle.asn1.x509.AlgorithmIdentifier object.

Alternatively, I could create an AlgorithmIdentifier object from an OID, but that begs the question of how to translate an algorithm string into an OID instead.

I could create a giant if/else, but there's got to be a standard way to do this.


回答1:


You can use the algorithm finders of BouncyCastle (see javadoc)

import org.bouncycastle.operator.DefaultDigestAlgorithmIdentifierFinder;
import org.bouncycastle.operator.DefaultSignatureAlgorithmIdentifierFinder;

AlgorithmIdentifier sigAlgId = new DefaultSignatureAlgorithmIdentifierFinder().find(signatureAlgorithm);
AlgorithmIdentifier digAlgId = new DefaultDigestAlgorithmIdentifierFinder().find(sigAlgId);

The AlgorithmIdentifier OID's obtained for SHA256withECDSA (not ECDSAwithSHA256, see bouncycastle specifications) will be

1.2.840.10045.4.3.2
2.16.840.1.101.3.4.2.1


来源:https://stackoverflow.com/questions/38273270/how-do-you-convert-a-jce-algorithm-name-into-an-algorithmidentifier-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!