Android 通过RSA对密码进行加密

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返回的数据就可以传给服务器进行登录了

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容