1.参考资料

1.1 android的Cipher官方文档

  https://developer.android.com/reference/javax/crypto/Cipher

  其中 构造Cipher实例时要提供加密算法参数 transformation 它的格式有两种:

    1. "algorithm/mode/padding"
    2. "algorithm"

  代码如下:

  Cipher c = Cipher.getInstance("DES/CBC/PKCS5Padding");

  其中关于rsa的部分:

Algorithm Modes Paddings Supported API Levels
RSA ECB
NONE
NoPadding
OAEPPadding
PKCS1Padding
1+
OAEPwithSHA-1andMGF1Padding
OAEPwithSHA-256andMGF1Padding
10+
OAEPwithSHA-224andMGF1Padding
OAEPwithSHA-384andMGF1Padding
OAEPwithSHA-512andMGF1Padding
23+

1.2 关于算法、Modes、Paddings

  https://docs.oracle.com/javase/8/docs/technotes/guides/security/StandardNames.html#Cipher

  其中 rsa 的mode ECB的介绍如下:注意红色部分。

ECB Electronic Codebook Mode, as defined in FIPS PUB 81 (generally this mode should not be used for multiple blocks of data).

 

2.生成密钥

2.1 使用openssl

第1步:生产私钥文件,rsa_private_key.pem

openssl genrsa -out rsa_private_key.pem 2048

查看下它的内容:

其中

  • -----BEGIN RSA PRIVATE KEY----- -----END RSA PRIVATE KEY----- 之间就是私钥经过base64编码后的字符。
  • 解码后就可以得到私钥的字节数组。

第2步:根据私钥生产公钥,rsa_public_key.pem

openssl rsa -in rsa_private_key.pem -out rsa_public_key.pem -pubout

它的内容与私钥相似。

其中

  • -----BEGIN PUBLIC KEY----- -----END PUBLIC KEY----- 之间就是公钥经过base64编码后的字符。
  • 解码后就可以得到公钥的字节数组。

第3步:对私钥进行PKCS#8编码

openssl pkcs8 -topk8 -in rsa_private_key.pem -out rsa_pkcs8_private_key.pem -nocrypt

第4步:认证(收费)

如果担心公钥的可靠性,可去权威机构认证一下,得到数字证书。

创建证书请求文件,填写各项信息,如国家、城市、公司,邮箱、密码等等。

openssl req -new -key rsa_private_key.pem -out rsa_cert.csr

用生成的 rsa_cert.csr文件 去权威机构(CA)申请证书,CA会给你数字证书文件。

2.2 用java代码生成密钥

除了上面方法外,也可以用java代码生成公钥、私钥。

       try {
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
kpg.initialize();
KeyPair keyPair = kpg.genKeyPair();
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); } catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}

3.示例

3.1 加密、解密(支持分段)

公钥加密--->私钥解密

     // 公钥加密,支持分段。最大 MAX_ENCRYPT_SIZE 字节
public static byte[] encryptByPublicKey(@NonNull byte[] data,RSAPublicKey key) throws Exception { // 加密数据
Cipher cipher = Cipher.getInstance(ANDROID_TRANSFORMATION);
cipher.init(Cipher.ENCRYPT_MODE, key); int inOffset = ,outOffset = ; int olen = cipher.getOutputSize(data.length);
byte output[] = new byte[olen];
Log.e("RSAUtil", "encryptByPublicKey: data.len = " + data.length + " outLen = " + olen); int loop = data.length / MAX_ENCRYPT_SIZE; for (int i = ; i < loop; ++i){
int inLen = MAX_ENCRYPT_SIZE;
int outLen = cipher.update(data,inOffset,inLen,output,outOffset);
Log.e("RSAUtil", "encryptByPublicKey: outLen = " + outLen + " inLen = " + inLen);
inOffset += inLen;
outOffset += outLen;
} if (inOffset <= data.length){
cipher.update(data,inOffset,data.length - inOffset,output,outOffset);
}
return cipher.doFinal();
} // 私钥解密,支持分段。最大 MAX_DECRYPT_SIZE 字节
public static byte[] decryptByPrivateKey(@NonNull byte[] encrypted,RSAPrivateKey key) throws Exception { // 解密数据
Cipher cipher = Cipher.getInstance(ANDROID_TRANSFORMATION);
cipher.init(Cipher.DECRYPT_MODE, key); int inOffset = ,outOffset = ; int olen = cipher.getOutputSize(encrypted.length);
byte output[] = new byte[olen];
Log.e("RSAUtil", "decryptByPrivateKey: data.len = " + encrypted.length + " outLen = " + olen); int loop = encrypted.length / MAX_DECRYPT_SIZE; for (int i = ; i < loop; ++i){
int inLen = MAX_DECRYPT_SIZE;
int outLen = cipher.update(encrypted,inOffset,inLen,output,outOffset);
Log.e("RSAUtil", "decryptByPrivateKey: outLen = " + outLen + " inLen = " + inLen);
inOffset += inLen;
outOffset += outLen;
} if (inOffset <= encrypted.length){
cipher.update(encrypted,inOffset,encrypted.length - inOffset,output,outOffset);
} return cipher.doFinal();
}

私钥加密--->公钥解密

     // 私钥加密,支持分段。最大 MAX_ENCRYPT_SIZE 字节
public static byte[] encryptByPrivateKey(@NonNull byte[] data,RSAPrivateKey key) throws Exception { // 数据加密
Cipher cipher = Cipher.getInstance(ANDROID_TRANSFORMATION);
cipher.init(Cipher.ENCRYPT_MODE, key); int inOffset = ,outOffset = ; int olen = cipher.getOutputSize(data.length);
byte output[] = new byte[olen];
Log.e("RSAUtil", "encryptByPrivateKey: data.len = " + data.length + " outLen = " + olen); int loop = data.length / MAX_ENCRYPT_SIZE; for (int i = ; i < loop; ++i){
int inLen = MAX_ENCRYPT_SIZE;
int outLen = cipher.update(data,inOffset,inLen,output,outOffset);
Log.e("RSAUtil", "encryptByPrivateKey: outLen = " + outLen + " inLen = " + inLen);
inOffset += inLen;
outOffset += outLen;
} if (inOffset <= data.length){
cipher.update(data,inOffset,data.length - inOffset,output,outOffset);
}
return cipher.doFinal();
} // 公钥解密,支持分段。最大 MAX_DECRYPT_SIZE 字节
public static byte[] decryptByPublicKey(@NonNull byte[] data,RSAPublicKey key) throws Exception { // 数据解密
Cipher cipher = Cipher.getInstance(ANDROID_TRANSFORMATION);
cipher.init(Cipher.DECRYPT_MODE, key); int inOffset = ,outOffset = ; int olen = cipher.getOutputSize(data.length);
byte output[] = new byte[olen];
Log.e("RSAUtil", "decryptByPublicKey: data.len = " + data.length + " outLen = " + olen); int loop = data.length / MAX_DECRYPT_SIZE; for (int i = ; i < loop; ++i){
int inLen = MAX_DECRYPT_SIZE;
int outLen = cipher.update(data,inOffset,inLen,output,outOffset);
Log.e("RSAUtil", "decryptByPublicKey: outLen = " + outLen + " inLen = " + inLen);
inOffset += inLen;
outOffset += outLen;
} if (inOffset <= data.length){
cipher.update(data,inOffset,data.length - inOffset,output,outOffset);
}
return cipher.doFinal();
}

3.2 签名、验证

    // 签名
public static byte[] sign(@NonNull byte [] data,RSAPrivateKey key) throws Exception {
Signature signature = Signature.getInstance("SHA256withRSA");
signature.initSign(key);
signature.update(data);
return signature.sign();
} // 验签
public static boolean verify(byte [] data, byte [] signed,RSAPublicKey key) throws Exception {
Signature signature = Signature.getInstance("SHA256withRSA");
signature.initVerify(key);
signature.update(data);
return signature.verify(signed);

4.注意事项

  • 加密最多(秘钥长度/8-11)个字节的数据,2048为245,1024为117。
  • android系统的RSA实现是"RSA/None/NoPadding",标准JDK实现是"RSA/ECB/PKCS1Padding" ,有可能在android机上加密后无法在服务器上解密。

5.代码

  完整示例

  https://github.com/f9q/rsa

android rsa 示例的更多相关文章

  1. 二维码(2)二维码登录原理及Android客户端示例

    1,原理 服务器: 数据库: 建立一个2维码登录的数据表,产生一个登录页时,插入一条记录X,X含将要登录的用户名字段(初始为空),2维码中的数据字段(唯一) 登录页面: 在产生的2维码中包含关键数据Y ...

  2. Android程序示例

    目录 Android代码示例 OptionsMenu ImageButton CheckBox & RadioButton Context Menu快捷菜单 Key Event ListVie ...

  3. Android RSA加密解密

    概述 RSA是目前最有影响力的公钥加密算法,该算法基于一个十分简单的数论事实:将两个大素数相乘十分容易,但那时想要对其乘积进行因式分解却极其困 难,因此可以将乘积公开作为加密密钥,即公钥,而两个大素数 ...

  4. 导入cocos2d-x samples android官方示例

    导了一晚上samples android示例,查了一晚上资料,费了很大的劲,终于成功导入并运行成功,分享一下经验: 1.下载eclipse与ADT跟android SDK,相信大家都会装了吧. 2.下 ...

  5. Android RSA加密对象数据

    前几天说了手头项目用MD5和双向加密DES来进行加密的,因为产品是在不断的改造中完善的.考虑到DES和MD5加解密还是会存下一定的风险,于是产品一拍,不行,听你们说MD5加密是不安全的,咱们产品以后做 ...

  6. Android编程示例:创建机场计划模拟器应用程序

    在本文中,我们将演示如何使用Android Studio和Java编程语言创建一个示例Android应用程序,从“临时”实现高级响应用户界面的功能.本文中讨论的应用程序将实现机场航班时刻表模拟的功能. ...

  7. Android系统示例分析之AndroidBeamDemo

    在这个示例工程中,只有一个Activity: public class Beam extends Activity implements CreateNdefMessageCallback,      ...

  8. Android系统示例之ActionBarCompat

    导入工程ActionBarCompat时,出现错误.从其他工程下拷贝project.propertiest文件过来,问题仍在.拷贝后需要重启Eclipse才解决.问题如下: [2013-07-03 1 ...

  9. Android系统示例分析之AccelerometerPlay

    程序界面: 创建 SimulationView , 实现接口 SensorEventListener , 实现接口中两个抽象方法 public void onSensorChanged(SensorE ...

随机推荐

  1. VS2010-MFC(对话框:为控件添加消息处理函数)

    转自:http://www.jizhuomi.com/software/156.html MFC为对话框和控件等定义了诸多消息,我们对它们操作时会触发消息,这些消息最终由消息处理函数处理.比如我们点击 ...

  2. Java-JPA:JPA

    ylbtech-Java-JPA:JPA JPA是Java Persistence API的简称,中文名Java持久层API,是JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对 ...

  3. PAT甲级——A1081 Rational Sum

    Given N rational numbers in the form numerator/denominator, you are supposed to calculate their sum. ...

  4. varStatus实现<c:forEach获取元素的下标 集合长度

    有时候需要获取元素的下标,来实现一个编号的效果 那么使用 varStatus可以实现 代码 效果 如果需要把序号倒着数 那就用集合的长度减去当前下标 首先引入 <%@ taglib prefix ...

  5. GitHub for Visual Studio使用讲解

    从VS2015起(应该是吧?),微软已经在VS中集成了GitHub,方便开发者对项目进行版本控制. 扩展包下载地址:https://aka.ms/ghfvs 其实VS2015的安装包中已经自带了这个扩 ...

  6. HZOI20190823模拟31题解

    题面:https://www.cnblogs.com/Juve/articles/11425141.html math:仔细看看其实是个水题 #include<iostream> #inc ...

  7. kill 3000

    杀3000端口,是作为一个web未开发人员经常遇到的事情 所以我今天就分享一下我的杀3000端口秘诀 lsof -i: 先要找到端口 node zcool 20u IPv6 0xdddbb4f6f12 ...

  8. Interface Builder: What are the UIView's Layout iOS 6/7 Deltas for?

    up vote57down votefavorite 19 I just noticed the iOS 6/7 Delta property found under the UIView's str ...

  9. 数据表中记录明明有,session.get(类.class, id);返回null

    出现null的处理思路首先检查数据库中是否真的有这个记录 确实存在的,用接口查一下最大值,也是存在的,数据库连接正常 写sql也可以查得到 然而诡异的事情出现了 难道是一直在用的dao代码出了问题? ...

  10. Jemter测压工具的安装与使用

    注:在安装Jmeter之前,请先检查下电脑有没有装JDK:开始->运行->然后输入cmd->进入命令行界面,输入java -version , 出现以下信息就是此电脑已安装了JDK ...