From 525e167baedc9051d23f98fe4f557fbe96088170 Mon Sep 17 00:00:00 2001 From: Zhanty Date: Fri, 6 Sep 2019 17:58:05 +0800 Subject: [PATCH] update SimpleRsa --- .../base/utils/security/SimpleRsa.java | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/lib_base/src/main/java/com/android/base/utils/security/SimpleRsa.java b/lib_base/src/main/java/com/android/base/utils/security/SimpleRsa.java index 3a95a0d..77e67e9 100644 --- a/lib_base/src/main/java/com/android/base/utils/security/SimpleRsa.java +++ b/lib_base/src/main/java/com/android/base/utils/security/SimpleRsa.java @@ -1,5 +1,6 @@ package com.android.base.utils.security; +import android.os.Build; import android.util.Base64; import java.io.ByteArrayInputStream; @@ -31,7 +32,14 @@ public class SimpleRsa { private static PublicKey getPublicKeyFromX509(String algorithm, String bysKey) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchProviderException { byte[] decodedKey = Base64.decode(bysKey, Base64.DEFAULT); X509EncodedKeySpec x509 = new X509EncodedKeySpec(decodedKey); - KeyFactory keyFactory = KeyFactory.getInstance(algorithm, "BC"); + KeyFactory keyFactory; + //适配Android P及以后版本,否则报错NoSuchAlgorithmException + //https://android-developers.googleblog.com/2018/03/cryptography-changes-in-android-p.html + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { + keyFactory = KeyFactory.getInstance(algorithm); + } else { + keyFactory = KeyFactory.getInstance(algorithm, "BC"); + } return keyFactory.generatePublic(x509); } @@ -68,14 +76,14 @@ public class SimpleRsa { InputStream ins = new ByteArrayInputStream(Base64.decode(content, Base64.DEFAULT)); ByteArrayOutputStream writer = new ByteArrayOutputStream(); byte[] buf = new byte[128]; - int bufl; - while ((bufl = ins.read(buf)) != -1) { + int offset; + while ((offset = ins.read(buf)) != -1) { byte[] block; - if (buf.length == bufl) { + if (buf.length == offset) { block = buf; } else { - block = new byte[bufl]; - System.arraycopy(buf, 0, block, 0, bufl); + block = new byte[offset]; + System.arraycopy(buf, 0, block, 0, offset); } writer.write(cipher.doFinal(block)); }