1. 程式人生 > >android RSA和Java RSA加密不一致的坑

android RSA和Java RSA加密不一致的坑

最近專案採用RSA進行加密,遇到了坑,記錄一下

1、BASE64Decoder在Android中是不存在的,需用Base64替換,
BASE64Decoder base64Decoder= new BASE64Decoder();
byte[] buffer= base64Decoder.decodeBuffer(privateKeyStr);
替換為:
byte[] buffer= Base64.decode(DEFAULT_PUBLIC_KEY, Base64.DEFAULT);

導包

import android.util.Base64;
2、cipher= Cipher.getInstance
("RSA"); cipher.init(Cipher.ENCRYPT_MODE, publicKey); 做加密,但是一直無法與伺服器那邊對接,而且每次生成的密文是一樣的。 後來參考文章:http://my.oschina.net/oschenxiaomo/blog/543199 使用cipher= Cipher.getInstance("RSA/ECB/PKCS1Padding")後就可以了。