📄 jsoftlib.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: JSoftLib.java
package jit.jcrypto.soft;
import jit.asn1.*;
import jit.asn1.pkcs.PKCSObjectIdentifiers;
import jit.asn1.x509.AlgorithmIdentifier;
import jit.asn1.x9.*;
import jit.asn1parser.Parser;
import jit.crypto.AsymmetricCipherKeyPair;
import jit.crypto.CipherParameters;
import jit.crypto.agreement.ECDHBasicAgreement;
import jit.crypto.digests.SHA1Digest;
import jit.crypto.engines.IESEngine;
import jit.crypto.generators.ECKeyPairGenerator;
import jit.crypto.generators.KDF2BytesGenerator;
import jit.crypto.macs.HMac;
import jit.crypto.params.*;
import jit.cryptolib.toolkit.Crypto;
import jit.jcrypto.*;
import jit.jcrypto.param.*;
import jit.math.BigInteger;
import jit.math.ec.*;
import jit.security.SecureRandom;
// Referenced classes of package jit.jcrypto.soft:
// JMechanism
public class JSoftLib extends Session
{
private static final int SUPPORT_TYPE_LIST[] = {
0, 1, 4, 5, 6, 256, 257, 258, 288, 289,
290, 305, 306, 307, 512, 513, 528, 529, 544, 545,
800, 801, 802, 832, 833, 834, 848, 849, 850, 0x80000002,
0x80000003, 0x80000004, 0x80000010, 0x80000011, 0x80000012, 0x80000013, 0x80000014, 0x80000015, 0x80000020, 848,
849, 850, 1025, 1026, 1027
};
public JSoftLib(int _dID, int _sID)
{
super(_dID, _sID);
}
public boolean login(byte password[])
{
return true;
}
public boolean logout()
{
return true;
}
public boolean setPIN(byte oldPwd[], byte newPwd[])
{
return true;
}
public byte[] digest(Mechanism mechanism, byte sourceData[])
throws Exception
{
int mType = mechanism.getMechanismType();
byte digest[] = null;
switch(mType)
{
case 512:
digest = Crypto.messageDigest(3, sourceData);
break;
case 528:
digest = Crypto.messageDigest(2, sourceData);
break;
case 544:
digest = Crypto.messageDigest(1, sourceData);
break;
default:
throw new Exception(String.valueOf(String.valueOf((new StringBuffer("JSoftLib: can't support MechanismType:")).append(mType).append(" in messageDigest mod."))));
}
return digest;
}
public byte[] generateMac(Mechanism mechanism, JKey key, byte sourceData[])
throws Exception
{
CipherParameters macKey = Parser.conver2CipherParam(key);
byte mac[] = null;
int mType = mechanism.getMechanismType();
switch(mType)
{
case 513:
mac = Crypto.generateHMAC(40, macKey, sourceData);
break;
case 529:
mac = Crypto.generateHMAC(5, macKey, sourceData);
break;
case 545:
mac = Crypto.generateHMAC(4, macKey, sourceData);
break;
default:
throw new Exception(String.valueOf(String.valueOf((new StringBuffer("JSoftLib: can't support MechanismType:")).append(mType).append(" in mac mod."))));
}
return mac;
}
public boolean verifyMac(Mechanism mechanism, JKey key, byte sourceData[], byte macData[])
{
byte mac[] = null;
try
{
CipherParameters macKey = Parser.conver2CipherParam(key);
int mType = mechanism.getMechanismType();
switch(mType)
{
case 513:
mac = Crypto.generateHMAC(40, macKey, sourceData);
break;
case 529:
mac = Crypto.generateHMAC(5, macKey, sourceData);
break;
case 545:
mac = Crypto.generateHMAC(4, macKey, sourceData);
break;
default:
boolean flag1 = false;
return flag1;
}
}
catch(Exception ex)
{
boolean flag = false;
return flag;
}
return isEqualArray(mac, macData);
}
public byte[] sign(Mechanism mechanism, JKey key, byte sourceData[])
throws Exception
{
CipherParameters signerKey = Parser.conver2CipherParam(key);
int mType = mechanism.getMechanismType();
byte hash[] = null;
switch(mType)
{
case 4: // '\004'
hash = Crypto.messageDigest(3, sourceData);
break;
case 5: // '\005'
hash = Crypto.messageDigest(2, sourceData);
break;
case 6: // '\006'
hash = Crypto.messageDigest(1, sourceData);
break;
case 1027:
hash = Crypto.messageDigest(1, sourceData);
return Crypto.ECDSASignature(14, signerKey, hash);
default:
throw new Exception(String.valueOf(String.valueOf((new StringBuffer("JSoftLib: can't support MechanismType:")).append(mType).append(" in sign Mod."))));
}
byte signedData[] = Crypto.RSAEncode(12, true, signerKey, hash);
return signedData;
}
public boolean verifySign(Mechanism mechanism, JKey key, byte sourceData[], byte signData[])
{
byte hash[] = null;
byte decryptedData[] = null;
try
{
CipherParameters signerPubKey = Parser.conver2CipherParam(key);
int mType = mechanism.getMechanismType();
if(mType != 1027)
decryptedData = Crypto.RSAEncode(12, false, signerPubKey, signData);
switch(mType)
{
case 4: // '\004'
hash = Crypto.messageDigest(3, sourceData);
break;
case 5: // '\005'
hash = Crypto.messageDigest(2, sourceData);
break;
case 6: // '\006'
hash = Crypto.messageDigest(1, sourceData);
break;
case 1027:
hash = Crypto.messageDigest(1, sourceData);
boolean flag1 = Crypto.ECDSAVerify(14, signerPubKey, hash, signData);
return flag1;
default:
boolean flag2 = false;
return flag2;
}
}
catch(Exception ex)
{
boolean flag = false;
return flag;
}
return isEqualArray(hash, decryptedData);
}
public byte[] encrypt(Mechanism mechanism, JKey key, byte sourceData[])
throws Exception
{
byte encryptedData[] = doCipher(mechanism, key, sourceData, true);
return encryptedData;
}
public byte[] decrypt(Mechanism mechanism, JKey key, byte encryptedData[])
throws Exception
{
return doCipher(mechanism, key, encryptedData, false);
}
public JKey generateKey(Mechanism mechanism, int keyLength)
throws Exception
{
int eng = 0;
int mType = mechanism.getMechanismType();
switch(mType)
{
case 288:
eng = 6;
break;
case 305:
eng = 8;
break;
case 256:
eng = 10;
break;
case 848:
eng = 22;
break;
case 800:
eng = 28;
break;
case 832:
eng = 30;
break;
default:
throw new Exception(String.valueOf(String.valueOf((new StringBuffer("JSoftLib: can't support MechanismType:")).append(mType).append(" in generateKey mod."))));
}
CipherParameters key = Crypto.generateSymmetricKey(eng, keyLength, false);
return Parser.convert2JKey(145, key);
}
public JKey generatePBEKey(Mechanism mechanism, byte password[])
throws Exception
{
int mType = mechanism.getMechanismType();
if(mType != 0x80000003 && mType != 0x80000002 && mType != 0x80000004)
{
throw new Exception(String.valueOf(String.valueOf((new StringBuffer("JSoftLib: can't support MechanismType:")).append(mType).append(" in PBE key generate mod."))));
} else
{
KeyParameter pwd = new KeyParameter(password);
return Parser.convert2JKey(145, pwd);
}
}
public JKey[] generateKeyPair(Mechanism mechanism)
throws Exception
{
AsymmetricCipherKeyPair keyPair = null;
JKey keys[] = new JKey[2];
if(mechanism.getMechanismType() == 0)
{
RSAParam rsaParam = (RSAParam)mechanism.getParam();
if(rsaParam == null)
{
throw new Exception("JSoftLib: the MechanismParam must not be null in RSA keyPair generate mod.");
} else
{
int length = rsaParam.getKeyLength();
BigInteger pubE = new BigInteger(Integer.toString(rsaParam.getPubE()));
keyPair = Crypto.generateRSAKeyPair(pubE, null, length, 0);
keys[0] = Parser.convert2JKey(1, keyPair.getPublic());
keys[1] = Parser.convert2JKey(2, keyPair.getPrivate());
return keys;
}
}
if(mechanism.getMechanismType() == 1025)
{
ECCParam eccParam = (ECCParam)mechanism.getParam();
if(eccParam == null)
{
throw new Exception("JSoftLib: the MechanismParam must not be null in ECC keyPair generate mod.");
} else
{
int curveID = eccParam.getCurveID();
keyPair = generateECCKeyPair(curveID);
keys[0] = Parser.convert2JKey(1001, keyPair.getPublic());
keys[1] = Parser.convert2JKey(1002, keyPair.getPrivate());
return keys;
}
} else
{
throw new Exception(String.valueOf(String.valueOf((new StringBuffer("JSoftLib: can't support MechanismType:")).append(mechanism.getMechanismType()).append(" in key pair generate mod."))));
}
}
public byte[] generateRandom(Mechanism mechanism, int length)
throws Exception
{
if(mechanism.getMechanismType() != 0x80000020)
{
throw new Exception(String.valueOf(String.valueOf((new StringBuffer("the MechanismType:")).append(mechanism.getMechanismType()).append(" used to generate Random is not suitable."))));
} else
{
SecureRandom sRandom = new SecureRandom();
byte data[] = new byte[length];
sRandom.nextBytes(data);
return data;
}
}
private byte[] doCipher(Mechanism mechanism, JKey jKey, byte source_data[], boolean isEncryption)
throws Exception
{
int mType = mechanism.getMechanismType();
CipherParameters key = Parser.conver2CipherParam(jKey);
CBCParam cbcParam = null;
PBEParam pbeParam = null;
RSAParam rsaParam = null;
ParametersWithIV ivKey = null;
byte iv[] = null;
int pbeEng = -1;
int iterations = -1;
byte salt[] = null;
byte password[] = null;
switch(mType)
{
case 289:
return Crypto.cipherEncode(6, isEncryption, key, source_data);
case 290:
cbcParam = (CBCParam)mechanism.getParam();
if(cbcParam == null)
{
throw new Exception("JSoftLib: the MechanismParam must not be null in CBC mod.");
} else
{
iv = cbcParam.getIV();
ivKey = new ParametersWithIV(key, iv);
return Crypto.cipherEncode(7, isEncryption, ivKey, source_data);
}
case 257:
return Crypto.cipherEncode(10, isEncryption, key, source_data);
case 258:
cbcParam = (CBCParam)mechanism.getParam();
if(cbcParam == null)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -