这是我第一篇博客,是因为后台传过来16位的模数和指数让前段自己生成秘钥并进行加密 ,网上有很多类似的的功能, 我看过很多大神写的资料,才做成功,废话不说看代码
好久没看了 看有不少人看这个博客 如果模数和指数加密的操作 这些代码复制过去就可以用了
Log.e("aaaaa",RSAjiami("123456789")) ;
jiemi(RSAjiami("123456789")+"");
String s1="";
public String RSAjiami(String context) {
KeyPairGenerator keyPairGen = null;
String s = "qq1991100788";//需要加密的参数
String pe = "10001";//指数
String modulus = "cd30d855dcfd357ff6a1fae0773383aa4c0ea1579e1ddb94eb1ed07f9dc5893a0abc5400600eeb037caa9e490c758aeaaaf9fdd65f7206ec58f3e88a0b215f38f9d3b9dc5149d1e57ebfea719365289f84f1958af5d2096a94bb0c843a0a389a125af222e514da6a00dc2b7501c38dcaf30b1d309abcc73dd199074ee207e38f";//模数
try {
keyPairGen = KeyPairGenerator.getInstance("RSA");
keyPairGen.initialize(1024, new SecureRandom());
KeyPair keyPair = keyPairGen.generateKeyPair();
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
// String PrivateExponent = privateKey.getPrivateExponent().toString(16);//公钥
// KLog.e("TAs", PrivateExponent);//
//s1 = encryptByPublicKey(s.getBytes(), PrivateExponent);
BigInteger b1 = publicKey.getModulus();
BigInteger b2 = publicKey.getPublicExponent();
RSAPublicKeySpec keySpec = new RSAPublicKeySpec(new BigInteger(modulus, 16), new BigInteger(pe, 16));
KeyFactory factory = KeyFactory.getInstance("RSA");
publicKey = (RSAPublicKey) factory.generatePublic(keySpec);
// PublicKey a = (RSAPublicKey) factory.generatePublic(keySpec);
// KLog.e("TAG", a.getClass().getName());
// 对数据加密
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] encryptedData = cipher.doFinal(s.getBytes());
String encryptedString = Base64Coder.encodeLines(encryptedData);//加密后的密码 密文
System.out.println("11111:
" + encryptedString);
StringBuffer sb = new StringBuffer(encryptedString);
System.out.println("加密后:
" + sb.toString());
s1 = sb.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return s1;
}
解密
public String void jiemi(String context){
Log.e("aaaaa",context);//加密过的字符串
KeyPairGenerator keyPairGen = null;
KeyFactory factory = null;
try {
String modulus = "cd30d855dcfd357ff6a1fae0773383aa4c0ea1579e1ddb94eb1ed07f9dc5893a0abc5400600eeb037caa9e490c758aeaaaf9fdd65f7206ec58f3e88a0b215f38f9d3b9dc5149d1e57ebfea719365289f84f1958af5d2096a94bb0c843a0a389a125af222e514da6a00dc2b7501c38dcaf30b1d309abcc73dd199074ee207e38f";//模数
BigInteger bigIntPrivateExponent = new BigInteger(publicExponent);
// BigInteger bigIntPrivateExponent = new BigInteger(publicExponent);//publicExponent 是私钥 下面这句是跟公钥对应的 用之前更换对应私钥
String PrivateExponent = "c9917e337b0fd30a8dc1b9addfeb5f205feecf583f1f9da1c1075852c52e540c0c97ccfd415fe6465aa55130f9684ebb1092dc654705c0ff54b2e6711556072f32f9896020fc53846296b62fb1d750216d6e4be5408794f7f2982f9300bd93fa5be25a2b74f111c2422542dee7f50c07dcfd7b5fcc9f3cceab7657945bb7e1e1";
keyPairGen = KeyPairGenerator.getInstance("RSA");
factory = KeyFactory.getInstance("RSA");
keyPairGen = KeyPairGenerator.getInstance("RSA");
keyPairGen.initialize(1024, new SecureRandom());
KeyPair keyPair = keyPairGen.generateKeyPair();
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
RSAPrivateKeySpec pkspec = new RSAPrivateKeySpec(new BigInteger(modulus, 16), new BigInteger(PrivateExponent, 16));
privateKey = (RSAPrivateKey) factory.generatePrivate(pkspec);
byte[] encryptedDatass = Base64Coder.decodeLines(context);
byte[] privatemima = decryptData(encryptedDatass, privateKey);
Log.e("aaaaaaaaaaaa",new String(privatemima));
return new String(privatemima)+"";
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (InvalidKeySpecException e) {
e.printStackTrace();
}
return "失败";
}
以上是加密解密的代码 现在两个是工具类
/**
* 用私钥解密
*
* @param encryptedData
* 经过encryptedData()加密返回的byte数据
* @param privateKey
* 私钥
* @return
*/
private static String RSA = "RSA";
public static byte[] decryptData(byte[] encryptedData, PrivateKey privateKey)
{
try
{
Cipher cipher = Cipher.getInstance(RSA);
cipher.init(Cipher.DECRYPT_MODE, privateKey);
return cipher.doFinal(encryptedData);
} catch (Exception e)
{
KLog.e("TAS",e);
return null;
}
}
这个包,直接拖进去就行
base64下载地址
欢迎留言评论