一个小的加密


import java.io.IOException;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class DesTmp {

private static String strDefaultKey = "TmpWin";

private byte[] desKey;

public DesTmp(){
this.desKey = strDefaultKey.getBytes();
}

public DesTmp(String desKey) {
this.desKey = desKey.getBytes();
}

/**
* 将byte数组转换为表示16进制值的字符串, 如:byte[]{8,18}转换为:0813, 和public static byte[]
* hexStr2ByteArr(String strIn) 互为可逆的转换过程
*
* @param arrB
* 需要转换的byte数组
* @return 转换后的字符串
* @throws Exception
* 本方法不处理任何异常,所有异常全部抛出
*/
public static String byteArr2HexStr(byte[] arrB) throws Exception {
int iLen = arrB.length;
// 每个byte用两个字符才能表示,所以字符串的长度是数组长度的两倍
StringBuffer sb = new StringBuffer(iLen * 2);
for (int i = 0; i < iLen; i++) {
int intTmp = arrB[i];
// 把负数转换为正数
while (intTmp < 0) {
intTmp = intTmp + 256;
}
// 小于0F的数需要在前面补0
if (intTmp < 16) {
sb.append("0");
}
sb.append(Integer.toString(intTmp, 16));
}
return sb.toString();
}

/**
* 将表示16进制值的字符串转换为byte数组, 和public static String byteArr2HexStr(byte[] arrB)
* 互为可逆的转换过程
*
* @param strIn
* 需要转换的字符串
* @return 转换后的byte数组
* @throws Exception
* 本方法不处理任何异常,所有异常全部抛出
* @author <a href="mailto:leo841001@163.com">LiGuoQing</a>
*/
public static byte[] hexStr2ByteArr(String strIn) throws Exception {
int iLen = strIn.getBytes().length;

// 两个字符表示一个字节,所以字节数组长度是字符串长度除以2
byte[] arrOut = new byte[iLen / 2];
String strTmp;
for (int i = 0; i < iLen; i = i + 2) {
strTmp = strIn.substring(i, i + 2);
arrOut[i / 2] = (byte) Integer.parseInt(strTmp, 16);
}
return arrOut;
}

public byte[] desEncrypt(byte[] plainText) throws Exception {
SecureRandom sr = new SecureRandom();
byte rawKeyData[] = desKey;
DESKeySpec dks = new DESKeySpec(rawKeyData);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey key = keyFactory.generateSecret(dks);
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, key, sr);
byte data[] = plainText;
byte encryptedData[] = cipher.doFinal(data);
return encryptedData;
}

public byte[] desDecrypt(byte[] encryptText) throws Exception {
SecureRandom sr = new SecureRandom();
byte rawKeyData[] = desKey;
DESKeySpec dks = new DESKeySpec(rawKeyData);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey key = keyFactory.generateSecret(dks);
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, key, sr);
byte encryptedData[] = encryptText;
byte decryptedData[] = cipher.doFinal(encryptedData);
return decryptedData;
}

public String encrypt(String input) throws Exception {
String base64 = base64Encode(desEncrypt(input.getBytes()));
base64 = base64.replace("=", "_");
base64 = base64.replace("/", "-");
base64 = base64.replace("+", "!");
base64 = base64.replace("\r\n", "");
base64 = base64.replace("\n", "");
return base64;
}


public String decrypt(String input) throws Exception {
input = input.replace("_", "=");
input = input.replace("-", "/");
input = input.replace("!", "+");
//input = input.replace("", "\r\n");
byte[] result = base64Decode(input);
return new String(desDecrypt(result));
}

public static String base64Encode(byte[] s) {
if (s == null)
return null;
BASE64Encoder b = new sun.misc.BASE64Encoder();
return b.encode(s);
}

public static byte[] base64Decode(String s) throws IOException {
if (s == null)
return null;
BASE64Decoder decoder = new BASE64Decoder();
byte[] b = decoder.decodeBuffer(s);
return b;
}


public static void main(String[] args) throws Exception {
//String input = "中国人的一切";//!NrbmCgiHDlxPr5F0wqZM6!eLxOu3cPLr!o5OZ4oVxhszMIfWRm23GxZ3UGOKLb!zxZe!s5rFqF8XZEhywjWrftBnsskzeiG
//String input = "fdkwHd6fgIfD8eCzwztf1HYVY63Tdnaumso-nfVeP4cXm-K-IiuV88OClQLoNbsop84jlkEZcYmBMNmm-Qu75hPXrZVFd!7J";
String input = "Z8IvPesWDqiyG!hGExePXLysT5fazHybq1oGZ!L3CBiDkpbhBTxVFn2F1XVri4xueNLQqhJKeKRZLHVO42eYXftBnsskzeiG";
//String input = "Pd!IipaPNSfHeZFnagGIpHI2GYVlVaHoj2pO60v9KjjXHnLfcOG0HnXTWkiXhB43vofgwTgpqqMVGrNLWR41S-luttdl7M0O-c9zuvDkwnk_//";
DesTmp crypt = new DesTmp();
System.out.println("Encode:" + crypt.encrypt(input));
System.out.println("Decode:" + crypt.decrypt(crypt.encrypt(input)));
System.out.println("result:" + crypt.decrypt(input));
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值