DES对字符串的加密问题
我在开发中需要对字符串进行DES加密
在本机上测试时对任何字符串加密解密都没有问题
但是一旦使用了Soket网络传输以后解密出现问题,是密文长度不够造成的
我的加密解密程序如下:
public class DesEncrypt {
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
* @return
*/
public String getEncString(String strMing) {
byte[] byteMi = null;
byte[] byteMing = null;
String strMi = "";
Base64 base64en = new Base64();
try {
byteMing = strMing.getBytes("UTF-8");
byteMi = this.getEncCode(byteMing);
strMi = base64en.encode(byteMi);
} catch (Exception e) {
e.printStackTrace();
} finally {
base64en = null;
byteMing = null;
byteMi = null;
}
return strMi;
}
/**
* 解密 以String密文输入,String明文输出
* @param strMi
* @return
*/
public String getDesString(String strMi) {
Base64 base64De = new Base64();
byte[] byteMing = null;
byte[] byteMi = null;
String strMing = "";
try {
byteMi=base64De.decode(strMi);
byteMing = this.getDesCode(byteMi);
strMing = new String(byteMing, "UTF-8");
} catch (Exception e) {
e.printStackTrace();
} finally {
base64De = null;
byteMing = null;
byteMi = null;
}
return strMing;
}
/**
* 加密以byte[]明文输入,byte[]密文输出
* @param byteS
* @return
*/
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
* @return
*/
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;
}
public static void main(String args[]) {
DesEncrypt des=new DesEncrypt();//实例化一个对像
des.getKey("asdefd");//生成密匙
String strEnc = des.getEncString("ceshi测试");//加密字符串,返回String的密文
System.out.println("加密文:"+strEnc);
String strDes = des.getDesString(strEnc);//把String 类型的密文解密
System.out.println("解密文:"+strDes);
}
大家帮忙看看 很急的问题
在本机上测试时对任何字符串加密解密都没有问题
但是一旦使用了Soket网络传输以后解密出现问题,是密文长度不够造成的
我的加密解密程序如下:
public class DesEncrypt {
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
* @return
*/
public String getEncString(String strMing) {
byte[] byteMi = null;
byte[] byteMing = null;
String strMi = "";
Base64 base64en = new Base64();
try {
byteMing = strMing.getBytes("UTF-8");
byteMi = this.getEncCode(byteMing);
strMi = base64en.encode(byteMi);
} catch (Exception e) {
e.printStackTrace();
} finally {
base64en = null;
byteMing = null;
byteMi = null;
}
return strMi;
}
/**
* 解密 以String密文输入,String明文输出
* @param strMi
* @return
*/
public String getDesString(String strMi) {
Base64 base64De = new Base64();
byte[] byteMing = null;
byte[] byteMi = null;
String strMing = "";
try {
byteMi=base64De.decode(strMi);
byteMing = this.getDesCode(byteMi);
strMing = new String(byteMing, "UTF-8");
} catch (Exception e) {
e.printStackTrace();
} finally {
base64De = null;
byteMing = null;
byteMi = null;
}
return strMing;
}
/**
* 加密以byte[]明文输入,byte[]密文输出
* @param byteS
* @return
*/
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
* @return
*/
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;
}
public static void main(String args[]) {
DesEncrypt des=new DesEncrypt();//实例化一个对像
des.getKey("asdefd");//生成密匙
String strEnc = des.getEncString("ceshi测试");//加密字符串,返回String的密文
System.out.println("加密文:"+strEnc);
String strDes = des.getDesString(strEnc);//把String 类型的密文解密
System.out.println("解密文:"+strDes);
}
大家帮忙看看 很急的问题
...全文
相关推荐
字符串DES加密解密,可自定义KEY和向量IV
根据提供的压缩包文件名称列表,有两个同名文件"字符串DES加密解密.exe"和"字符串DES加密解密"。第一个很可能是程序的可执行文件,用户可以直接双击运行。
DES.rar_DES字符串加密_des加密封装
**结果处理**:将加密后的字节数组转换回字符串,可能需要考虑解码问题。然而,描述中提到文件加密没有实现,这意味着这个封装可能不适用于大文件的加密。
MD5字符串加密和DES加密
在VC++环境下,你可以利用CryptoAPI等工具实现这两种加密方式,但需要注意MD5的安全性问题和DES的相对较弱的密钥长度。3DES作为改进,提供更高的安全性,但计算量也相应增加。
DES加密解密字符串的JAVA实现(lp)
#### 三、注意事项- **安全性问题**:由于DES算法密钥长度较短,存在安全风险,实际应用中推荐使用更安全的加密算法如AES。
对于字符串进行DES加密的控件
然而,由于DES的安全性问题,现在更多地被AES(高级加密标准)所取代,后者提供了更长的密钥长度和更高的安全性。在实际应用中,还需要注意以下几点:1.


Java SE
