我在windows上做的加密到linux上解密总是失败,报java.lang.Exception: pad block corrupted
解决方案 »
- javascript代码有时候用‘’ 有时候用 “”
- Swing中如何让窗体全屏,总在最前面,没有边框和控制栏,并且不能切换出去,也不能最小化……
- NPE异常求教
- 这个操作数据库的问题好奇怪?
- 怎么创建一个dbf文件...
- 关于JAVA的标准库
- 急!!!一个很简单的问题,谁回答出来我给他(她)100分!!!!
- 用JBuilder编译时弹出runtime properties对话框,如何搞定它?
- 我安装了JBuilder 5 为什么 Enterprise 项的很多东西都是灰显得?
- java里怎么定义一个有范围的变量
- 找下原因:dos下运行Junit测试用例
- 大家说说PrintStream 与 PrintWriter的区别?
import java.security.Key;
import java.security.SecureRandom;import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;public class DesEncrypt {
private static Key key;
private final static String KEY_STRING = "cssc2010cssc2010zcec";
private static DesEncrypt instance = null;
public static DesEncrypt getInstance(){
if(instance == null){
instance = new DesEncrypt();
}
return instance;
}
public DesEncrypt(String str) {
//String keyString ="ACDAFAAD";
getKey(str);// 生成密匙
} private DesEncrypt(){
getKey(KEY_STRING);// 生成密匙
}
/**
* 根据参数生成KEY
*/
public void getKey(String strKey) {
try {
KeyGenerator _generator = KeyGenerator.getInstance("DES");
_generator.init(new SecureRandom(strKey.getBytes()));
key = _generator.generateKey();
_generator = null;
} catch (Exception e) {
throw new RuntimeException(
"Error initializing SqlMap class. Cause: " + e);
}
} /**
* 加密String明文输入,String密文输出
*/
public String getEncString(String strMing) {
byte[] byteMi = null;
byte[] byteMing = null;
String strMi = "";
BASE64Encoder base64en = new BASE64Encoder();
try {
byteMing = strMing.getBytes("UTF8");
byteMi = this.getEncCode(byteMing);
strMi = base64en.encode(byteMi);
} catch (Exception e) {
throw new RuntimeException(
"Error initializing SqlMap class. Cause: " + e);
} finally {
base64en = null;
byteMing = null;
byteMi = null;
}
return strMi;
} /**
* 解密 以String密文输入,String明文输出
*
* @param strMi
* @return
*/
public String getDesString(String strMi) {
BASE64Decoder base64De = new BASE64Decoder();
byte[] byteMing = null;
byte[] byteMi = null;
String strMing = "";
try {
byteMi = base64De.decodeBuffer(strMi);
byteMing = this.getDesCode(byteMi);
strMing = new String(byteMing, "UTF8");
} catch (Exception e) {
throw new RuntimeException(
"Error initializing SqlMap class. Cause: " + e);
} finally {
base64De = null;
byteMing = null;
byteMi = null;
}
return strMing;
} /**
* 加密以byte[]明文输入,byte[]密文输出
*
* @param byteS
* @return
*/
public static byte[] getEncCode(byte[] byteS) {
byte[] byteFina = null;
Cipher cipher;
try {
cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, key);
byteFina = cipher.doFinal(byteS);
} catch (Exception e) {
throw new RuntimeException(
"Error initializing SqlMap class. Cause: " + e);
} finally {
cipher = null;
}
return byteFina;
} /**
* 解密以byte[]密文输入,以byte[]明文输出
*
* @param byteD
* @return
*/
public static byte[] getDesCode(byte[] byteD) {
Cipher cipher;
byte[] byteFina = null;
try {
cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, key);
byteFina = cipher.doFinal(byteD);
} catch (Exception e) {
throw new RuntimeException(
"Error initializing SqlMap class. Cause: " + e);
} finally {
cipher = null;
}
return byteFina;
} public static void main(String args[]) {
DesEncrypt des = DesEncrypt.getInstance();
String str1 = "ASDDasdadadadada你的明 aaasda啊大大大大的dadadq使用手册欠妥人ASDADA";
// DES加密
String str2 = des.getEncString(str1);
str2="sPtE/3vQPyiRYXfyUIVFPx0T9Ya3Uhzg0bNQ+4f4ZvTeOII6aE7+aVchiC3s67T0C8cklLjIEfxiXE3O3gXrwg==";
System.out.println("根据密钥解密后的明文:" + des.getDesString(str2));
}
}
* 根据参数生成KEY
*/
public void getKey(String strKey) {
try {
KeyGenerator _generator = KeyGenerator.getInstance("DES");
_generator.init(new SecureRandom(strKey.getBytes()));
key = _generator.generateKey();
_generator = null;
} catch (Exception e) {
throw new RuntimeException("Error initializing SqlMap class. Cause: " + e);
}
}使用 KeyGenerator 生成的密钥是随机密钥,并不是你传入的密钥,更改一下:public Key toKey(byte[] key) throws Exception {
KeySpec dks = new DESKeySpec(key);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
return keyFactory.generateSecret(dks);
}DES 属于对称加密算法的块加密,密钥应该是 8 个字节。private final static String KEY_STRING = "cssc2010cssc2010zcec";这样的密钥在 DES 中是不合法的。