📄 spkiparser.java
字号:
// Decompiled by Jad v1.5.7g. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi
// Source File Name: SPKIParser.java
package jit.asn1parser.x509;
import jit.asn1.*;
import jit.asn1.pkcs.PKCSObjectIdentifiers;
import jit.asn1.x509.*;
import jit.asn1.x9.X9ECParameters;
import jit.asn1parser.Parser;
import jit.crypto.CipherParameters;
import jit.crypto.params.*;
import jit.jcrypto.JKey;
import jit.math.ec.ECCurve;
import jit.math.ec.ECPoint;
public class SPKIParser
{
public SPKIParser()
{
}
public SubjectPublicKeyInfo generateSPKI(int keyType, JKey jpubKey)
throws Exception
{
CipherParameters pubKey = Parser.conver2CipherParam(jpubKey);
if(keyType == 1)
if(!(pubKey instanceof RSAKeyParameters))
throw new Exception("wrong type of key");
else
return generateRsaSPKI(pubKey);
if(keyType == 2)
{
if(!(pubKey instanceof ECKeyParameters))
throw new Exception("wrong type of key");
else
return generateEccSPKI(pubKey);
} else
{
throw new Exception("unknown publicKey type");
}
}
private SubjectPublicKeyInfo generateRsaSPKI(CipherParameters pubKey)
{
AlgorithmIdentifier rsaAlg = new AlgorithmIdentifier(PKCSObjectIdentifiers.rsaEncryption, null);
RSAKeyParameters param = (RSAKeyParameters)pubKey;
RSAPublicKeyStructure keyStruc = new RSAPublicKeyStructure(param.getModulus(), param.getExponent());
return new SubjectPublicKeyInfo(rsaAlg, keyStruc);
}
private SubjectPublicKeyInfo generateEccSPKI(CipherParameters pubKey)
{
ECPublicKeyParameters ecPubKey = (ECPublicKeyParameters)pubKey;
ECDomainParameters ecdp = ecPubKey.getParameters();
X9ECParameters x9params = new X9ECParameters(ecdp.getCurve(), ecdp.getG(), ecdp.getN(), ecdp.getH());
AlgorithmIdentifier eccAlg = new AlgorithmIdentifier(PKCSObjectIdentifiers.ecEncryption, x9params);
SubjectPublicKeyInfo spki = new SubjectPublicKeyInfo(eccAlg, ecPubKey.getQ().getEncoded(true));
AlgorithmIdentifier alg = new AlgorithmIdentifier(PKCSObjectIdentifiers.ecEncryption, null);
return new SubjectPublicKeyInfo(alg, spki);
}
public JKey getPublicKey(SubjectPublicKeyInfo spki)
throws Exception
{
DERObjectIdentifier oid = spki.getAlgorithmId().getObjectId();
if(oid.equals(PKCSObjectIdentifiers.rsaEncryption))
return Parser.convert2JKey(1, getRsaPubKey(spki));
if(oid.equals(PKCSObjectIdentifiers.ecEncryption))
return Parser.convert2JKey(1001, getEccPubKey(spki));
else
throw new Exception("wrong keyType: keyType is not Parser.RSAKEY");
}
private CipherParameters getRsaPubKey(SubjectPublicKeyInfo spki)
throws Exception
{
ASN1Sequence seq = (ASN1Sequence)spki.getPublicKey();
RSAPublicKeyStructure keyStruc = new RSAPublicKeyStructure(seq);
return new RSAKeyParameters(false, keyStruc.getModulus(), keyStruc.getPublicExponent());
}
private CipherParameters getEccPubKey(SubjectPublicKeyInfo _spki)
throws Exception
{
SubjectPublicKeyInfo spki = SubjectPublicKeyInfo.getInstance(_spki.getPublicKey());
ASN1Sequence seq = (ASN1Sequence)spki.getAlgorithmId().getParameters();
X9ECParameters x9params = new X9ECParameters(seq);
byte pointData[] = spki.getPublicKeyData().getBytes();
ECPoint point = x9params.getCurve().decodePoint(pointData);
ECDomainParameters ecdp = new ECDomainParameters(x9params.getCurve(), x9params.getG(), x9params.getN(), x9params.getH());
return new ECPublicKeyParameters(point, ecdp);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -