1.当用户输入密码后,请求接口获取一个公钥
2.通过RSA对密码进行加密
//密码加密
private String initEncryptor(String mPasswordContent,String result) {
//mPasswordContent 输入的密码
//result 公钥
String outStr = "";
Log.e("tag","公钥:"+result);
try {
// base64编码的公钥
byte[] decoded = Base64.decode(result, Base64.DEFAULT);
RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(decoded));
// RSA加密
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
outStr = Base64.encodeToString(cipher.doFinal(mPasswordContent.getBytes("UTF-8")), Base64.DEFAULT);
Log.e("tag","加密后的数据:"+outStr);
} catch (Exception e) {
e.printStackTrace();
}
return outStr;
}
3.通过initEncryptor返回的数据就可以传给服务器进行登录了