DES加密解密(JAVA)
来源:互联网 发布:java虚拟机经典书籍 编辑:程序博客网 时间:2024/05/03 13:27
今天在网上搜了一下DES加密算法(注释部分展开),总报如下错误:
javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when decrypting with padded cipher
最终原因是byte数组是不能强制转换成字符串的,换言之:字符串(2个字节)和byte数组在这种情况下不是互逆的;要避免这种情况,我们需要做一些修订,可以考虑将二进制数据转换成十六进制表示
在网上找了些代码,修改以后如下:
public class DES {
private Key key; //密钥
/**
* 根据参数生成KEY
*
* @param strKey 密钥字符串
*/
public void getKey(String strKey) {
try {
KeyGenerator _generator = KeyGenerator.getInstance("DES");
_generator.init(new SecureRandom(strKey.getBytes()));
this.key = _generator.generateKey();
_generator = null;
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 加密String明文输入,String密文输出
*
* @param strMing String明文
* @return String密文
*/
public String getEncString(String strMing) {
byte[] byteMi = null;
byte[] byteMing = null;
String strMi = "";
try {
/*byteMing = strMing.getBytes("UTF8");
byteMi = this.getEncCode(byteMing);
strMi = new String(byteMi, "UTF8");*/
strMi = parseByte2HexStr(getEncCode(strMing.getBytes()));
} catch (Exception e) {
e.printStackTrace();
} finally {
byteMing = null;
byteMi = null;
}
return strMi;
}
/**
* 解密 以String密文输入,String明文输出
*
* @param strMi String密文
* @return String明文
*/
public String getDesString(String strMi) {
byte[] byteMi = null;
byte[] byteMing = null;
String strMing = "";
try {
/*byteMi = strMi.getBytes("UTF8");
byteMing = this.getDesCode(byteMi);
strMing = new String(byteMing, "UTF8");*/
strMing = new String(getDesCode(parseHexStr2Byte(strMi)));
} catch (Exception e) {
e.printStackTrace();
} finally {
byteMing = null;
byteMi = null;
}
return strMing;
}
/**
* 加密以byte[]明文输入,byte[]密文输出
*
* @param byteS byte[]明文
* @return byte[]密文
*/
private 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) {
e.printStackTrace();
} finally {
cipher = null;
}
return byteFina;
}
/**
* 解密以byte[]密文输入,以byte[]明文输出
*
* @param byteD byte[]密文
* @return byte[]明文
*/
private 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) {
e.printStackTrace();
} finally {
cipher = null;
}
return byteFina;
}
/**
* 将二进制转换成16进制
* @param buf
* @return
*/
public String parseByte2HexStr(byte buf[]) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < buf.length; i++) {
String hex = Integer.toHexString(buf[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
sb.append(hex.toUpperCase());
}
return sb.toString();
}
/**
* 将16进制转换为二进制
* @param hexStr
* @return
*/
public byte[] parseHexStr2Byte(String hexStr) {
if (hexStr.length() < 1)
return null;
byte[] result = new byte[hexStr.length() / 2];
for (int i = 0; i < hexStr.length() / 2; i++) {
int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2),
16);
result[i] = (byte) (high * 16 + low);
}
return result;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
DES encrypt = new DES();
encrypt.getKey("嘿嘿");
String strEnc = encrypt.getEncString("hi,我是程序员");
System.out.println(strEnc);
String strDes = encrypt.getDesString(strEnc);
System.out.println(strDes);
}
}
- DES加密解密(JAVA)
- java--des加密解密
- java DES加密解密
- java DES加密解密
- java DES加密解密
- Java DES加密解密
- Java DES 加密解密
- java DES加密解密
- java DES加密解密
- JAVA DES加密解密
- JAVA DES加密解密
- java des加密解密
- DES加密与解密(java版)
- JAVA 3DES 加密解密(二)
- java DES加密,解密 (一)
- java DES 加密解密 (二)
- Java加密解密算法-DES加密解密
- ios des加密与解密(对应JAVA加解密)
- GNU连接器ld
- Nginx和Tomcat负载均衡实现session共享
- hdu 1234 Rooted Trees Problem 有重复的组合+乘法原理
- ARM9协处理器CP15及MCR和MRC指令
- 2011-09-29 10:06 java对象OBJECT转换成int型
- DES加密解密(JAVA)
- red5 媒体服务器在eclipse插件的安装
- 在VS2005下编译x264的心得体会
- Redhat 和 Ubuntu linux安装VMware tools
- 树莓派(一)安装系统
- 如何把UltraEdit加入到系统右键菜单中
- 12岁的少年教你用Python做小游戏
- 15个值得开发人员关注的jQuery开发技巧和心得
- jqGrid: jQuery Grid Plugin PHP component Demo