1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| public static void main(String[] args) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, NoSuchProviderException, InvalidKeySpecException, UnsupportedEncodingException, InvalidAlgorithmParameterException, DataLengthException, IllegalStateException, InvalidCipherTextException
{
byte[] json_byte = Base64.decodeBase64("eyJjdCI6InhITnNOZC9USEJ5RDVXZ2c2VWRsTFE9PSIsIml2IjoiOThiMzNmNjQyM2ZjODk5OWY4MjgwNjcyOTlmNzk3YTkiLCJzIjoiZTgwOTg3ZDQxZDYxMDM3MiJ9");
String json_string = new String(json_byte, "UTF-8");
System.out.println(json_string);
String ct = "xHNsNd/THByD5Wgg6UdlLQ=="; // Base 64
String cl = "cle";
String iv = "98b33f6423fc8999f828067299f797a9"; // Hex 256 bits
String st = "e80987d41d610372"; // Hex 128 bits
byte[] iv_byte = DatatypeConverter.parseHexBinary(iv);
byte[] st_byte = DatatypeConverter.parseHexBinary(st);
Security.addProvider(new BouncyCastleProvider());
byte[] result = decrypt(Base64.decodeBase64(ct.getBytes()), cl.getBytes(), st_byte);
System.out.println(new String(result));
}
private static byte[] decrypt(byte[] cipherText, byte[] password, byte[] salt) throws DataLengthException, IllegalStateException, InvalidCipherTextException, InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException
{
BlockCipher engine = new AESEngine();
CBCBlockCipher cipher = new CBCBlockCipher(engine);
PKCS5S1ParametersGenerator keyGenerator = new PKCS5S1ParametersGenerator(new SHA256Digest());
keyGenerator.init(password, salt, 20);
CipherParameters keyParams = keyGenerator.generateDerivedParameters(256);
cipher.init(false, keyParams);
byte[] decryptedBytes = new byte[cipherText.length];
int numBytesCopied = cipher.processBlock(cipherText, 0, decryptedBytes, 0);
return decryptedBytes;
} |
Partager