Android(安卓)RSA 公钥加密 遇到坑
16lz
2021-01-26
2019独角兽企业重金招聘Python工程师标准>>>
public class RSAUtil { public static final String KEY_ALGORITHM = "RSA/None/PKCS1Padding"; /** * 使用模和指数生成RSA公钥 * 注意:【此代码用了默认补位方式,为RSA/None/PKCS1Padding,不同JDK默认的补位方式可能不同,如Android默认是RSA * /None/NoPadding】 * * @param modulus 模 * @param exponent 指数 * @return */ public static RSAPublicKey getPublicKey(String modulus, String exponent) { try { BigInteger b1 = new BigInteger(modulus, 16); BigInteger b2 = new BigInteger(exponent, 16); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); RSAPublicKeySpec keySpec = new RSAPublicKeySpec(b1, b2); return (RSAPublicKey) keyFactory.generatePublic(keySpec); } catch (Exception e) { e.printStackTrace(); return null; } } /** * 公钥加密 */ public static byte[] encrypt(byte[] content, PublicKey publicKey) { try { Cipher cipher = Cipher.getInstance(KEY_ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, publicKey); return cipher.doFinal(content); } catch (Exception e) { e.printStackTrace(); } return null; }}
Android 用这个方法生成公私钥对
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
Android RSA加解密的时候用
Cipher cipher = Cipher.getInstance("RSA/None/PKCS1Padding");
转载于:https://my.oschina.net/547217475/blog/3028495
更多相关文章
- Ubuntu 交叉编译 OpenSSL,并在 Android(安卓)中使用
- Android(安卓)使用SWIG生成Jni代码
- HockeyApp环境搭建
- android coredump 调试
- AS 配置使用NDK
- 编译源码生成的SDK,创建android project 没有proguard.cfg问题
- 导入android源码有错,R.java文件不能自动生成解决方法[转]
- Android(安卓)如何关闭Navigation Bar M
- NDK 入门