|
|
|
@ -7,9 +7,6 @@ import com.github.catvod.utils.Util; |
|
|
|
import java.nio.charset.StandardCharsets; |
|
|
|
import java.nio.charset.StandardCharsets; |
|
|
|
import java.security.Key; |
|
|
|
import java.security.Key; |
|
|
|
import java.security.KeyFactory; |
|
|
|
import java.security.KeyFactory; |
|
|
|
import java.security.PublicKey; |
|
|
|
|
|
|
|
import java.security.interfaces.RSAPrivateKey; |
|
|
|
|
|
|
|
import java.security.interfaces.RSAPublicKey; |
|
|
|
|
|
|
|
import java.security.spec.PKCS8EncodedKeySpec; |
|
|
|
import java.security.spec.PKCS8EncodedKeySpec; |
|
|
|
import java.security.spec.X509EncodedKeySpec; |
|
|
|
import java.security.spec.X509EncodedKeySpec; |
|
|
|
import java.util.Arrays; |
|
|
|
import java.util.Arrays; |
|
|
|
@ -50,21 +47,13 @@ public class Crypto { |
|
|
|
public static String rsa(String mode, boolean pub, boolean encrypt, String input, boolean inBase64, String key, boolean outBase64) { |
|
|
|
public static String rsa(String mode, boolean pub, boolean encrypt, String input, boolean inBase64, String key, boolean outBase64) { |
|
|
|
try { |
|
|
|
try { |
|
|
|
Key rsaKey = generateKey(pub, key); |
|
|
|
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(StandardCharsets.UTF_8); |
|
|
|
byte[] inBytes = inBase64 ? Base64.decode(input.replaceAll("_", "/").replaceAll("-", "+"), Base64.DEFAULT) : input.getBytes(StandardCharsets.UTF_8); |
|
|
|
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); |
|
|
|
String tranformation = "RSA/ECB/PKCS1Padding"; |
|
|
|
|
|
|
|
if ("RSA/PKCS1".equals(mode)) tranformation = "RSA/ECB/PKCS1Padding"; |
|
|
|
|
|
|
|
else if ("RSA/None/NoPadding".equals(mode)) tranformation = "RSA/None/NoPadding"; |
|
|
|
|
|
|
|
Cipher cipher = Cipher.getInstance(tranformation); |
|
|
|
cipher.init(encrypt ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE, rsaKey); |
|
|
|
cipher.init(encrypt ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE, rsaKey); |
|
|
|
int blockLen = encrypt ? len / 8 - 11 : len / 8; |
|
|
|
byte[] outBytes = cipher.doFinal(inBytes); |
|
|
|
int bufIdx = 0; |
|
|
|
|
|
|
|
while (bufIdx < inBytes.length) { |
|
|
|
|
|
|
|
int bufEndIdx = Math.min(bufIdx + blockLen, inBytes.length); |
|
|
|
|
|
|
|
byte[] tmpInBytes = new byte[bufEndIdx - bufIdx]; |
|
|
|
|
|
|
|
System.arraycopy(inBytes, bufIdx, tmpInBytes, 0, tmpInBytes.length); |
|
|
|
|
|
|
|
byte[] tmpBytes = cipher.doFinal(tmpInBytes); |
|
|
|
|
|
|
|
bufIdx = bufEndIdx; |
|
|
|
|
|
|
|
outBytes = concatArrays(outBytes, tmpBytes); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return outBase64 ? Base64.encodeToString(outBytes, Base64.NO_WRAP) : new String(outBytes, StandardCharsets.UTF_8); |
|
|
|
return outBase64 ? Base64.encodeToString(outBytes, Base64.NO_WRAP) : new String(outBytes, StandardCharsets.UTF_8); |
|
|
|
} catch (Exception e) { |
|
|
|
} catch (Exception e) { |
|
|
|
e.printStackTrace(); |
|
|
|
e.printStackTrace(); |
|
|
|
@ -77,18 +66,4 @@ public class Crypto { |
|
|
|
else key = key.replaceAll(System.lineSeparator(), "").replace("-----BEGIN PRIVATE KEY-----", "").replace("-----END PRIVATE KEY-----", ""); |
|
|
|
else key = key.replaceAll(System.lineSeparator(), "").replace("-----BEGIN PRIVATE KEY-----", "").replace("-----END PRIVATE KEY-----", ""); |
|
|
|
return pub ? KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(Base64.decode(key, Base64.DEFAULT))) : KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(Base64.decode(key, Base64.DEFAULT))); |
|
|
|
return pub ? KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(Base64.decode(key, Base64.DEFAULT))) : KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(Base64.decode(key, Base64.DEFAULT))); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private static int getModulusLength(Key key) { |
|
|
|
|
|
|
|
if (key instanceof PublicKey) return ((RSAPublicKey) key).getModulus().bitLength(); |
|
|
|
|
|
|
|
else return ((RSAPrivateKey) key).getModulus().bitLength(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static byte[] concatArrays(byte[] a, byte[] b) { |
|
|
|
|
|
|
|
int aLen = a.length; |
|
|
|
|
|
|
|
int bLen = b.length; |
|
|
|
|
|
|
|
byte[] result = new byte[aLen + bLen]; |
|
|
|
|
|
|
|
System.arraycopy(a, 0, result, 0, aLen); |
|
|
|
|
|
|
|
System.arraycopy(b, 0, result, aLen, bLen); |
|
|
|
|
|
|
|
return result; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|