证书Key直接去换行符存放到就可以更方便,如果需要做安全的话可以把字段放到so层比较好
public class RSAUtil {
public static final String KEY_ALGORITHM = "RSA";
public static final String SIGNATURE_ALGORITHM = "MD5withRSA";
private static final int KEY_SIZE = 1024;
private static final String PUBLIC_KEY = "RSAPublicKey";
private static final String PRIVATE_KEY = "RSAPrivateKey";
public static String str_pubK = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAs6lJ8f52yfY4FlQDkpBBHcIdd2GH+fdjvQaI28Hmhzvz+6vc/cfkoc7Gk7n55aXnpu28/2HNk3i8Yw8HXR7hwkqFn5Yw8X9qmui11dk2RYO86WLYIzP8sqQuMpZQkF/qnAxAzbWCNA0sXCIjF4qBm3tw06CmJ2X2rr21WoAcBy6zCLuyU/VJOfVWaUMlnIvZqGZRXmSU6JBS5nnk06o4YG91ZWasWZzKQ9lNycsL7KUbxQeO6OElZaFY3FmVQP8bmvtmVL66bH5h6T4Rj5+Tz1EMea3Dl3o7QMXdIX2WBVQo5nte1jKzC4rWfBqcgzFN3kcSQVMfj+uucAK82uwmEQIDAQAB";
public static String str_priK = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCoYmgZgevtZmYrvX9F3rHqSrfpDr+/Gh72zBcAQaTNi/driNQ9fAeyjgs4H2TDDWHpJi2/6Itm21kBCe8p6ylKrAWJMcM+fD476dDqQrm/GHYoUtAWGhBiErC++NOGgDHpwFcDh8uQkHVH6+CJjoRI8xbD15Uba4RMbdRQsMyDHmTi3Zpvj8WKJBZ5Gp/rJHYGQCQlR7pinQdXLvhSlsROgJAUq6KK9NbS/Y7fP5ImwkXi/U5qkMaWWU9XlL3MhX+YRKgCK2B92R1XBKL99UlixHigBtLMIuFdvzyPfhZ66pq8byuqk7jyR2VSEzsTG3PjPnhJVf/Zyuyp/XWJrFBHAgMBAAECggEAIOB16uUPJ58iu8+YFhBRNKm6FusmTS3QdeeLZiVrBCcb/AcPSaH57scFMcQP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NmHvDVqUoJ4C4yiZzzIMXHaMdM40D+WgBVay8N6fd1XK4rst92fYR86bE5kCGPXXQ9";
/**
* 得到公钥
*
* @param key 密钥字符串(经过base64编码)
* @throws Exception
*/
public static PublicKey getPublicKey(String key) throws Exception {
byte[] keyBytes;
keyBytes = (new BASE64Decoder()).decodeBuffer(key);
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey publicKey = keyFactory.generatePublic(keySpec);
return publicKey;
}
/**
* 得到私钥
*
* @param key 密钥字符串(经过base64编码)
* @throws Exception
*/
public static PrivateKey getPrivateKey(String key) throws Exception {
byte[] keyBytes;
keyBytes = (new BASE64Decoder()).decodeBuffer(key);
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
return privateKey;
}
//***************************签名和验证*******************************
public static byte[] sign(byte[] data) throws Exception {
PrivateKey priK = getPrivateKey(str_priK);
Signature sig = Signature.getInstance(SIGNATURE_ALGORITHM);
sig.initSign(priK);
sig.update(data);
return sig.sign();
}
public static boolean verify(byte[] data, byte[] sign) throws Exception {
PublicKey pubK = getPublicKey(str_pubK);
Signature sig = Signature.getInstance(SIGNATURE_ALGORITHM);
sig.initVerify(pubK);
sig.update(data);
return sig.verify(sign);
}
//************************加密解密**************************
public static byte[] encrypt(byte[] bt_plaintext) throws Exception {
PublicKey publicKey = getPublicKey(str_pubK);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] bt_encrypted = cipher.doFinal(bt_plaintext);
return bt_encrypted;
}
public static byte[] decrypt(byte[] bt_encrypted) throws Exception {
PrivateKey privateKey = getPrivateKey(str_priK);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
// byte[] result;
// byte[] inputArray = bt_encrypted;
// int inputLength = inputArray.length;
// System.out.println("加密字节数:" + inputLength);
// int MAX_ENCRYPT_BLOCK = 256;
// // 标识
// int offSet = 0;
// byte[] resultBytes = {};
// byte[] cache = {};
// while (inputLength - offSet > 0) {
// if (inputLength - offSet > MAX_ENCRYPT_BLOCK) {
// cache = cipher.doFinal(inputArray, offSet, MAX_ENCRYPT_BLOCK);
// offSet += MAX_ENCRYPT_BLOCK;
// } else {
// cache = cipher.doFinal(inputArray, offSet, inputLength - offSet);
// offSet = inputLength;
// }
// resultBytes = Arrays.copyOf(resultBytes, resultBytes.length + cache.length);
// System.arraycopy(cache, 0, resultBytes, resultBytes.length - cache.length, cache.length);
// }
// result = Base64.encodeBase64(resultBytes);
byte[] bt_original = cipher.doFinal(bt_encrypted);
return bt_original;
}
//********************main函数:加密解密和签名验证*********************
public static void main(String[] args) throws Exception {
// String str_plaintext = "这是一段用来测试密钥转换的明文";
// System.err.println("明文:" + str_plaintext);
// byte[] bt_cipher = encrypt(str_plaintext.getBytes());
// System.out.println("加密后:" + Base64.encodeBase64String(bt_cipher));
//
//
byte[] bt_original = decrypt(Base64.decodeBase64("eaOm2Vf2syg5Kc+OkEXnbRKW1MWdJnkCX5tjlAygDNIpQm1LfFK57OQ/fNK3lSBs7DSVm8SFkhrsOwb/zhW749xo+IzZ9VrCiiYtPfo4zEb9BrlXUXxgee943gOXHTukapATvxKUTWtjEJOw/WABP0t1KON4yWZVntPpvbwh1VTO5WIWvbF8r/jBHORfNqqHaPsG+j/G7z/4RQ9Xnvaak/3BHSA55aYC0r9c8YzYLWxhm0aiSyp+DGY77d80LdpGtqfv1J4e3g+joBeuJLn+qbAsu86uv63XaOZBI2OwBcJksKUPTcvcdppTQZvDGQmOmNrddqOdqBVUYqF582uRfA=="));
String str_original = new String(bt_original);
System.out.println("解密结果:" + str_original);
//
// String str = "被签名的内容";
// System.err.println("\n原文:" + str);
// byte[] signature = sign(str.getBytes());
// System.out.println("产生签名:" + Base64.encodeBase64String(signature));
// boolean status = verify(str.getBytes(), signature);
// System.out.println("验证情况:" + status);
}
}