|
|
|
|
@ -4,6 +4,7 @@ import android.util.Base64; |
|
|
|
|
|
|
|
|
|
import com.github.catvod.utils.Util; |
|
|
|
|
|
|
|
|
|
import java.nio.charset.StandardCharsets; |
|
|
|
|
import java.security.Key; |
|
|
|
|
import java.security.KeyFactory; |
|
|
|
|
import java.security.PublicKey; |
|
|
|
|
@ -38,8 +39,8 @@ public class Crypto { |
|
|
|
|
SecretKeySpec keySpec = new SecretKeySpec(keyBuf, "AES"); |
|
|
|
|
if (iv == null) cipher.init(encrypt ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE, keySpec); |
|
|
|
|
else cipher.init(encrypt ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE, keySpec, new IvParameterSpec(ivBuf)); |
|
|
|
|
byte[] inBuf = inBase64 ? Base64.decode(input.replaceAll("_", "/").replaceAll("-", "+"), Base64.DEFAULT) : input.getBytes("UTF-8"); |
|
|
|
|
return outBase64 ? Base64.encodeToString(cipher.doFinal(inBuf), Base64.NO_WRAP) : new String(cipher.doFinal(inBuf), "UTF-8"); |
|
|
|
|
byte[] inBuf = inBase64 ? Base64.decode(input.replaceAll("_", "/").replaceAll("-", "+"), Base64.DEFAULT) : input.getBytes(StandardCharsets.UTF_8); |
|
|
|
|
return outBase64 ? Base64.encodeToString(cipher.doFinal(inBuf), Base64.NO_WRAP) : new String(cipher.doFinal(inBuf), StandardCharsets.UTF_8); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
e.printStackTrace(); |
|
|
|
|
return ""; |
|
|
|
|
@ -51,7 +52,7 @@ public class Crypto { |
|
|
|
|
Key rsaKey = generateKey(pub, key); |
|
|
|
|
int len = getModulusLength(rsaKey); |
|
|
|
|
byte[] outBytes = new byte[0]; |
|
|
|
|
byte[] inBytes = inBase64 ? Base64.decode(input.replaceAll("_", "/").replaceAll("-", "+"), Base64.DEFAULT) : input.getBytes("UTF-8"); |
|
|
|
|
byte[] inBytes = inBase64 ? Base64.decode(input.replaceAll("_", "/").replaceAll("-", "+"), Base64.DEFAULT) : input.getBytes(StandardCharsets.UTF_8); |
|
|
|
|
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); |
|
|
|
|
cipher.init(encrypt ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE, rsaKey); |
|
|
|
|
int blockLen = encrypt ? len / 8 - 11 : len / 8; |
|
|
|
|
@ -64,7 +65,7 @@ public class Crypto { |
|
|
|
|
bufIdx = bufEndIdx; |
|
|
|
|
outBytes = concatArrays(outBytes, tmpBytes); |
|
|
|
|
} |
|
|
|
|
return outBase64 ? Base64.encodeToString(outBytes, Base64.NO_WRAP) : new String(outBytes, "UTF-8"); |
|
|
|
|
return outBase64 ? Base64.encodeToString(outBytes, Base64.NO_WRAP) : new String(outBytes, StandardCharsets.UTF_8); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
e.printStackTrace(); |
|
|
|
|
return ""; |
|
|
|
|
|