坑点:
用其他语言的一定注意,Java秘钥和php秘钥不同,不用混用。
package cn.muyan.monitoragent.utils.encrypt; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; import javax.crypto.Cipher; import java.io.IOException; import java.security.*; import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.X509EncodedKeySpec; /*** * RSA加解密操作 */ public class RSAUtils { // 加密算法 private static String ALGORITHM = "RSA"; // 加签算法 private static String SIGN_ALGORITHM = "MD5withRSA"; private static String CHARSET = "utf8"; /*** * 生成秘钥对 * @return * @throws Exception */ public static KeyPair getKeyPair() throws Exception { KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(ALGORITHM); keyPairGenerator.initialize(2048); KeyPair keyPair = keyPairGenerator.generateKeyPair(); return keyPair; } /*** * 获取公钥(Base64编码) * @param keyPair * @return */ public static String getPublicKey(KeyPair keyPair) { PublicKey publicKey = keyPair.getPublic(); byte[] bytes = publicKey.getEncoded(); return byte2Base64(bytes); } /*** * 获取私钥(Base64编码) * @param keyPair * @return */ public static String getPrivateKey(KeyPair keyPair) { PrivateKey privateKey = keyPair.getPrivate(); byte[] bytes = privateKey.getEncoded(); return byte2Base64(bytes); } /*** * 将Base64编码后的公钥转换成PublicKey对象 * @param pubStr * @return * @throws Exception */ public static PublicKey string2PublicKey(String pubStr) throws Exception { byte[] keyBytes = base642Byte(pubStr); X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM); PublicKey publicKey = keyFactory.generatePublic(keySpec); return publicKey; } /*** * 将Base64编码后的私钥转换成PrivateKey对象 * * @param priStr * @return * @throws Exception */ public static PrivateKey string2PrivateKey(String priStr) throws Exception { byte[] keyBytes = base642Byte(priStr); PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM); PrivateKey privateKey = keyFactory.generatePrivate(keySpec); return privateKey; } /*** * 公钥加密 * @param base64Key * @param publicKey * @return * @throws Exception */ public static String publicEncrypt(String base64Key, PublicKey publicKey) throws Exception { byte[] content = base64Key.getBytes(); Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, publicKey); byte[] bytes = cipher.doFinal(content); return byte2Base64(bytes); } /*** * 公钥解密 * @param base64Key * @param publicKey * @return * @throws Exception */ public static String publicDecrypt(String base64Key, PublicKey publicKey) throws Exception { byte[] content = base642Byte(base64Key); Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, publicKey); byte[] bytes = cipher.doFinal(content); return new String(bytes, CHARSET); } /*** * 私钥解密 * @param base64Key * @param privateKey * @return * @throws Exception */ public static String privateDecrypt(String base64Key, PrivateKey privateKey) throws Exception { byte[] content = base642Byte(base64Key); Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, privateKey); byte[] bytes = cipher.doFinal(content); return new String(bytes, CHARSET); } /*** * 私钥加密 * @param base64Key * @param privateKey * @return * @throws Exception */ public static String privateEncrypt(String base64Key, PrivateKey privateKey) throws Exception { byte[] content = base64Key.getBytes(); Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, privateKey); byte[] bytes = cipher.doFinal(content); return byte2Base64(bytes); } /** * 私钥签名 * * @param data 待签名数据 * @param privateKey 私钥 * @return 签名 */ public static String sign(String data, PrivateKey privateKey) throws Exception { byte[] keyBytes = privateKey.getEncoded(); PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM); PrivateKey key = keyFactory.generatePrivate(keySpec); Signature signature = Signature.getInstance(SIGN_ALGORITHM); signature.initSign(key); signature.update(data.getBytes()); return byte2Base64(signature.sign()); } /** * 公钥验签 * * @param srcData 原始字符串 * @param publicKey 公钥 * @param sign 签名 * @return 是否验签通过 */ public static boolean verify(String srcData, PublicKey publicKey, String sign) throws Exception { byte[] keyBytes = publicKey.getEncoded(); X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM); PublicKey key = keyFactory.generatePublic(keySpec); Signature signature = Signature.getInstance(SIGN_ALGORITHM); signature.initVerify(key); signature.update(srcData.getBytes()); return signature.verify(base642Byte(sign)); } /*** * 字节数组转Base64编码 * * @param bytes * @return */ public static String byte2Base64(byte[] bytes) { BASE64Encoder encoder = new BASE64Encoder(); return encoder.encode(bytes); } /*** * Base64编码转字节数组 * @param base64Key * @return * @throws IOException */ public static byte[] base642Byte(String base64Key) throws IOException { BASE64Decoder decoder = new BASE64Decoder(); return decoder.decodeBuffer(base64Key); } public static void main(String[] args) { String publicKeyStr = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAkm6NbElYw8dJTjK9e7i+urYfBa5mj5PGkMvlfS3nrGROVfA4kenfVGbrKrTmbJBOaCHwrzpHWC16g4dRUKTyGwPMEGZp1wRfqbCXLQcy7Q8H1YlpC0eUxlg7a9GCMIAPDYil+ANmpcueGi0dHmd/4jNMPpVrjA/T/8vnb+QIeXf2GkWp/5F1zD6D6kstNJIpz9wfLenWHV29eMHO+FZa4fYSPWB04snjRdTeG3QDlfFNfoVL0ZNb6n4+K4uWUNu4u4MwNepXvi2MgSUtNuaSIsfiMgNc26qzcwAD/P5NquMFtDJnS3b9AeX49XAsrLvUScitVXpS7/1Mj0bM/vHeIQIDAQAB"; String privateKeyStr = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCSbo1sSVjDx0lOMr17uL66th8FrmaPk8aQy+V9LeesZE5V8DiR6d9UZusqtOZskE5oIfCvOkdYLXqDh1FQpPIbA8wQZmnXBF+psJctBzLtDwfViWkLR5TGWDtr0YIwgA8NiKX4A2aly54aLR0eZ3/iM0w+lWuMD9P/y+dv5Ah5d/YaRan/kXXMPoPqSy00kinP3B8t6dYdXb14wc74Vlr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0332JQaA386VSmFF+4Pxm+XkR2eaXs25qkMQgUujcvNq5dTLLSd184VNZkdLkq6+A5wJEz0GKzAqmXz8N1mmxUOFBBb+/NB3ZNO1ce39hoL7AoGAevZ5deD/yz/dguxd5tB58MUorFwqM/VLmxQAggXRvR8UusSNdrsa8Zjwzn/WyXJ7wWJ3rFdar6Ml3QMS6d31fxOd3QD01cssYFqL7HqsOcityfuIgsJZl7t7GvUt1YHpn3SuEkbDHvtoWlYwMhtnBeVQmy+RX261c8Rvbu6zXa4="; String byte2Base64 = "ggam2MkjuDZ75jOWGuD+WV5EA+VYVk5kFtlAUxfze2kZ0OlNEaK2c9CgovfTKyhRhKCGQRcFNY+ihvIfHP9wUjOvnM3jqDteIs8Thz1S3aTPGQT8zji8Ze2CObELPuZ9xPnbo+wL9cTIMkA4t9d7xMLfMnmV6irIXVOQ5anYCC+U8NLQtEsshqnzzyOMuRojqG+MBWEpe0X9LqFSIzdxVegtwZh9eJ1Zmw9VkvotBs1YnQqF/ivRiX2y6ZB8L7nZwCcS65GQq4ZARcVr5I3PN36p9dznjQguFKGxd3pftmupqoB7w51KzzYpZNfOzGYHM/sHDagUjYQWZL1fsBMnIg=="; try { // KeyPair keyPair = getKeyPair(); // String publicKeyStr = getPublicKey(keyPair); // String privateKeyStr = getPrivateKey(keyPair); // System.out.println("RSA公钥Base64编码:" + publicKeyStr); // System.out.println("RSA私钥Base64编码:" + privateKeyStr); String message = "123456"; ////// //将Base64编码后的公钥转换成PublicKey对象 PublicKey publicKey = string2PublicKey(publicKeyStr); //用公钥加密 String publicEncrypt = publicEncrypt(message, publicKey); //加密后的内容Base64编码 //byte2Base64 = byte2Base64(publicEncrypt); System.out.println("公钥加密并Base64编码的结果:" + publicEncrypt); //将Base64编码后的私钥转换成PrivateKey对象 PrivateKey privateKey = string2PrivateKey(privateKeyStr); //加密后的内容Base64解码 // byte[] base642Byte = base642Byte(byte2Base64); //用私钥解密 String privateDecrypt = privateDecrypt(publicEncrypt, privateKey); //解密后的明文 System.out.println("解密后的明文: " + privateDecrypt); // 签名 // String signStr = sign("11111111", privateKey); // System.out.println("签名: " + signStr); } catch (Exception e) { e.printStackTrace(); System.out.println(e); } } }
标签:return,String,加解密,rsa,param,static,java,byte,throws From: https://www.cnblogs.com/yangzailu/p/16865968.html