📄 custmxcipherx.java
字号:
package psdi.webclient.beans.signature;
import java.lang.reflect.Constructor;
import java.math.BigInteger;
import java.rmi.RemoteException;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.Provider;
import java.security.PublicKey;
import java.security.Security;
import java.security.spec.RSAPrivateKeySpec;
import java.security.spec.RSAPublicKeySpec;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import psdi.server.MXServerRemote;
import psdi.util.MXException;
import psdi.util.MXSystemException;
public class CustMXCipherX {
public CustMXCipherX(MXServerRemote mxserverremote)throws MXException, RemoteException {
algorithm = "DESede";
mode = "CBC";
padding = "PKCS5Padding";
key = "j3*9vk0e8rjvc9fj(*KFikd#";
spec = "kE*(RKc%";
modulus = "";
cipherEncrypt = null;
transformation = null;
secretKey = null;
ivSpec = null;
pbeParamSpec = null;
secretkeySpec = null;
publicKey = null;
privateKey = null;
nonSunProviders = false;
providerClass = null;
padLen = 8;
String s = mxserverremote.getConfig().getProperty(
"mxe.security.cryptox.algorithm");
String s1 = mxserverremote.getConfig().getProperty(
"mxe.security.cryptox.mode");
String s2 = mxserverremote.getConfig().getProperty(
"mxe.security.cryptox.padding");
String s3 = mxserverremote.getConfig().getProperty(
"mxe.security.cryptox.key");
String s4 = mxserverremote.getConfig().getProperty(
"mxe.security.cryptox.spec");
String s5 = mxserverremote.getConfig().getProperty(
"mxe.security.cryptox.modulus");
String s6 = mxserverremote.getConfig().getProperty(
"mxe.security.provider");
init(s, s1, s2, s3, s4, s5, s6);
}
protected void init(String s, String s1, String s2, String s3, String s4,
String s5, String s6) throws MXException, RemoteException {
try {
if (s6 != null && !s6.equals("")) {
Class class1 = Class.forName(s6);
Class aclass[] = new Class[0];
Constructor constructor = class1.getConstructor(aclass);
Object aobj[] = new Object[0];
providerClass = (Provider) constructor.newInstance(aobj);
Security.addProvider(providerClass);
}
} catch (Exception exception) {
exception.printStackTrace();
throw new MXSystemException("system", "major", exception);
}
Provider aprovider[] = Security.getProviders();
for (int i = 0; i < aprovider.length; i++) {
if (aprovider[i].getName().toUpperCase().startsWith("SUN"))
;
nonSunProviders = true;
}
validateParams(s, s1, s2, s3, s4, s5);
transformation = algorithm;
if (mode != null && !mode.equals("") && padding != null
&& !padding.equals(""))
transformation = transformation + "/" + mode + "/" + padding;
try {
cipherEncrypt = buildCipher(false);
} catch (Exception exception1) {
exception1.printStackTrace();
throw new MXSystemException("system", "major", exception1);
}
}
private void validateParams(String s, String s1, String s2, String s3,
String s4, String s5) throws MXException, RemoteException {
if (s != null && !s.equals(""))
algorithm = s;
if (s1 != null && !s1.equals(""))
mode = s1;
if (s2 != null && !s2.equals(""))
padding = s2;
if (s3 != null && !s3.equals(""))
key = s3;
if (s4 != null && !s4.equals(""))
spec = s4;
if (s5 != null && !s5.equals(""))
modulus = s5;
if (algorithm == null)
throw new MXSystemException("system", "major", new Exception(
"mxe.security.algorithm"));
if (algorithm.equals("AES") || algorithm.equals("Serpent")
|| algorithm.equals("MARS") || algorithm.equals("RC6")
|| algorithm.equals("Rijndael") || algorithm.equals("Square")
|| algorithm.equals("Twofish"))
padLen = 16;
else if (algorithm.equals("RSA"))
padLen = 0;
if (!algorithm.equals("DES") && !algorithm.equals("DESede")
&& !algorithm.equals("AES")) {
if (s1 == null || s1.equals(""))
mode = "";
if (s2 == null || s2.equals(""))
padding = "";
}
if (nonSunProviders)
return;
if (!algorithm.equals("DESede") && !algorithm.equals("DES")
&& !algorithm.equals("AES")
&& !algorithm.equals("PBEWithMD5AndDES"))
throw new MXSystemException("system", "major", new Exception(
"mxe.security.algorithm"));
if (algorithm.equals("AES") && (mode == null || !mode.equals("ECB")))
throw new MXSystemException("system", "major", new Exception(
"mxe.security.mode"));
if (algorithm.equals("PBEWithMD5AndDES")) {
if (mode == null || !mode.equals("CBC"))
throw new MXSystemException("system", "major", new Exception(
"mxe.security.mode"));
if (padding == null || !padding.equals("PKCS5Padding"))
throw new MXSystemException("system", "major", new Exception(
"mxe.security.padding"));
if (key.getBytes().length != 8)
throw new MXSystemException("system", "major", new Exception(
"mxe.security.key"));
}
if (mode != null && !mode.equals("") && !mode.equals("CBC")
&& !mode.equals("CFB") && !mode.equals("ECB")
&& !mode.equals("OFB") && !mode.equals("PCBC"))
throw new MXSystemException("system", "major", new Exception(
"mxe.security.mode"));
if (padding != null && !padding.equals("")
&& !padding.equals("NoPadding")
&& !padding.equals("PKCS5Padding"))
throw new MXSystemException("system", "major", new Exception(
"mxe.security.padding"));
if (key != null && !key.equals("") && !algorithm.equals("RSA")
&& key.length() % 24 != 0)
throw new MXSystemException("system", "major", new Exception(
"mxe.security.key"));
if (spec != null && !spec.equals("") && !algorithm.equals("RSA")
&& spec.length() % 8 != 0)
throw new MXSystemException("system", "major", new Exception(
"mxe.security.spec"));
if (mode != null && mode.equals("OFB") && padding != null
&& !padding.equals("NoPadding")
&& (algorithm == null || !algorithm.equals("DES")))
throw new MXSystemException("system", "major", new Exception(
"mxe.security.padding"));
else
return;
}
Cipher buildCipher(boolean flag) throws Exception {
Cipher cipher = null;
byte byte0 = ((byte) (!flag ? 2 : 1));
if (algorithm.equals("DESede") || algorithm.equals("TripleDES")) {
if (secretKey == null || ivSpec == null) {
DESedeKeySpec desedekeyspec = new DESedeKeySpec(key.getBytes());
SecretKeyFactory secretkeyfactory = SecretKeyFactory
.getInstance(algorithm);
secretKey = secretkeyfactory.generateSecret(desedekeyspec);
ivSpec = new IvParameterSpec(spec.getBytes());
}
if (providerClass == null)
cipher = Cipher.getInstance(transformation);
else
cipher = Cipher.getInstance(transformation, providerClass);
if (transformation.indexOf("ECB") < 0)
cipher.init(byte0, secretKey, ivSpec);
else
cipher.init(byte0, secretKey);
} else if (algorithm.equals("DES")) {
if (secretKey == null || ivSpec == null) {
DESKeySpec deskeyspec = new DESKeySpec(key.getBytes());
SecretKeyFactory secretkeyfactory1 = SecretKeyFactory
.getInstance(algorithm);
secretKey = secretkeyfactory1.generateSecret(deskeyspec);
ivSpec = new IvParameterSpec(spec.getBytes());
}
if (providerClass == null)
cipher = Cipher.getInstance(transformation);
else
cipher = Cipher.getInstance(transformation, providerClass);
if (transformation.indexOf("ECB") < 0)
cipher.init(byte0, secretKey, ivSpec);
else
cipher.init(byte0, secretKey);
} else if (algorithm.startsWith("PBEWith")) {
if (secretKey == null || pbeParamSpec == null) {
pbeParamSpec = new PBEParameterSpec(spec.getBytes(), 20);
PBEKeySpec pbekeyspec = new PBEKeySpec(spec.toCharArray());
SecretKeyFactory secretkeyfactory2 = SecretKeyFactory
.getInstance(algorithm);
secretKey = secretkeyfactory2.generateSecret(pbekeyspec);
}
if (providerClass == null)
cipher = Cipher.getInstance(transformation);
else
cipher = Cipher.getInstance(transformation, providerClass);
cipher.init(byte0, secretKey, pbeParamSpec);
} else if (algorithm.equals("RSA")) {
if (publicKey == null || privateKey == null) {
KeyFactory keyfactory = KeyFactory.getInstance("RSA",
providerClass);
publicKey = keyfactory.generatePublic(new RSAPublicKeySpec(
new BigInteger(modulus), new BigInteger(key)));
privateKey = keyfactory.generatePrivate(new RSAPrivateKeySpec(
new BigInteger(modulus), new BigInteger(spec)));
}
if (providerClass == null)
cipher = Cipher.getInstance(transformation);
else
cipher = Cipher.getInstance(transformation, providerClass);
if (flag)
cipher.init(byte0, publicKey);
else
cipher.init(byte0, privateKey);
} else {
if (secretkeySpec == null) {
byte byte1 = ((byte) (algorithm.equals("SKIPJACK") ? 10 : 16));
byte abyte0[] = spec.getBytes();
abyte0 = pad(abyte0, byte1);
secretkeySpec = new SecretKeySpec(abyte0, algorithm);
}
if (providerClass == null)
cipher = Cipher.getInstance(transformation);
else
cipher = Cipher.getInstance(transformation, providerClass);
cipher.init(byte0, secretkeySpec);
}
return cipher;
}
protected byte[] pad(byte abyte0[]) {
return pad(abyte0, padLen);
}
protected byte[] pad(byte abyte0[], int i) {
if (i == 0)
return abyte0;
int j = abyte0.length;
int k = j;
int l = j % i;
if (l > 0)
k = j + (i - l);
byte abyte1[] = new byte[k];
for (int i1 = 0; i1 < j; i1++)
abyte1[i1] = abyte0[i1];
return abyte1;
}
public synchronized byte[] decData(byte[] pwd) throws MXException {
try {
return cipherEncrypt.doFinal(pwd);
} catch (Exception exception) {
exception.printStackTrace();
throw new MXSystemException("system", "major", exception);
}
}
String algorithm;
String mode;
String padding;
String key;
String spec;
String modulus;
private Cipher cipherEncrypt;
String transformation;
SecretKey secretKey;
IvParameterSpec ivSpec;
PBEParameterSpec pbeParamSpec;
SecretKeySpec secretkeySpec;
PublicKey publicKey;
PrivateKey privateKey;
boolean nonSunProviders;
Provider providerClass;
int padLen;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -