public
class
Encrypt {
public static final String rastwo(String symmetricKey, String base64PublicKey) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
byte[] bArr
=
Base64.decode(base64PublicKey,
2
);
PublicKey key
=
KeyFactory.getInstance(
"RSA"
).generatePublic(new X509EncodedKeySpec(bArr));
Cipher cipher
=
Cipher.getInstance(
"RSA/ECB/PKCS1Padding"
);
cipher.init(
1
, key);
byte[] bytes
=
symmetricKey.getBytes(UTF_8);
byte[] bytes2
=
(
"FREECHARGE_V2|"
+
Base64.encodeToString(cipher.doFinal(bytes),
2
)).getBytes(UTF_8);
return
Base64.encodeToString(bytes2,
2
);
}
public static final String aesinput(String
input
, String symmetricKey) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidAlgorithmParameterException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
byte[] IV
=
new byte[
12
];
SecretKeySpec secretKeySpec
=
SecretKey(symmetricKey);
Cipher cipher
=
Cipher.getInstance(
"AES/GCM/NoPadding"
);
cipher.init(
1
, secretKeySpec, new GCMParameterSpec(
128
, IV));
byte[] bytes
=
input
.getBytes(UTF_8);
byte[] bytes2
=
(
"FREECHARGE_V2|"
+
Base64.encodeToString(cipher.doFinal(bytes),
2
)).getBytes(UTF_8);
return
Base64.encodeToString(bytes2,
2
);
}
public static final SecretKeySpec SecretKey(String secretKey) throws NoSuchAlgorithmException {
MessageDigest messageDigest
=
MessageDigest.getInstance(
"SHA-256"
);
byte[] bytes
=
secretKey.getBytes(UTF_8);
byte[] digest
=
messageDigest.digest(bytes);
byte[] copyOf
=
Arrays.copyOf(digest,
16
);
SecretKeySpec secretKeySpec
=
new SecretKeySpec(copyOf,
"AES"
);
return
secretKeySpec;
}
}