字符串加密与解密
来源:互联网 发布:c语言反斜杠怎么打 编辑:程序博客网 时间:2024/05/16 08:53
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();
}
}
0 0
- 字符串加密与解密
- 字符串加密与解密
- 字符串-加密与解密
- 字符串进行加密与解密。
- java 字符串加密与解密
- 字符串的加密与解密
- 字符串的加密与解密
- java字符串加密与解密
- 字符串的加密与解密
- java 对字符串DES加密与解密
- C# 简单字符串加密与解密
- 字符串的(编码/加密)与(解码/解密)
- C#一个字符串的加密与解密
- c#字符串加密与解密封装
- 字符串的简单加密与解密
- 加密解密字符串
- 字符串加密解密函数
- 字符串加密解密
- iOS 中KVC、KVO、NSNotification、delegate 总结及区别
- [leetcode]Happy Number
- Java——人口翻倍
- 【项目管理软件盛宴】谁才是人气王?
- 题目1514:数值的整数次方
- 字符串加密与解密
- c++设计模式之桥接模式
- Linux 终端下的小工具-JC
- 第十二周项目4——点、圆的关系(1—4)
- 让各种开机自启的程序实现延时启动,来加快开机时间
- jsp牛刀小试之在线报名系统
- java多线程(一):进程与线程
- 面向对象(下)
- Fragment生命周期