|
|
@ -1,5 +1,6 @@ |
|
|
|
package com.android.base.utils.security; |
|
|
|
package com.android.base.utils.security; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import android.os.Build; |
|
|
|
import android.util.Base64; |
|
|
|
import android.util.Base64; |
|
|
|
|
|
|
|
|
|
|
|
import java.io.ByteArrayInputStream; |
|
|
|
import java.io.ByteArrayInputStream; |
|
|
@ -31,7 +32,14 @@ public class SimpleRsa { |
|
|
|
private static PublicKey getPublicKeyFromX509(String algorithm, String bysKey) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchProviderException { |
|
|
|
private static PublicKey getPublicKeyFromX509(String algorithm, String bysKey) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchProviderException { |
|
|
|
byte[] decodedKey = Base64.decode(bysKey, Base64.DEFAULT); |
|
|
|
byte[] decodedKey = Base64.decode(bysKey, Base64.DEFAULT); |
|
|
|
X509EncodedKeySpec x509 = new X509EncodedKeySpec(decodedKey); |
|
|
|
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); |
|
|
|
return keyFactory.generatePublic(x509); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -68,14 +76,14 @@ public class SimpleRsa { |
|
|
|
InputStream ins = new ByteArrayInputStream(Base64.decode(content, Base64.DEFAULT)); |
|
|
|
InputStream ins = new ByteArrayInputStream(Base64.decode(content, Base64.DEFAULT)); |
|
|
|
ByteArrayOutputStream writer = new ByteArrayOutputStream(); |
|
|
|
ByteArrayOutputStream writer = new ByteArrayOutputStream(); |
|
|
|
byte[] buf = new byte[128]; |
|
|
|
byte[] buf = new byte[128]; |
|
|
|
int bufl; |
|
|
|
int offset; |
|
|
|
while ((bufl = ins.read(buf)) != -1) { |
|
|
|
while ((offset = ins.read(buf)) != -1) { |
|
|
|
byte[] block; |
|
|
|
byte[] block; |
|
|
|
if (buf.length == bufl) { |
|
|
|
if (buf.length == offset) { |
|
|
|
block = buf; |
|
|
|
block = buf; |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
block = new byte[bufl]; |
|
|
|
block = new byte[offset]; |
|
|
|
System.arraycopy(buf, 0, block, 0, bufl); |
|
|
|
System.arraycopy(buf, 0, block, 0, offset); |
|
|
|
} |
|
|
|
} |
|
|
|
writer.write(cipher.doFinal(block)); |
|
|
|
writer.write(cipher.doFinal(block)); |
|
|
|
} |
|
|
|
} |
|
|
|