RSA加密算法
2021/7/19 9:34:47
本文主要是介绍RSA加密算法,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
RSA算法类:
import org.apache.tomcat.util.codec.binary.Base64; import javax.crypto.Cipher; import java.nio.charset.StandardCharsets; import java.security.*; import java.security.interfaces.RSAPrivateKey; import java.security.interfaces.RSAPublicKey; import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.X509EncodedKeySpec; import java.util.HashMap; import java.util.Map; public class RSAEncrypt { private static final int KEY_SIZE = 512; private static final String RSA_ALGORITHM = "RSA"; /** * 随机生成密钥对 * @return 密钥对map */ public static Map<String, String> getKeyPair() { KeyPairGenerator keyPairGenerator = null; try { keyPairGenerator = KeyPairGenerator.getInstance(RSA_ALGORITHM); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } keyPairGenerator.initialize(KEY_SIZE); KeyPair keyPair = keyPairGenerator.generateKeyPair(); RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); HashMap<String, String> keyPairMap = new HashMap<>(); keyPairMap.put("publicKey", encodeBase64(publicKey.getEncoded())); keyPairMap.put("privateKey", encodeBase64(privateKey.getEncoded())); return keyPairMap; } /** * RSA加密 * @param str 需要加密的字符串 * @param publicKey 加密的公钥 */ public static String encrypt(String str, String publicKey) { byte[] publicKeyBytes = decodeBase46(publicKey); String outStr = null; try { KeyFactory keyFactory = KeyFactory.getInstance(RSA_ALGORITHM); PublicKey pubKey = keyFactory.generatePublic(new X509EncodedKeySpec(publicKeyBytes)); Cipher cipher = Cipher.getInstance(RSA_ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, pubKey); byte[] bytes = cipher.doFinal(str.getBytes(StandardCharsets.UTF_8)); outStr = encodeBase64(bytes); } catch (Exception e) { e.printStackTrace(); } return outStr; } /** * * @param str 需要解码的字符串(是经过base64编码的) * @param privateKey 解码用的私钥 * @return */ public static String decrypt(String str, String privateKey) { byte[] bytes = decodeBase46(str); byte[] privateKeyBytes = decodeBase46(privateKey); String outStr = null; try { KeyFactory instance = KeyFactory.getInstance(RSA_ALGORITHM); PrivateKey priKey = instance.generatePrivate(new PKCS8EncodedKeySpec(privateKeyBytes)); Cipher cipher = Cipher.getInstance(RSA_ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, priKey); outStr = new String(cipher.doFinal(bytes)); } catch (Exception e) { e.printStackTrace(); } return outStr; } /** * base64编码 * @param encoded 需要编码的字节数组 * @return base64字符串 */ private static String encodeBase64(byte[] encoded) { return Base64.encodeBase64String(encoded); } /** * base64解码成字节数组 * @param base64String base64字符串 * @return 解码出的字节数组 */ private static byte[] decodeBase46(String base64String) { return Base64.decodeBase64(base64String); } }
介绍:
这篇关于RSA加密算法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2025-01-01成为百万架构师的第一课:设计模式:Spring中的设计模式
- 2025-01-01一个基于注解驱动的可视化的DDD架构-超越COLA的设计
- 2025-01-01PlantUML 时序图 基本例子
- 2025-01-01plantuml 信号时序图
- 2025-01-01聊聊springboot项目如何优雅进行数据校验
- 2024-12-31自由职业者效率提升指南:3个时间管理技巧搞定多个项目
- 2024-12-31适用于咨询行业的项目管理工具:提升跨团队协作和工作效率的最佳选择
- 2024-12-31高效协作的未来:2024年实时文档工具深度解析
- 2024-12-31商务谈判者的利器!哪 6 款办公软件能提升春节合作成功率?
- 2024-12-31小团队如何选择最实用的项目管理工具?高效协作与任务追踪指南