Android和Java中RSA标准不一致解决方法

来源:互联网 发布:电脑挂机赚钱软件2016 编辑:程序博客网 时间:2024/05/21 14:46

Android和Java中RSA标准不一致解决方法

最近在做java RSA加密时发现Android和Java中机密的结果不一样,阅读源码查阅文档后得知java和Android对RSA的标准不一致

原生Java中的doFinal()方法源码

public final byte[] doFinal(byte[] var1) throws IllegalBlockSizeException, BadPaddingException {    this.checkCipherState();    if(var1 == null) {        throw new IllegalArgumentException("Null input buffer");    } else {        this.chooseFirstProvider();        return this.spi.engineDoFinal(var1, 0, var1.length);    }}

Android中doFinal()方法源码

   public final byte[] doFinal(byte[] input)               throws IllegalBlockSizeException, BadPaddingException {    checkCipherState();    // Input sanity check    if (input == null) {        throw new IllegalArgumentException("Null input buffer");    }    updateProviderIfNeeded();    return spi.engineDoFinal(input, 0, input.length);   }

解决方法

在获取Cipher时指定编码方式

在Java中

public static final String KEY_ALGORITHM = "RSA";

在Android中

public static final String KEY_ALGORITHM = "RSA/ECB/PKCS1Padding";

获取Cipher

Cipher cipher = Cipher.getInstance(KEY_ALGORITHM);
阅读全文
0 0