📄 privatekeyenvelop.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: PrivateKeyEnvelop.java
package jit.util.jitca;
import java.io.*;
import jit.asn1.*;
import jit.asn1.pkcs.RSAPrivateKeyStructure;
import jit.asn1.x9.X9ECParameters;
import jit.asn1.x9.X9PrivateKeyInfo;
import jit.asn1parser.Parser;
import jit.crypto.CipherParameters;
import jit.crypto.params.*;
import jit.jcrypto.*;
import jit.jcrypto.soft.JMechanism;
import jit.math.BigInteger;
import jit.util.encoders.Base64;
// Referenced classes of package jit.util.jitca:
// Key
public class PrivateKeyEnvelop
{
private int deviceId;
private int cryptoType;
private int algId;
private byte encryptedCipherKey[];
private byte encryptedPrvKey[];
private CipherParameters encryptPubKey;
private byte b_deviceId[];
private byte b_cryptoType[];
private byte b_algId[];
private byte b_prvKey[];
public static final int RC2_ALG = 257;
public static final int DES_ALG = 289;
public static final int DES3_ALG = 306;
private static final int LEN = 16;
private int privateKeyType;
private Session session;
public PrivateKeyEnvelop(Session session)
{
this.session = null;
this.session = session;
deviceId = 0;
cryptoType = 0;
algId = 0;
encryptedCipherKey = null;
encryptedPrvKey = null;
encryptPubKey = null;
b_deviceId = null;
b_cryptoType = null;
b_algId = null;
}
public void loadEnvelop(byte envelop[])
throws Exception
{
ByteArrayInputStream bis = new ByteArrayInputStream(envelop);
loadEnvelop(((InputStream) (bis)));
}
public void loadEnvelop(InputStream ins)
throws Exception
{
deviceId = getValue(ins);
cryptoType = getValue(ins);
int keyLength = 0;
int res = 0;
if(cryptoType != 0)
{
algId = getValue(ins);
keyLength = getValue(ins);
encryptedCipherKey = new byte[keyLength];
res = 0;
res = ins.read(encryptedCipherKey);
if(res != keyLength)
throw new Exception("CipherKey has been crabed.");
}
keyLength = getValue(ins);
encryptedPrvKey = new byte[keyLength];
res = ins.read(encryptedPrvKey);
if(res != keyLength)
throw new Exception("RSAKey has been crabed.");
else
return;
}
public int getDeviceID()
{
return deviceId;
}
public int getCryptoType()
{
return cryptoType;
}
public int getAlgID()
{
return algId;
}
public JKey getPrivateKey(JKey cipherKey)
throws Exception
{
DERInputStream dis = null;
ByteArrayInputStream bis = null;
ASN1Sequence seq = null;
Key k = null;
byte de_data[] = null;
int keyType = -1;
if(cryptoType != 0)
{
if(cipherKey == null)
throw new Exception("Decrypte Key should not be Null.");
encryptedCipherKey = Parser.convertBase64(encryptedCipherKey);
encryptedCipherKey = Base64.decode(encryptedCipherKey);
bis = new ByteArrayInputStream(encryptedCipherKey);
dis = new DERInputStream(bis);
seq = (ASN1Sequence)dis.readObject();
k = Key.getInstance(seq);
encryptedCipherKey = k.getKey().getOctets();
byte key[] = null;
Mechanism cryptoM = null;
if(cipherKey.getKeyType() == 2)
cryptoM = new JMechanism(1);
else
if(cipherKey.getKeyType() == 1002)
cryptoM = new JMechanism(1026);
else
throw new Exception("the cipher key is neither RSA nor ECC type.");
key = session.decrypt(cryptoM, cipherKey, encryptedCipherKey);
encryptedPrvKey = Parser.convertBase64(encryptedPrvKey);
encryptedPrvKey = Base64.decode(encryptedPrvKey);
bis = new ByteArrayInputStream(encryptedPrvKey);
dis = new DERInputStream(bis);
seq = (ASN1Sequence)dis.readObject();
k = Key.getInstance(seq);
encryptedPrvKey = k.getKey().getOctets();
keyType = k.getKeyType().getValue().intValue();
JKey jkey = new JKey(145, key);
switch(algId)
{
case 257:
cryptoM = new JMechanism(257);
break;
case 289:
cryptoM = new JMechanism(289);
break;
case 306:
cryptoM = new JMechanism(306);
break;
default:
throw new Exception("Not Support Algorithm");
}
de_data = session.decrypt(cryptoM, jkey, encryptedPrvKey);
} else
{
encryptedPrvKey = Parser.convertBase64(encryptedPrvKey);
encryptedPrvKey = Base64.decode(encryptedPrvKey);
bis = new ByteArrayInputStream(encryptedPrvKey);
dis = new DERInputStream(bis);
seq = (ASN1Sequence)dis.readObject();
k = Key.getInstance(seq);
de_data = k.getKey().getOctets();
}
bis = new ByteArrayInputStream(de_data);
dis = new DERInputStream(bis);
seq = (ASN1Sequence)dis.readObject();
if(keyType == 2)
{
RSAPrivateKeyStructure stru = new RSAPrivateKeyStructure(seq);
RSAPrivateCrtKeyParameters privateKey = new RSAPrivateCrtKeyParameters(stru.getModulus(), stru.getPublicExponent(), stru.getPrivateExponent(), stru.getPrime1(), stru.getPrime2(), stru.getExponent1(), stru.getExponent2(), stru.getCoefficient());
return Parser.convert2JKey(2, privateKey);
} else
{
X9PrivateKeyInfo pki = new X9PrivateKeyInfo(seq);
return Parser.convert2JKey(1002, pki.getPrivateKey());
}
}
public void setDeviceId(int did)
{
deviceId = did;
b_deviceId = Parser.writeLength(deviceId, 16, (byte)48);
}
public void setCryptoType(int ct)
{
cryptoType = ct;
b_cryptoType = Parser.writeLength(cryptoType, 16, (byte)48);
}
public void setAlgId(int ai)
throws Exception
{
if(cryptoType == 0)
{
throw new Exception("you can't set algId when set CryptoType = 0.");
} else
{
algId = ai;
b_algId = Parser.writeLength(ai, 16, (byte)48);
return;
}
}
public void setEncryptPublicKey(JKey pubKey)
throws Exception
{
if(cryptoType == 0)
{
throw new Exception("you can't set algId when set CryptoType = 0.");
} else
{
encryptPubKey = Parser.conver2CipherParam(pubKey);
return;
}
}
public void setPrivateKey(JKey priKey)
throws Exception
{
CipherParameters prvKey = Parser.conver2CipherParam(priKey);
if(prvKey instanceof RSAKeyParameters)
{
privateKeyType = 2;
RSAPrivateCrtKeyParameters pk = (RSAPrivateCrtKeyParameters)prvKey;
RSAPrivateKeyStructure stru = new RSAPrivateKeyStructure(pk.getModulus(), pk.getPublicExponent(), pk.getExponent(), pk.getP(), pk.getQ(), pk.getDP(), pk.getDQ(), pk.getQInv());
b_prvKey = Parser.writeDERObj2Bytes(stru.getDERObject());
} else
{
if(!(prvKey instanceof ECKeyParameters))
throw new Exception("the private key is neither RSA nor ECC type.");
privateKeyType = 512;
ECPrivateKeyParameters ecPriKey = (ECPrivateKeyParameters)prvKey;
DEROctetString d = new DEROctetString(ecPriKey.getD().toByteArray());
ECDomainParameters ecdp = ecPriKey.getParameters();
X9ECParameters x9Params = new X9ECParameters(ecdp.getCurve(), ecdp.getG(), ecdp.getN(), ecdp.getH());
X9PrivateKeyInfo eki = new X9PrivateKeyInfo(d, x9Params);
b_prvKey = Parser.writeDERObj2Bytes(eki.getDERObject());
}
}
public byte[] generatePrvKeyEnvelop()
throws Exception
{
if(cryptoType == 0)
{
DEROctetString oct = new DEROctetString(b_prvKey);
Key k = null;
if(privateKeyType == 2)
k = new Key(new DERInteger(2), oct);
else
k = new Key(new DERInteger(512), oct);
b_prvKey = Base64.encode(Parser.writeDERObj2Bytes(k.getDERObject()));
byte len[] = Parser.writeLength(b_prvKey.length, 16, (byte)48);
encryptedPrvKey = new byte[len.length + b_prvKey.length];
System.arraycopy(len, 0, encryptedPrvKey, 0, len.length);
System.arraycopy(b_prvKey, 0, encryptedPrvKey, len.length, b_prvKey.length);
} else
{
Mechanism keyGenM = null;
Mechanism cryptoM = null;
switch(algId)
{
case 257:
keyGenM = new JMechanism(256);
cryptoM = new JMechanism(257);
break;
case 289:
keyGenM = new JMechanism(288);
cryptoM = new JMechanism(289);
break;
case 306:
keyGenM = new JMechanism(305);
cryptoM = new JMechanism(306);
break;
default:
throw new Exception("Not Support Algorithm");
}
JKey jkey = session.generateKey(keyGenM, 0);
byte tmp[] = session.encrypt(cryptoM, jkey, b_prvKey);
DEROctetString oct = new DEROctetString(tmp);
Key k = null;
if(privateKeyType == 2)
k = new Key(new DERInteger(2), oct);
else
k = new Key(new DERInteger(512), oct);
tmp = Base64.encode(Parser.writeDERObj2Bytes(k.getDERObject()));
byte len[] = Parser.writeLength(tmp.length, 16, (byte)48);
encryptedPrvKey = new byte[len.length + tmp.length];
System.arraycopy(len, 0, encryptedPrvKey, 0, len.length);
System.arraycopy(tmp, 0, encryptedPrvKey, len.length, tmp.length);
byte b_param[] = jkey.getKey();
JKey enPubKey = null;
if(encryptPubKey instanceof RSAKeyParameters)
{
cryptoM = new JMechanism(1);
enPubKey = Parser.convert2JKey(1, encryptPubKey);
} else
if(encryptPubKey instanceof ECKeyParameters)
{
cryptoM = new JMechanism(1026);
enPubKey = Parser.convert2JKey(1001, encryptPubKey);
} else
{
throw new Exception("the encryptPubKey tyep is neither RSA nor ECC.");
}
tmp = session.encrypt(cryptoM, enPubKey, b_param);
oct = new DEROctetString(tmp);
k = new Key(new DERInteger(145), oct);
tmp = Base64.encode(Parser.writeDERObj2Bytes(k.getDERObject()));
len = Parser.writeLength(tmp.length, 16, (byte)48);
encryptedCipherKey = new byte[len.length + tmp.length];
System.arraycopy(len, 0, encryptedCipherKey, 0, len.length);
System.arraycopy(tmp, 0, encryptedCipherKey, len.length, tmp.length);
}
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bos.write(b_deviceId);
bos.write(b_cryptoType);
if(cryptoType != 0)
{
bos.write(b_algId);
bos.write(encryptedCipherKey);
}
bos.write(encryptedPrvKey);
return bos.toByteArray();
}
private int getValue(InputStream ins)
throws Exception
{
byte tmp[] = new byte[16];
int res = 0;
res = ins.read(tmp);
if(res != 16)
throw new Exception("length is not enough.");
else
return Parser.getLength(tmp, (byte)48);
}
static
{
RC2_ALG = 257;
DES_ALG = 289;
DES3_ALG = 306;
LEN = 16;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -