package service;
public class Test {
/**
* 给字符串加密与解密DEMO
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
String str ="我是中国人,我爱自己的祖国!";
//加密
str = DES.encode(str, "LEO");
System.out.println(str);
//解密
str = DES.decode(str, "LEO");
System.out.println(str);
}
}
=====================================输出结果=====================================
66fe21f5f9e6e10771128e627c9977f664896be4d9afae5143c0f48570b332b15e4f26949a28b1764bdbde4e58b2ba8e
我是中国人,我爱自己的祖国!
=================================================================================
package service;
import java.security.Key;
import javax.crypto.Cipher;
public class DES {
public static String decode(String str,String str_key) throws Exception {
Key key=getKey(str_key);
Cipher decryptCipher = Cipher.getInstance("DES");
decryptCipher.init(Cipher.DECRYPT_MODE, key);
str= new String(decryptCipher.doFinal(hexStr2ByteArr(str)));
return str;
}
public static String encode(String str,String str_key) throws Exception{
Key key=getKey(str_key);
Cipher encryptCipher = Cipher.getInstance("DES");
encryptCipher.init(Cipher.ENCRYPT_MODE, key);
str=byteArr2HexStr(encryptCipher.doFinal(str.getBytes()));
return str;
}
private static Key getKey(String str) throws Exception {
byte[] arrBTmp=str.getBytes();
byte[] arrB = new byte[8];
for (int i = 0; i < arrBTmp.length && i < 8; i++) {
arrB[i] = arrBTmp[i];
}
Key key = new javax.crypto.spec.SecretKeySpec(arrB, "DES");
return key;
}
private static byte[] hexStr2ByteArr(String strIn) throws Exception {
byte[] arrB = strIn.getBytes();
int iLen = arrB.length;
byte[] arrOut = new byte[iLen / 2];
for (int i = 0; i < iLen; i = i + 2) {
String strTmp = new String(arrB, i, 2);
arrOut[i / 2] = (byte) Integer.parseInt(strTmp, 16);
}
return arrOut;
}
private static String byteArr2HexStr(byte[] arrB) throws Exception {
int iLen = arrB.length;
StringBuffer sb = new StringBuffer(iLen * 2);
for (int i = 0; i < iLen; i++) {
int intTmp = arrB[i];
while (intTmp < 0) {
intTmp = intTmp + 256;
}
if (intTmp < 16) {
sb.append("0");
}
sb.append(Integer.toString(intTmp, 16));
}
return sb.toString();
}
}
public class Test {
/**
* 给字符串加密与解密DEMO
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
String str ="我是中国人,我爱自己的祖国!";
//加密
str = DES.encode(str, "LEO");
System.out.println(str);
//解密
str = DES.decode(str, "LEO");
System.out.println(str);
}
}
=====================================输出结果=====================================
66fe21f5f9e6e10771128e627c9977f664896be4d9afae5143c0f48570b332b15e4f26949a28b1764bdbde4e58b2ba8e
我是中国人,我爱自己的祖国!
=================================================================================
package service;
import java.security.Key;
import javax.crypto.Cipher;
public class DES {
public static String decode(String str,String str_key) throws Exception {
Key key=getKey(str_key);
Cipher decryptCipher = Cipher.getInstance("DES");
decryptCipher.init(Cipher.DECRYPT_MODE, key);
str= new String(decryptCipher.doFinal(hexStr2ByteArr(str)));
return str;
}
public static String encode(String str,String str_key) throws Exception{
Key key=getKey(str_key);
Cipher encryptCipher = Cipher.getInstance("DES");
encryptCipher.init(Cipher.ENCRYPT_MODE, key);
str=byteArr2HexStr(encryptCipher.doFinal(str.getBytes()));
return str;
}
private static Key getKey(String str) throws Exception {
byte[] arrBTmp=str.getBytes();
byte[] arrB = new byte[8];
for (int i = 0; i < arrBTmp.length && i < 8; i++) {
arrB[i] = arrBTmp[i];
}
Key key = new javax.crypto.spec.SecretKeySpec(arrB, "DES");
return key;
}
private static byte[] hexStr2ByteArr(String strIn) throws Exception {
byte[] arrB = strIn.getBytes();
int iLen = arrB.length;
byte[] arrOut = new byte[iLen / 2];
for (int i = 0; i < iLen; i = i + 2) {
String strTmp = new String(arrB, i, 2);
arrOut[i / 2] = (byte) Integer.parseInt(strTmp, 16);
}
return arrOut;
}
private static String byteArr2HexStr(byte[] arrB) throws Exception {
int iLen = arrB.length;
StringBuffer sb = new StringBuffer(iLen * 2);
for (int i = 0; i < iLen; i++) {
int intTmp = arrB[i];
while (intTmp < 0) {
intTmp = intTmp + 256;
}
if (intTmp < 16) {
sb.append("0");
}
sb.append(Integer.toString(intTmp, 16));
}
return sb.toString();
}
}