📄 parser.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: Parser.java
package jit.asn1parser;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import jit.asn1.*;
import jit.asn1.pkcs.PKCSObjectIdentifiers;
import jit.asn1.pkcs.RSAPrivateKeyStructure;
import jit.asn1.pkcs.pkcs7.ContentInfo;
import jit.asn1.x509.*;
import jit.asn1.x9.X9ECParameters;
import jit.asn1.x9.X9PrivateKeyInfo;
import jit.crypto.CipherParameters;
import jit.crypto.params.*;
import jit.jcrypto.JKey;
import jit.math.BigInteger;
import jit.math.ec.ECCurve;
import jit.math.ec.ECPoint;
public class Parser
{
public Parser()
{
}
public static ASN1Sequence oct2Seq(ASN1OctetString oct)
throws Exception
{
byte b[] = oct.getOctets();
ByteArrayInputStream bis = new ByteArrayInputStream(b);
ASN1InputStream ais = new ASN1InputStream(bis);
return (ASN1Sequence)ais.readObject();
}
public static byte[] writeDERObj2Bytes(DERObject obj)
throws Exception
{
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DEROutputStream dos = new DEROutputStream(bos);
dos.writeObject(obj);
return bos.toByteArray();
}
public static DERObject writeBytes2DERObj(byte data[])
throws Exception
{
ByteArrayInputStream bis = new ByteArrayInputStream(data);
ASN1InputStream dis = new ASN1InputStream(bis);
return dis.readObject();
}
public static byte[] convertBase64(byte data[])
{
ByteArrayInputStream bis = new ByteArrayInputStream(data);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
do
{
byte tmp;
if((tmp = (byte)bis.read()) == -1)
break;
if(tmp != 10 && tmp != 13)
bos.write(tmp);
} while(true);
return bos.toByteArray();
}
public static int getLength(byte num[], byte skipByte)
throws Exception
{
int i = 0;
int len = num.length;
for(i = 0; i < len && num[i] == skipByte; i++);
if(i == len)
throw new Exception("Convert Number Failed");
int length = 0;
byte numLen[] = new byte[len - i];
System.arraycopy(num, i, numLen, 0, len - i);
try
{
length = Integer.parseInt(new String(numLen));
}
catch(Exception e)
{
throw new Exception("Convert Number Failed");
}
finally
{
return length;
}
}
public static byte[] writeLength(int number, int byteslen, byte fill)
{
byte res[] = new byte[byteslen];
byte val[] = (new Integer(number)).toString().getBytes();
int i;
for(i = 0; i < byteslen - val.length; i++)
res[i] = fill;
System.arraycopy(val, 0, res, i, val.length);
return res;
}
public static byte[] DEREncode(byte data[])
throws Exception
{
DEROctetString derO = new DEROctetString(data);
return writeDERObj2Bytes(derO);
}
public static byte[] DERDecode(byte data[])
throws Exception
{
ASN1OctetString octet = (ASN1OctetString)writeBytes2DERObj(data);
return octet.getOctets();
}
public static CipherParameters conver2CipherParam(JKey key)
throws Exception
{
int keyType = key.getKeyType();
byte data[] = key.getKey();
switch(keyType)
{
case 145:
return new KeyParameter(data);
case 1: // '\001'
ASN1Sequence pubKeySeq = (ASN1Sequence)writeBytes2DERObj(data);
SubjectPublicKeyInfo spki = new SubjectPublicKeyInfo(pubKeySeq);
ASN1Sequence seq = (ASN1Sequence)spki.getPublicKey();
RSAPublicKeyStructure keyStruc = new RSAPublicKeyStructure(seq);
return new RSAKeyParameters(false, keyStruc.getModulus(), keyStruc.getPublicExponent());
case 2: // '\002'
ASN1Sequence prvKeySeq = (ASN1Sequence)writeBytes2DERObj(data);
RSAPrivateKeyStructure stru = new RSAPrivateKeyStructure(prvKeySeq);
RSAPrivateCrtKeyParameters prvKey = new RSAPrivateCrtKeyParameters(stru.getModulus(), stru.getPublicExponent(), stru.getPrivateExponent(), stru.getPrime1(), stru.getPrime2(), stru.getExponent1(), stru.getExponent2(), stru.getCoefficient());
return prvKey;
case 1001:
ASN1Sequence eccPubKeySeq = (ASN1Sequence)writeBytes2DERObj(data);
SubjectPublicKeyInfo eccSpki = new SubjectPublicKeyInfo(eccPubKeySeq);
SubjectPublicKeyInfo spki = SubjectPublicKeyInfo.getInstance(eccSpki.getPublicKey());
eccPubKeySeq = (ASN1Sequence)spki.getAlgorithmId().getParameters();
X9ECParameters x9params = new X9ECParameters(eccPubKeySeq);
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);
case 1002:
ASN1Sequence eccPrvKeySeq = (ASN1Sequence)writeBytes2DERObj(data);
X9PrivateKeyInfo x9Pki = new X9PrivateKeyInfo(eccPrvKeySeq);
return x9Pki.getPrivateKey();
}
throw new Exception(String.valueOf(String.valueOf((new StringBuffer("not support KeyType:")).append(keyType).append(" int JSoftLib."))));
}
public static JKey convert2JKey(int keyType, CipherParameters cipherParam)
throws Exception
{
byte data[] = null;
switch(keyType)
{
case 145:
if(cipherParam instanceof KeyParameter)
{
KeyParameter key = (KeyParameter)cipherParam;
byte sourceKey[] = key.getKey();
data = sourceKey;
} else
{
throw new Exception("the CipherParameter can not be conveted to CKC_KEYTYPE_SYMMETRY type.");
}
break;
case 1: // '\001'
if(!(cipherParam instanceof RSAKeyParameters))
throw new Exception("the CipherParameter can not be convered to CKC_KEYTYPE_RSA_PUBLICKEY type.");
RSAKeyParameters pubKey = (RSAKeyParameters)cipherParam;
AlgorithmIdentifier rsaPubAlg = new AlgorithmIdentifier(PKCSObjectIdentifiers.rsaEncryption, null);
RSAPublicKeyStructure keyStruc = new RSAPublicKeyStructure(pubKey.getModulus(), pubKey.getExponent());
SubjectPublicKeyInfo spki = new SubjectPublicKeyInfo(rsaPubAlg, keyStruc);
data = writeDERObj2Bytes(spki.getDERObject());
break;
case 2: // '\002'
if(!(cipherParam instanceof RSAPrivateCrtKeyParameters))
throw new Exception("the CipherParameter can not be convered to CKC_KEYTYPE_RSA_PRIVATEKEY type.");
RSAPrivateCrtKeyParameters prvKey = (RSAPrivateCrtKeyParameters)cipherParam;
RSAPrivateKeyStructure stru = new RSAPrivateKeyStructure(prvKey.getModulus(), prvKey.getPublicExponent(), prvKey.getExponent(), prvKey.getP(), prvKey.getQ(), prvKey.getDP(), prvKey.getDQ(), prvKey.getQInv());
data = writeDERObj2Bytes(stru.getDERObject());
break;
case 1001:
if(!(cipherParam instanceof ECPublicKeyParameters))
throw new Exception("the CipherParameter can not be convered to CKC_KEYTYPE_ECC_PUBLIC_KEY type.");
ECPublicKeyParameters ecPubKey = (ECPublicKeyParameters)cipherParam;
ECDomainParameters ecdp = ecPubKey.getParameters();
X9ECParameters x9params = new X9ECParameters(ecdp.getCurve(), ecdp.getG(), ecdp.getN(), ecdp.getH());
AlgorithmIdentifier eccAlg = new AlgorithmIdentifier(PKCSObjectIdentifiers.ecEncryption, x9params);
SubjectPublicKeyInfo eccSpki = new SubjectPublicKeyInfo(eccAlg, ecPubKey.getQ().getEncoded(true));
AlgorithmIdentifier alg = new AlgorithmIdentifier(PKCSObjectIdentifiers.ecEncryption, null);
eccSpki = new SubjectPublicKeyInfo(alg, eccSpki);
data = writeDERObj2Bytes(eccSpki.getDERObject());
break;
case 1002:
if(!(cipherParam instanceof ECPrivateKeyParameters))
throw new Exception("the CipherParameter can not be convered to CKC_KEYTYPE_ECC_PRIVATE_KEY type.");
ECPrivateKeyParameters ecPriKey = (ECPrivateKeyParameters)cipherParam;
DEROctetString d = new DEROctetString(ecPriKey.getD().toByteArray());
ECDomainParameters prvEcdp = ecPriKey.getParameters();
X9ECParameters x9Params = new X9ECParameters(prvEcdp.getCurve(), prvEcdp.getG(), prvEcdp.getN(), prvEcdp.getH());
X9PrivateKeyInfo eccPrvKeyInfo = new X9PrivateKeyInfo(d, x9Params);
data = writeDERObj2Bytes(eccPrvKeyInfo.getDERObject());
break;
default:
throw new Exception(String.valueOf(String.valueOf((new StringBuffer("not support KeyType:")).append(keyType).append(" in JSoftLib."))));
}
return new JKey(keyType, data);
}
public static DERObjectIdentifier getOIDFromContentInfo(byte contentInfo[])
throws Exception
{
ContentInfo content = ContentInfo.getInstance(writeBytes2DERObj(contentInfo));
return content.getContentType();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -