• java.security.*篇(1) RSA 加密与解密demo


    下一篇介绍《java.security.*篇(2) RSA签名与校验demo》 

    涉及类:KeyPairGenerator KeyPair SAPrivateKey RSAPublicKey X509EncodedKeySpec X509EncodedKeySpec PKCS8EncodedKeySpec Cipher

    rsa 公钥加密,私钥解密常用使用场景
    
     1.客户端-服务端通讯发送消息,客户端消息公钥加密,服务端私钥解密
     2.机密文件加密存储,服务端解密在线显示
     3.机密数据库数据内容加密存储,服务端解密显示
     4.文章关键页加密,付费后服务端解密查看等等

    rsa 公钥加密私钥解密demo

    1. commons-codec
    2. commons-codec
    3. 1.11

    公钥加密私钥解密测试方法

    1. private static final String ALGORITHM_NAME = "RSA";
    2. public static final String CHARSET = "UTF-8";

    1. /**
    2. * @description: demos of jdk8 java.security KeyPairGenerator KeyPair
    3. * RSAPrivateKey RSAPublicKey X509EncodedKeySpec X509EncodedKeySpec PKCS8EncodedKeySpec Cipher
    4. * ras public_key encode and private_key decode 用ras 公钥加密 私钥解密
    5. */
    6. @Test
    7. public void testPublicEncryptAndPrivateDecrypt() throws Exception {
    8. String originMessage = "需要加密的字符串";
    9. // KeyPairGenerator类用于生成公钥和私钥对,基于RSA算法生成对象
    10. KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
    11. // 初始化生成器大小
    12. keyPairGenerator.initialize(1024);
    13. // 生成密钥对
    14. KeyPair keyPair = keyPairGenerator.generateKeyPair();
    15. // 获取私钥
    16. PrivateKey privateKey = keyPair.getPrivate();
    17. // 获取公钥
    18. PublicKey publicKey = keyPair.getPublic();
    19. // 公钥字符串
    20. String publicKeyStr = Base64.encodeBase64URLSafeString(publicKey.getEncoded());
    21. // 私钥字符串
    22. String privateKeyStr =Base64.encodeBase64URLSafeString(privateKey.getEncoded());
    23. System.out.println("public key str is:"+ publicKeyStr);
    24. System.out.println("private key str is:"+ privateKeyStr);
    25. // 加密后bytes
    26. byte[] encryptedBytes = publicKeyEncrypt(originMessage,publicKeyStr);
    27. // 解密后bytes
    28. byte[] decryptedBytes = privateKeydecrypt(encryptedBytes,privateKeyStr);
    29. //输出加密后字符串
    30. System.out.println("public encrypted str:"+ new String(encryptedBytes));
    31. //输出解密后内容
    32. System.out.println("private decrypted str:"+ new String(decryptedBytes,CHARSET));
    33. }

    公钥加密方法

    1. /**
    2. * @description public key encrypt
    3. * 公钥加密:实际情况一般是私钥和公钥是提前生成好的,我们需要读取配置文件或者输入值获取公钥私钥字符串去加密解密,因为传入参数需要是字符串,
    4. * privateKey publicKey 对象进行后续操作
    5. **/
    6. public byte[] publicKeyEncrypt(String message ,String publicKeyStr) throws NoSuchAlgorithmException, InvalidKeySpecException, InvalidKeyException, NoSuchPaddingException, BadPaddingException, IllegalBlockSizeException, UnsupportedEncodingException {
    7. // 初始化rsa密钥factory
    8. KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM_NAME);
    9. // 根据公钥字符串初始化X509的keySpec对象
    10. X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(Base64.decodeBase64(publicKeyStr));
    11. // 根据keySpec初始化rsa公钥,以及Cipher密码器
    12. RSAPublicKey rsaPublicKey = (RSAPublicKey) keyFactory.generatePublic(x509KeySpec);
    13. Cipher cipher =Cipher.getInstance("RSA");
    14. cipher.init(Cipher.ENCRYPT_MODE,rsaPublicKey);
    15. // 返回加密后的内容
    16. return cipher.doFinal(message.getBytes(CHARSET));
    17. }

    私钥解密方法 

    1. /**
    2. * @description private key decrypt
    3. * 私钥解密:实际情况一般是私钥和公钥是提前生成好的,我们需要读取配置文件或者输入值获取公钥私钥字符串去加密解密,因为传入参数需要是字符串,
    4. * privateKey publicKey 对象进行后续操作
    5. **/
    6. public byte[] privateKeydecrypt(byte[] encryptBytes ,String privateKeyStr) throws NoSuchAlgorithmException, InvalidKeyException, NoSuchPaddingException, BadPaddingException, IllegalBlockSizeException, InvalidKeySpecException {
    7. // 初始化rsa密钥factory
    8. KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM_NAME);
    9. // 根据私钥初始化PKCS8的keySpec对象
    10. PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(Base64.decodeBase64(privateKeyStr));
    11. // 根据keySpec初始化rsa私钥,以及Cipher密码器
    12. RSAPrivateKey privateKey = (RSAPrivateKey) keyFactory.generatePrivate(pkcs8KeySpec);
    13. Cipher cipher =Cipher.getInstance("RSA");
    14. cipher.init(Cipher.DECRYPT_MODE,privateKey);
    15. // 返回解密后的内容
    16. return cipher.doFinal(encryptBytes);
    17. }

  • 相关阅读:
    《存储IO路径》专题:四种IO栈大比武
    30m退耕还湿空间数据集(2000-2010年,西南地区)
    内存泄漏,内存溢出,抽象类和接口,netstat、ping、ifconfig的区别
    Neo4j学习笔记1:增删改查
    21天打卡挑战 - 经典算法之快速排序
    二叉树是什么?有几种遍历方式?怎么实现遍历?看完这篇文章之后相信你会有更好的理解
    Linux 基础指令汇总大全
    【Android】 Glide 官方库自带的图片裁切类
    解决docker运行elastic服务端启动不成功
    Go的优雅退出
  • 原文地址:https://blog.csdn.net/madness1010/article/details/128083078