以下为加密的工具类:

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom; import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;

import com.sun.xml.internal.messaging.saaj.packaging.mime.util.BASE64DecoderStream;
  import com.sun.xml.internal.messaging.saaj.packaging.mime.util.BASE64EncoderStream;

public class util {
/**
* 传入名文和公钥钥对数据进行RSA解密
* <br>生成时间:2014年5月2日 下午2:38:13
* <br>返回值:String
* <br>@param src
* <br>@param pubkey
* <br>@return
*/
public static String rsaEncoding(String src,PublicKey pubkey){
try {
Cipher cip = Cipher.getInstance("RSA");
cip.init(cip.ENCRYPT_MODE, pubkey);
byte[] by = cip.doFinal(src.getBytes());
return new String(BASE64EncoderStream.encode(by)); } catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
} catch (NoSuchPaddingException e) {
throw new RuntimeException(e);
} catch (InvalidKeyException e) {
throw new RuntimeException(e);
} catch (IllegalBlockSizeException e) {
throw new RuntimeException(e);
} catch (BadPaddingException e) {
throw new RuntimeException(e);
} }
/**
* 传入RSA密文和私钥对数据进行解密
* <br>生成时间:2014年5月2日 下午2:37:08
* <br>返回值:String
* <br>@param sec
* <br>@param privkey
* <br>@return
*/
public static String rsaDeEncoding(String sec,PrivateKey privkey){
try {
Cipher cip = Cipher.getInstance("RSA");
cip.init(cip.DECRYPT_MODE, privkey);
byte[] by = BASE64DecoderStream.decode(sec.getBytes());
return new String(cip.doFinal(by)); } catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
} catch (NoSuchPaddingException e) {
throw new RuntimeException(e);
} catch (InvalidKeyException e) {
throw new RuntimeException(e);
} catch (IllegalBlockSizeException e) {
throw new RuntimeException(e);
} catch (BadPaddingException e) {
throw new RuntimeException(e);
} } /**
* 传入字符串、密钥,并加密字符串(对称加密加密),支持:DES、AES、DESede(3DES)
* <br>生成时间:2014年5月2日 下午12:05:44
* <br>返回值:String 密文
* <br>@param src
* <br>@param key
* <br>@param method(DES、AES、DESede)
* <br>@return
*/
//对称加密加密
public static String doubKeyEncoding(String src,String keysrc,String method) {
SecretKey key;
try {
//生成密钥
KeyGenerator kg = KeyGenerator.getInstance(keysrc);
//初始化此密钥生成器。
kg.init(new SecureRandom(keysrc.getBytes("utf-8")));
key = kg.generateKey(); //加密
Cipher ciph = Cipher.getInstance(method);
ciph.init(Cipher.ENCRYPT_MODE, key);
ciph.update(src.getBytes("utf-8"));
//使用64进行编码,一避免出现丢数据情景
byte[] by = BASE64EncoderStream.encode(ciph.doFinal());
return new String(by);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
} catch (NoSuchPaddingException e) {
throw new RuntimeException(e);
} catch (InvalidKeyException e) {
throw new RuntimeException(e);
} catch (IllegalBlockSizeException e) {
throw new RuntimeException(e);
} catch (BadPaddingException e) {
throw new RuntimeException(e);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
/**
* 传入字符串、密钥、加密方式,并解密字符串(对称加密解密密),支持:DES、AES、DESede(3DES)
* <br>生成时间:2014年5月2日 下午1:12:13
* <br>返回值:String 密钥原文
* <br>@param sec
* <br>@param key
* <br>@param method(DES、AES、DESede)
* <br>@return
*/
public static String doubKeyDencoding(String sec,String keysrc,String method) {
SecretKey key;
try {
//生成密钥
KeyGenerator kg = KeyGenerator.getInstance(keysrc);
//初始化此密钥生成器。
kg.init(new SecureRandom(keysrc.getBytes("utf-8")));
key = kg.generateKey();
//加密
Cipher ciph = Cipher.getInstance(method);
ciph.init(ciph.DECRYPT_MODE, key);
//使用64进行解码,一避免出现丢数据情况
byte[] by = BASE64DecoderStream.decode(sec.getBytes());
ciph.update(by);
return new String(ciph.doFinal()); } catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
} catch (NoSuchPaddingException e) {
throw new RuntimeException(e);
} catch (InvalidKeyException e) {
throw new RuntimeException(e);
} catch (IllegalBlockSizeException e) {
throw new RuntimeException(e);
} catch (BadPaddingException e) {
throw new RuntimeException(e);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
} /**
* 单向信息加密(信息摘要),支持:md5、md2、SHA(SHA-1,SHA1)、SHA-256、SHA-384、SHA-512,
* <br>生成时间:2014年5月2日 上午11:13:44
* <br>返回值:String 加密后的密文
* <br>@param src 传入加密字符串(明文)
* <br>@param method 指定算法(md5、md2、SHA(SHA-1,SHA1)、SHA-256、SHA-384、SHA-512)
* <br>@return
*/
public static String ecodingPasswd(String src,String method){ try {
//信息摘要算法
MessageDigest md5 = MessageDigest.getInstance(method);
md5.update(src.getBytes());
byte[] encoding = md5.digest();
//使用64进行编码,一避免出现丢数据情景
return new String(BASE64EncoderStream.encode(encoding));
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e+"加密失败!!");
} }
}

以下为测试类

import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey; import org.junit.Test; import com.shuqi.utils.util; public class test {
@Test
public void ecoding() {
// //MD5 加密
// System.out.println("md5"+util.ecodingPasswd("shuqi", "md5"));
// System.out.println("md2"+util.ecodingPasswd("shuqi", "md2"));
// //sha加密
// System.out.println("sha"+util.ecodingPasswd("shuqi", "sha"));
// System.out.println("sha-1"+util.ecodingPasswd("shuqi", "sha-1"));
// System.out.println("sha1"+util.ecodingPasswd("shuqi", "sha1"));
// System.out.println("sha-256"+util.ecodingPasswd("shuqi", "sha-256"));
// System.out.println("sha-384"+util.ecodingPasswd("shuqi", "sha-384"));
// System.out.println("sha-512"+util.ecodingPasswd("shuqi", "sha-512")); // String mi = util.doubKeyEncoding("hehe", "shuqi", "AES");
// String ming = util.doubKeyDencoding(mi, "shuqi", "AES");
// System.out.println("AES加密解密");
// System.out.println("加密hehhe,密文 >>> "+mi);
// System.out.println("解密"+mi+",明文 >>> "+ming);
//
// mi = util.doubKeyEncoding("hehe", "shuqi", "DES");
// ming = util.doubKeyDencoding(mi, "shuqi", "DES");
// System.out.println("DES加密解密");
// System.out.println("加密hehhe,密文 >>> "+mi);
// System.out.println("解密"+mi+",明文 >>> "+ming);
//
// mi = util.doubKeyEncoding("hehe", "shuqi", "DESede");
// ming = util.doubKeyDencoding(mi, "shuqi", "DESede");
// System.out.println("DESede加密解密");
// System.out.println("加密hehhe,密文 >>> "+mi);
// System.out.println("解密"+mi+",明文 >>> "+ming); /**
* RSA算法的验证
*/
try {
//实例化一个密钥对生成器
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
//得到密钥对对象
KeyPair kp = kpg.genKeyPair();
PrivateKey prikay = kp.getPrivate();
PublicKey pubkey = kp.getPublic(); String mi = util.rsaEncoding("shuqi", pubkey);
String ming = util.rsaDeEncoding(mi, prikay);
System.out.println(ming+" :: "+mi); } catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
} }

  参考文章:shibenjie的博文  snowolf的博文

最新文章

  1. ArrayList&lt;E&gt;源码分析
  2. Ubuntu——apt &amp;&amp; dpkg参考
  3. css3 中的transition和transform
  4. Python之函数之路
  5. javascript最容易混淆的作用域、提升、闭包
  6. 服务端提供的JSON数据接口与用户端接收解析JSON数据
  7. Linux多线程(三)(同步互斥)
  8. MySQL单列索引和组合索引的区别介绍
  9. Android ScrollView嵌套HorizontalScrollView 滑动问题 ScrollView包括GridView显示问题
  10. 逻辑回归模型(Logistic Regression, LR)基础
  11. tt程序分析(一)
  12. 《MySQL必知必会》[03] 表数据的增删改
  13. Java-IO之总框架
  14. MyEclipse破解步骤
  15. Postgres使用ALTER USER命令修改用户的密码、密码过期,锁定,解锁
  16. Com类型
  17. android studio友盟分享demo运行报错Gradle&#39;s dependency cache may be corrupt解决方法
  18. Windows中的键盘快捷方式
  19. mybatis关联查询数据模型分析——(七)
  20. ZOJ 2975 Kinds of Fuwas

热门文章

  1. java:添加一条数据到数据库中文乱码
  2. Spring AOP那些学术概念—通知、增强处理连接点(JoinPoint)切面(Aspect)
  3. 关于函数strtok和strtok_r的使用要点和实现原理(二)
  4. Android GridView 二维布局界面
  5. Vim 缓冲区与窗口 操作
  6. 11.java.lang.ArrayStoreException
  7. 《转》JAVA中PriorityQueue优先级队列使用方法
  8. 如何解决”无法将类型为“System.DateTime”的对象强制转换为类型“System.String”。“
  9. 扩展C++ string类
  10. #include &lt;iostream&gt;