RSA加/解密工具类-RSAUtil

落日映苍穹つ 2022-05-21 08:05 343阅读 0赞
  1. import org.slf4j.Logger;
  2. import org.slf4j.LoggerFactory;
  3. import sun.misc.BASE64Decoder;
  4. import sun.misc.BASE64Encoder;
  5. import javax.crypto.Cipher;
  6. import java.io.FileInputStream;
  7. import java.io.IOException;
  8. import java.io.ObjectInputStream;
  9. import java.security.Key;
  10. /** * Copyright (C), 2011-2018 {company} * FileName: * Author: xxx * Email: xxx * Date: 2018/6/6 14:11 * Description: * History: * <Author> <Time> <version> <desc> * {xxx} 14:11 1.0 Create */ public class RSAUtil {
  11. private static final Logger log = LoggerFactory.getLogger(RSAUtil.class);
  12. /** 指定加密算法为RSA */ private static final String ALGORITHM = "RSA";
  13. /** 指定公钥存放文件 */ private static String PUBLIC_KEY_FILE = "merkey.public";
  14. /** 指定私钥存放文件 */ private static String PRIVATE_KEY_FILE = "merkey.private";
  15. public static void main(String[] args) throws Exception {
  16. String source = "你好nihao";// 要加密的字符串 System.out.println("准备用公钥加密的字符串为:" + source);
  17. String cryptograph = encrypt(source);// 生成的密文 System.out.print("用公钥加密后的结果为:" + cryptograph);
  18. System.out.println();
  19. String target = decrypt(cryptograph);// 解密密文 System.out.println("用私钥解密后的字符串为:" + target);
  20. System.out.println();
  21. }
  22. /** * 加密方法 * @param source 源数据 * @return * @throws Exception */ public static String encrypt(String source) throws Exception {
  23. Key publicKey = getKey(PUBLIC_KEY_FILE);
  24. /** 得到Cipher对象来实现对源数据的RSA加密 */ Cipher cipher = Cipher.getInstance(ALGORITHM);
  25. cipher.init(Cipher.ENCRYPT_MODE, publicKey);
  26. byte[] b = source.getBytes();
  27. /** 执行加密操作 */ byte[] b1 = cipher.doFinal(b);
  28. BASE64Encoder encoder = new BASE64Encoder();
  29. return encoder.encode(b1);
  30. }
  31. /** * 解密算法 * @param cryptograph 密文 * @return * @throws Exception */ public static String decrypt(String cryptograph) throws Exception {
  32. Key privateKey = getKey(PRIVATE_KEY_FILE);
  33. /** 得到Cipher对象对已用公钥加密的数据进行RSA解密 */ Cipher cipher = Cipher.getInstance(ALGORITHM);
  34. cipher.init(Cipher.DECRYPT_MODE, privateKey);
  35. BASE64Decoder decoder = new BASE64Decoder();
  36. byte[] b1 = decoder.decodeBuffer(cryptograph);
  37. /** 执行解密操作 */ byte[] b = null;
  38. try {
  39. b = cipher.doFinal(b1);
  40. return new String(b);
  41. }catch (Exception e){
  42. log.error("解密失败,秘钥不正确", e);
  43. }
  44. return "";
  45. }
  46. private static Key getKey(String fileName) throws Exception, IOException {
  47. Key key;
  48. ObjectInputStream ois = null;
  49. try {
  50. /** 将文件中的私钥对象读出 */ ois = new ObjectInputStream(new FileInputStream(fileName));
  51. key = (Key) ois.readObject();
  52. } catch (Exception e) {
  53. throw e;
  54. } finally {
  55. if(ois != null){
  56. ois.close();
  57. }
  58. }
  59. return key;
  60. }
  61. }

发表评论

表情:
评论列表 (有 0 条评论,343人围观)

还没有评论,来说两句吧...

相关阅读

    相关 RSA解密

    一:RSA加解密背景 RSA是目前使用最广泛的公钥密码体制之一。它是1977年由罗纳德·李维斯特(Ron Rivest)、阿迪·萨莫尔(Adi Shamir)和伦

    相关 RSA解密算法

    一、什么是RSA   RSA[公开密钥密码体制][Link 1]。所谓的公开密钥密码体制就是使用不同的加密密钥与解密密钥,是一种“由已知加密密钥推导出解密密钥在计算上是不