从网上找了一个加密程序,在eclipse上可以正常使用。但是程序打包后放到Tomcat上就无法解密了。
而且每次加密后的密文都不一样。
下面是代码:package com.recruit.tools;
import java.io.IOException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;public class Encode {
//加密
public String encode(String ming) {
String encyStr = getTwiceEncString(ming, "147258369");
return encyStr;
}

//解密
public String decode(String mi) {
String decyStr = getTwiceDecString(mi, "147258369");
return decyStr;
} private Key getKey(String strKey) {
Key key = null;
try {
KeyGenerator _generator = KeyGenerator.getInstance("DES");
_generator.init(new SecureRandom(strKey.getBytes()));
key = _generator.generateKey();
_generator = null;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return key;
} /**
 * 获得一次3DES加密后的密文
 * 
 * @param
 * @return strMi
 */
public String getEncString(String strMing, String strKey) {
byte[] byteMi = null;
byte[] byteMing = null;
String strMi = "";
Key key = getKey(strKey);
BASE64Encoder encoder = new BASE64Encoder();
try {
byteMing = strMing.getBytes("utf-8");
byteMi = getEncCode(byteMing, key);
strMi = encoder.encode(byteMi);
} catch (Exception e) {
e.printStackTrace();
} finally {
encoder = null;
byteMi = null;
byteMing = null;
}
return strMi;
} /**
 * 获得两次3DES加密后的密文
 * 
 * @param
 * @return strMi
 */
public String getTwiceEncString(String strMing, String strKey) {
return getEncString(getEncString(strMing, strKey), strKey);
} /**
 * 获得一次3DES解密后的明文
 * 
 * @param
 * @return strMing
 */
public String getDecString(String strMi, String strKey) {
BASE64Decoder base64Decoder = new BASE64Decoder();
byte[] byteMing = null;
byte[] byteMi = null;
String strMing = "";
Key key = getKey(strKey);
try {
byteMi = base64Decoder.decodeBuffer(strMi);
byteMing = getDecCode(byteMi, key);
strMing = new String(byteMing, "utf-8");
} catch (IOException e) {
e.printStackTrace();
} finally {
base64Decoder = null;
byteMing = null;
byteMi = null;
if (strMing.length() == 0) {
return "no";
}
}
return strMing;
} /**
 * 获得两次3DES解密后的明文
 * 
 * @param
 * @return strMing
 */
public String getTwiceDecString(String strMi, String strKey) {
return getDecString(getDecString(strMi, strKey), strKey);
} /**
 * 获得一次3DES加密后的密文
 * 
 * @param byts
 * @return
 */
private byte[] getEncCode(byte[] byts, Key key) {
byte[] byteFina = null;
Cipher cipher;
try {
cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, key);
byteFina = cipher.doFinal(byts);
} catch (Exception e) {
e.printStackTrace();
} finally {
cipher = null;
}
return byteFina;
} /**
 * 获得一次3DES解密后的明文
 * 
 * @param bytd
 * @return
 */
private byte[] getDecCode(byte[] bytd, Key key) {
byte[] byteFina = null;
Cipher cipher = null;
try {
cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, key);
byteFina = cipher.doFinal(bytd);
} catch (Exception e) {
e.printStackTrace();
} finally {
cipher = null;
}
return byteFina;
} public static void main(String[] args) {
Encode td = new Encode();
Key k = td.getKey("Key");
System.out.println("获得的密钥key是:" + k);
String encyStr = td.getEncString("Test", "147258369");
System.out.println("一次加密后的密文是:" + encyStr);
String decyStr = td.getDecString(encyStr, "147258369");
System.out.println("一次解密后的明文是:" + decyStr);
encyStr = td.getTwiceEncString("Test", "147258369");
System.out.println("两次加密后的密文是:" + encyStr);
decyStr = td.getTwiceDecString(encyStr, "147258369");
System.out.println("两次解密后的明文是:" + decyStr);
}}

解决方案 »

  1.   

    Linux上
      

  2.   

    改好了,问题出在随机数生成器上。WINDOWS和LINUX的随机数规则不一样。
    将getKey()函数中的random制定一种随机数产生方式就行了。private Key getKey(String strKey) {
    Key key = null;
    try {
    KeyGenerator _generator = KeyGenerator.getInstance("DES");
    // _generator.init(new SecureRandom(strKey.getBytes()));
    SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
    random.setSeed(strKey.getBytes());
    _generator.init(56, random);
    key = _generator.generateKey();
    _generator = null;
    } catch (NoSuchAlgorithmException e) {
    e.printStackTrace();
    }
    return key;
    }