1 2 3 4 5 6 7 8 9 10 11 12 13 14
| /** * 加密 * @params str 要加密的字符串 */ public static String Encrypt(String str) throws Exception { // base64编码的公钥 byte[] decoded = Base64.decode(pubKey.getBytes(), Base64.DEFAULT); RSAPublicKey rsaPublicKey = (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(decoded)); // RSA加密 Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); cipher.init(Cipher.ENCRYPT_MODE, rsaPublicKey); String outStr = Base64.encodeToString(cipher.doFinal(str.getBytes()), Base64.DEFAULT); return outStr; }
|