public class RSAUtil {
// 公钥
public static final String RSA_PUBLIC = "******************************...";
private static final String ALGORITHM = "RSA";
/**
* 使用公钥加密
*
* @param content 例 : age/24
* @return
*/
public static String encryptByPublic(Context context, String content)
{
try
{
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(com.wanzheng.driver.util.Base64.decode(RSA_PUBLIC));
// PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec(com.wanzheng.driver.util.Base64.decode(RSA_PUBLIC));
KeyFactory keyf = KeyFactory.getInstance(ALGORITHM);
PublicKey publicKey = keyf.generatePublic(keySpec);
// PublicKey publicKey = getPublicKeyFromX509(ALGORITHM, pubKey);
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] plaintext = content.getBytes();
byte[] output = cipher.doFinal(plaintext);
String str = Base64.encodeToString(output, Base64.DEFAULT);
String s1=str.replace("=","");
String s2=s1.replace("+","-");
String s3=s2.replace("/","_");
return s3;
} catch (Exception e)
{
e.printStackTrace();
return null;
}
}
}