PHP和Java的RSA加密互通
一、利用OpenSSL生成RSA公私钥
1.生成RSA私钥
openssl genrsa -out rsa_private_key.pem 1024
示例:
-----BEGIN RSA PRIVATE KEY-----MIICXAIBAAKBgQC19+3Zkg8ko4S7XeAjGl2ps8dEVGx2prFAAsq9OeNjvI4zbUG2iw7fvk02VZuilYyspB/MR1nMEWreVj21FdnN/szIlC/stptlNMtmkZ28jv8QVvls8O2Zp97qDxSWbYwZFT1nmQVK1uSZV7wMEldWTSlFcLuOXoFGGXndO9062QIDAQABAoGASoBHkVyLdqS8Izo8GiMhVemVHBS0k5+L0nlSKEcbIiqAze1dii9E17ZCRoymO9qezdAkdK6BxVscNgt5GDrqAQvS6LmQ1KQfOF2mr3rNeCjz6RLl4ujl9mjwAikGHRWBev7Dz9q/YjKg4OaFTTT5HcgnuyLzO2DY8rKA1PM3gkECQQDlPtu0N4gZJe2/BSqEFwLLme+lQP8d1NNoICAP5U+GowUc885AMMWqOObOUVe/PzaSSObyAsRTht2HOCalqWh/AkEAyzSUXpThSShB6JQxDC52r+SS9342bEa6z3vSJ2gBfIZObxA25jHSF7pRh/vXpGXzcsV4fzIDGKz6tEPHfh3wpwJBANUwnrs7RWs1taKGWGKcz7Guh4nkJxyD9tKHxalitJFd+3xQU4eok7pYznQie3rUe5iRCY0Y+6E987gzhOVc5VsCQFNs4MT75on8ZyKvRHu1z7Bi7RuCy6EkYKmyMhNPldyj3yulwoQ7S//F1Jc5g8zQtmQWQmQmCjNlQQAlG4/hht0CQByUzQ2Vj9uB/RXMvPWtGimEENaAO5Q2wF5kWnoyfg8U8DjPPoFe0B9mWxAjPBCnI3UwW3P7B7TZKrAlf+GbtxE=-----END RSA PRIVATE KEY-----
-----BEGIN PUBLIC KEY-----MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC19+3Zkg8ko4S7XeAjGl2ps8dEVGx2prFAAsq9OeNjvI4zbUG2iw7fvk02VZuilYyspB/MR1nMEWreVj21FdnN/szIlC/stptlNMtmkZ28jv8QVvls8O2Zp97qDxSWbYwZFT1nmQVK1uSZV7wMEldWTSlFcLuOXoFGGXndO9062QIDAQAB-----END PUBLIC KEY-----
-----BEGIN PRIVATE KEY-----MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBALX37dmSDySjhLtd4CMaXamzx0RUbHamsUACyr0542O8jjNtQbaLDt++TTZVm6KVjKykH8xHWcwRat5WPbUV2c3+zMiUL+y2m2U0y2aRnbyO/xBW+Wzw7Zmn3uoPFJZtjBkVPWeZBUrW5JlXvAwSV1ZNKUVwu45egUYZed073TrZAgMBAAECgYBKgEeRXIt2pLwjOjwaIyFV6ZUcFLSTn4vSeVIoRxsiKoDN7V2KL0TXtkJGjKY72p7N0CR0roHFWxw2C3kYOuoBC9LouZDUpB84Xaaves14KPPpEuXi6OX2aPACKQYdFYF6/sPP2r9iMqDg5oVNNPkdyCe7IvM7YNjysoDU8zeCQQJBAOU+27Q3iBkl7b8FKoQXAsuZ76VA/x3U02ggIA/lT4ajBRzzzkAwxao45s5RV78/NpJI5vICxFOG3Yc4JqWpaH8CQQDLNJRelOFJKEHolDEMLnav5JL3fjZsRrrPe9InaAF8hk5vEDbmMdIXulGH+9ekZfNyxXh/MgMYrPq0Q8d+HfCnAkEA1TCeuztFazW1ooZYYpzPsa6HieQnHIP20ofFqWK0kV37fFBTh6iTuljOdCJ7etR7mJEJjRj7oT3zuDOE5VzlWwJAU2zgxPvmifxnIq9Ee7XPsGLtG4LLoSRgqbIyE0+V3KPfK6XChDtL/8XUlzmDzNC2ZBZCZCYKM2VBACUbj+GG3QJAHJTNDZWP24H9Fcy89a0aKYQQ1oA7lDbAXmRaejJ+DxTwOM8+gV7QH2ZbECM8EKcjdTBbc/sHtNkqsCV/4Zu3EQ==-----END PRIVATE KEY-----
/** * 本方法使用SHA1withRSA签名算法产生签名 * @param String priKey 签名时使用的私钥(16进制编码) * @param String src 签名的原字符串 * @return String 签名的返回结果(16进制编码)。当产生签名出错的时候,返回null。 */ public static String generateSHA1withRSASigature(String priKey, String src) { try { Signature sigEng = Signature.getInstance("SHA1withRSA"); byte[] pribyte = UtilString.hexStrToBytes(priKey.trim()); PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(pribyte); KeyFactory fac = KeyFactory.getInstance("RSA"); RSAPrivateKey privateKey = (RSAPrivateKey) fac.generatePrivate(keySpec); sigEng.initSign(privateKey); sigEng.update(src.getBytes()); byte[] signature = sigEng.sign(); return UtilString.bytesToHexStr(signature); } catch (Exception e) { e.printStackTrace(); return null; } }
/** * 本方法使用SHA1withRSA签名算法验证签名 * @param String pubKey 验证签名时使用的公钥(16进制编码) * @param String sign 签名结果(16进制编码) * @param String src 签名的原字符串 * @return String 签名的返回结果(16进制编码) */ public static boolean verifySHA1withRSASigature(String pubKey, String sign, String src) { try { Signature sigEng = Signature.getInstance("SHA1withRSA"); byte[] pubbyte = UtilString.hexStrToBytes(pubKey.trim()); X509EncodedKeySpec keySpec = new X509EncodedKeySpec(pubbyte); KeyFactory fac = KeyFactory.getInstance("RSA"); RSAPublicKey rsaPubKey = (RSAPublicKey) fac.generatePublic(keySpec); sigEng.initVerify(rsaPubKey); sigEng.update(src.getBytes()); byte[] sign1 = UtilString.hexStrToBytes(sign); return sigEng.verify(sign1); } catch (Exception e) { e.printStackTrace(); //LogMan.log("[NeteaseSignUtil][verifySHA1withRSASigature]"+e); return false; } }
package com.nloan.cmis.pub.utils;public class UtilString { private static final char[] bcdLookup = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; /** * 将字节数组转换为16进制字符串的形式. */ public static final String bytesToHexStr(byte[] bcd){ StringBuffer s = new StringBuffer(bcd.length * 2); for (int i = 0; i >> 4) & 0x0f]); s.append(bcdLookup[bcd[i] & 0x0f]); } return s.toString(); } /** * 将16进制字符串还原为字节数组. */ public static final byte[] hexStrToBytes(String s) { byte[] bytes; bytes = new byte[s.length() / 2]; for (int i = 0; i <br><br><br><br><br>文档包中的key是配套key值,可直接使用,其他的文件有示例方法,可做参考。<br>网络参考地址:<br>http://blog.csdn.net/clh604/article/details/20224735<br>http://blog.csdn.net/chaijunkun/article/details/7275632<br><br><div class="clear"> </div>