分享一个非常不错的字符串加密和解密的程序。

可以指定不同的密钥对同一字符串进行不同的加密操作,增强加密性能。

Java代码如下:

package com.app;

import java.security.Key;
import java.security.Security;
import javax.crypto.Cipher;
/**
* DES加密和解密工具,可以对字符串进行加密和解密操作 。
*/
public class Test4 {
/**
* 默认构造方法,使用默认密钥
*/
public Test4() throws Exception {
this(strDefaultKey);
}
/**
* 指定密钥构造方法
* @param strKey 指定的密钥
* @throws Exception
*/
public Test4(String strKey) throws Exception {
// Security.addProvider(new com.sun.crypto.provider.SunJCE());
Key key = getKey(strKey.getBytes());
encryptCipher = Cipher.getInstance("DES");
encryptCipher.init(Cipher.ENCRYPT_MODE, key);
decryptCipher = Cipher.getInstance("DES");
decryptCipher.init(Cipher.DECRYPT_MODE, key);
}
/** 字符串默认键值 */
private static String strDefaultKey = "national";
/** 加密工具 */
private Cipher encryptCipher = null;
/** 解密工具 */
private Cipher decryptCipher = null;
/**
* 将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数组
*/
public static byte[] hexStr2ByteArr(String strIn) throws Exception {
byte[] arrB = strIn.getBytes();
int iLen = arrB.length;
// 两个字符表示一个字节,所以字节数组长度是字符串长度除以2
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;
}
/**
* 加密字节数组
* @param arrB 需加密的字节数组
* @return 加密后的字节数组
*/
public byte[] encrypt(byte[] arrB) throws Exception {
return encryptCipher.doFinal(arrB);
}
/**
* 加密字符串
* @param strIn 需加密的字符串
* @return 加密后的字符串
*/
public String encrypt(String strIn) throws Exception {
return byteArr2HexStr(encrypt(strIn.getBytes()));
}
/**
* 解密字节数组
* @param arrB 需解密的字节数组
* @return 解密后的字节数组
*/
public byte[] decrypt(byte[] arrB) throws Exception {
return decryptCipher.doFinal(arrB);
}
/**
* 解密字符串
* @param strIn 需解密的字符串
* @return 解密后的字符串
*/
public String decrypt(String strIn) throws Exception {
return new String(decrypt(hexStr2ByteArr(strIn)));
}
/**
* 从指定字符串生成密钥,密钥所需的字节数组长度为8位 不足8位时后面补0,超出8位只取前8位
* @param arrBTmp 构成该字符串的字节数组
* @return 生成的密钥
*/
private Key getKey(byte[] arrBTmp) throws Exception {
// 创建一个空的8位字节数组(默认值为0)
byte[] arrB = new byte[8];
// 将原始字节数组转换为8位
for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) {
arrB[i] = arrBTmp[i];
}
// 生成密钥
Key key = new javax.crypto.spec.SecretKeySpec(arrB, "DES");
return key;
}
public static void main(String[] args) {
try {
String test1 = "987654321";
Test4 des1 = new Test4();// 使用默认密钥
System.out.println("加密前的字符:" + test1);
System.out.println("加密后的字符:" + des1.encrypt(test1));
System.out.println("解密后的字符:" + des1.decrypt(des1.encrypt(test1))); String test2 = "123456789";
Test4 des2 = new Test4("leeme32nz");// 自定义密钥
System.out.println("加密前的字符:" + test2);
System.out.println("加密后的字符:" + des2.encrypt(test2));
System.out.println("解密后的字符:" + des2.decrypt(des2.encrypt(test2)));
} catch (Exception e) {
e.printStackTrace();
}
}
}

运行结果:

加密前的字符:987654321
加密后的字符:5579cb6ffef7b473e6b92e28c43dccd4
解密后的字符:987654321 加密前的字符:123456789
加密后的字符:beeaf4d9ae0b2b5153a02d9feb0f6a9c
解密后的字符:123456789

【注】转载自http://www.blogjava.net/vwpolo/archive/2009/12/05/304874.html

【Java】通过DES加密和解密工具,对字符串进行加密和解密操作的更多相关文章

  1. RSA加解密工具类RSAUtils.java,实现公钥加密私钥解密和私钥解密公钥解密

    package com.geostar.gfstack.cas.util; import org.apache.commons.codec.binary.Base64; import javax.cr ...

  2. C#一个字符串的加密与解密

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.S ...

  3. java常用加密和解密工具类EncryptUtil.java

    package cn.util; import java.io.UnsupportedEncodingException; import java.security.MessageDigest; im ...

  4. java实现DES加密与解密,md5加密

    很多时候要对秘要进行持久化加密,此时的加密采用md5.采用对称加密的时候就采用DES方法了 import java.io.IOException; import java.security.Messa ...

  5. Android和java平台 DES加密解密互通程序及其不能互通的原因

    网上的demo一搜一大堆,但是,基本上都是一知半解(包括我).为什么呢?我在尝试分别在两个平台加密的时候,竟然发现Android DES 加密和java DES加密的程序不能互通.就是加密的结果不一样 ...

  6. java 加密解密工具类(实用!!!)

    最近发现了一个加密解密的好例子,很方便使用,可以作为平时开发的工具集,记录一下. package com.sh.springboottdemo2.util; import com.sun.org.ap ...

  7. 一个java的DES加密解密类转换成C#

    一个java的des加密解密代码如下: //package com.visionsky.util; import java.security.*; //import java.util.regex.P ...

  8. JAVA使用DES加密算法加密解密

    程序中使用了.properties文件作为参数配置文档,好处是灵活配置各项参数 一旦对数据库的一些参数进行了配置,势必涉及数据库的IP,端口,用户名和密码 properties文件全是unicode编 ...

  9. java 实现 DES加密 解密算法

    DES算法的入口参数有三个:Key.Data.Mode.其中Key为8个字节共64位,是DES算法的工作密钥:Data也为8个字节64位,是要被加密或被解密的数据:Mode为DES的工作方式,有两种: ...

随机推荐

  1. POJ 3414 Pots bfs打印方案

    题目: http://poj.org/problem?id=3414 很好玩的一个题.关键是又16ms 1A了,没有debug的日子才是好日子.. #include <stdio.h> # ...

  2. 《Linux命令行大全》系列(一、shell是什么)

    学习 Linux, 从命令开始 图形界面只是让简单事情,更简单 图形化界面能快速处理简单的事情,如打开/关闭文件 然后,随着事务复杂度的提升,图形化界面的操作也就繁琐起来 例如,word中插入一个图片 ...

  3. Android 自定义RadioButton实现

    由于使用小米系统MIUI运行是RadioButton样式跟google Android API自定义的不一样,则我们可以定义任何想要的东东.没有做不到,只有想不到 Android 自定义RadioBu ...

  4. Investigation of Different Nets and Layers

    Investigation of Different Nets and Layers Overview of AlexNet (MIT Places | Flickr Finetuned | Oxfo ...

  5. hdu 1281

    二分图,简单的模板题,不过题目比较难懂: 其中important chess就是删掉它不能够完美匹配,所以就枚举每一个可能删的棋子: 代码: #include <cstdio> #incl ...

  6. [topcoder]IncreasingSubsequences

    http://community.topcoder.com/stat?c=problem_statement&pm=7753&rd=10672 http://community.top ...

  7. 《鸟哥的Linux私房菜》读书笔记五

    1. Ctrl+alt+FX(X=1~6)可以切换到6个不同的文字界面终端(Terminal) 再按Ctrl+alt+F7就可以回到X Window,按Ctrl+alt+Backspace这是结束所有 ...

  8. leetcode面试准备:Divide Two Integers

    leetcode面试准备:Divide Two Integers 1 题目 Divide two integers without using multiplication, division and ...

  9. 转:二十、java的抽象类

    http://blog.csdn.net/liujun13579/article/details/7737667 现实世界中,人们表征世界时,会把现实世界中的很多类具有相同特征的事物归为一个抽象类.比 ...

  10. 【HDOJ】3496 Watch The Movie

    二维费用背包. #include <stdio.h> #include <string.h> #define mymax(a, b) (a>b) ? a:b ][]; ] ...