一开始在Android尝试使用RSA,是直接参考文章:
http://blog.csdn.net/s464036801/article/details/7794599
该文章未能给出在Android上的正确的解决方案,但代码整体是可用的,需要注意一下几点的替换:
1、BASE64Decoder在Android中是不存在的,需用Base64替换,
BASE64Decoder base64Decoder= new BASE64Decoder();
byte[] buffer= base64Decoder.decodeBuffer(privateKeyStr);
替换为:
byte[] buffer= Base64.decode(DEFAULT_PUBLIC_KEY, Base64.DEFAULT);

2、对于String数据转换为byte[],直接使用getBytes()是不合理的,
标准Java的解决方案是:DatatypeConverter.parseHexBinary(data);
但Android上没有DatatypeConverter,所以要进行替换,
参考文章:
http://stackoverflow.com/questions/34342136/alternatives-a-datatypeconverter-in-androidsolved
在Android studio中加入依赖:compile 'commons-codec:commons-codec:1.3'
Hex.decodeHex(data.toCharArray())
替换为:
DatatypeConverter.parseHexBinary(data)

3、cipher= Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
做加密,但是一直无法与服务器那边对接,而且每次生成的密文是一样的。
后来参考文章:http://my.oschina.net/oschenxiaomo/blog/543199
使用cipher= Cipher.getInstance("RSA/ECB/PKCS1Padding")后就可以了。

更多相关文章

  1. 【适配】【转】Android开发:最全面、最易懂的Android屏幕适配解决
  2. 最受欢迎的文章汇总
  3. StevGuo系列文章翻译之Android电源管理
  4. Android 异步加载解决方案
  5. Android高效加载大图、多图解决方案,有效避免程序OOM
  6. Android Error:Execution failed for task ':app:compileDebugJa
  7. android 这篇文章让我更加明白android数据库存储
  8. Android沉浸式标题栏迄今为止最完美的解决方案

随机推荐

  1. android gridview 去除四周间隙
  2. Android中Broadcast的Intent大全
  3. Android(安卓)handler异步更新
  4. Android中获取应用程序(包)的大小-----Pa
  5. Android(安卓)HttpgetRequester+onRespon
  6. Android(安卓)HttpUtils工具类
  7. Android(安卓)LinearLayout和RelativeLay
  8. android 确认对话框实例
  9. iTextPdf--the world's preferred PDF li
  10. android ANR 案例分析