首页 / JAVA / Java非对称加密解密
Java非对称加密解密
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Java非对称加密解密,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4860字,纯文字阅读大概需要7分钟。
内容图文
![Java非对称加密解密](/upload/InfoBanner/zyjiaocheng/602/e7d6cf8951cb47748cecf99a63aea952.jpg)
从微信公众号摘抄,稍加改动,收藏
另外https://www.jianshu.com/p/048be4864559 写得挺好
import lombok.Data; import javax.crypto.Cipher; import java.security.*; import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.X509EncodedKeySpec; import java.util.Base64; public class SignatureUtil { /** * 生成公钥和私钥 * * @return * @throws NoSuchAlgorithmException */ public static Keys generateKey() throws NoSuchAlgorithmException { Keys resultMap = new Keys(); KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); KeyPair keyPair = keyPairGenerator.generateKeyPair(); Base64.Encoder encoder = Base64.getEncoder(); resultMap.setPrivateKey(encoder.encodeToString(keyPair.getPrivate().getEncoded())); resultMap.setPublicKey(encoder.encodeToString(keyPair.getPublic().getEncoded())); return resultMap; } /** * RSA加密-pub-en,pri-de * * @param key * @param content * @return * @throws Exception */ public static String rsaEncrypt(String key, String content) throws Exception { KeyFactory keyFactory = KeyFactory.getInstance("RSA"); Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.ENCRYPT_MODE, keyFactory.generatePublic(new X509EncodedKeySpec(Base64.getDecoder().decode(key)))); byte[] bytes = cipher.doFinal(content.getBytes()); return Base64.getEncoder().encodeToString(bytes); } /** * RSA解密-pub-en,pri-de * * @param key * @param content * @return * @throws Exception */ public static String rsaDecrypt(String key, String content) throws Exception { Cipher cipher = Cipher.getInstance("RSA"); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); cipher.init(Cipher.DECRYPT_MODE, keyFactory.generatePrivate(new PKCS8EncodedKeySpec(Base64.getDecoder().decode(key)))); byte[] bytes = cipher.doFinal(Base64.getDecoder().decode(content)); return new String(bytes); } /** * RSA加密-pri-en,pub-de * * @param key * @param content * @return * @throws Exception */ public static String rsaEncryptReverse(String key, String content) throws Exception { KeyFactory keyFactory = KeyFactory.getInstance("RSA"); Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.ENCRYPT_MODE, keyFactory.generatePrivate(new PKCS8EncodedKeySpec(Base64.getDecoder().decode(key)))); byte[] bytes = cipher.doFinal(content.getBytes()); return Base64.getEncoder().encodeToString(bytes); } /** * RSA解密-pri-en,pub-de * * @param key * @param content * @return * @throws Exception */ public static String rsaDecryptReverse(String key, String content) throws Exception { Cipher cipher = Cipher.getInstance("RSA"); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); cipher.init(Cipher.DECRYPT_MODE, keyFactory.generatePublic(new X509EncodedKeySpec(Base64.getDecoder().decode(key)))); byte[] bytes = cipher.doFinal(Base64.getDecoder().decode(content)); return new String(bytes); } /** * 私钥签名 * * @param privateKeyStr * @param content * @return * @throws Exception */ public static String generateSignature(String privateKeyStr, String content) throws Exception { KeyFactory keyFactory = KeyFactory.getInstance("RSA"); Signature signature = Signature.getInstance("SHA1withRSA"); PrivateKey privateKey = keyFactory.generatePrivate(new PKCS8EncodedKeySpec(Base64.getDecoder().decode(privateKeyStr))); signature.initSign(privateKey); signature.update(content.getBytes()); return Base64.getEncoder().encodeToString(signature.sign()); } /** * 公钥验证 * * @param publicKeyStr * @param content * @param sign * @return * @throws Exception */ public static boolean verifySignature(String publicKeyStr, String content, String sign) throws Exception { KeyFactory keyFactory = KeyFactory.getInstance("RSA"); Signature signature = Signature.getInstance("SHA1withRSA"); PublicKey publicKey = keyFactory.generatePublic(new X509EncodedKeySpec(Base64.getDecoder().decode(publicKeyStr))); signature.initVerify(publicKey); signature.update(content.getBytes()); return signature.verify(Base64.getDecoder().decode(sign)); } public static void main(String[] args) throws Exception { String content = "大王叫我来巡山呐"; //生成密钥对 Keys keys = generateKey(); //公钥加密,私钥解密 String publicKeyData = rsaEncrypt(keys.publicKey, content); System.out.println("公钥加密:" + publicKeyData); System.out.println("私钥解密:" + rsaDecrypt(keys.privateKey, publicKeyData)); //私钥加密,公钥解密 String privateKeyData = rsaEncryptReverse(keys.privateKey, content); System.out.println("私钥加密:" + privateKeyData); System.out.println("公钥解密:" + rsaDecryptReverse(keys.publicKey, privateKeyData)); //私钥签名 String sign = generateSignature(keys.privateKey, content); System.out.println("私钥签名:" + sign); //公钥验证 boolean verifyResult = verifySignature(keys.publicKey, content, sign); System.out.println("公钥验证:" + verifyResult); //将内容做下修改,再进行公钥验证 boolean verifyResult2 = verifySignature(keys.publicKey, content + "啦啦啦啦", sign); System.out.println("公钥验证:" + verifyResult2); } @Data public static class Keys { public String publicKey; public String privateKey; } }
内容总结
以上是互联网集市为您收集整理的Java非对称加密解密全部内容,希望文章能够帮你解决Java非对称加密解密所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。