The Golang encryption function for AES provided in the question encrypts a string using the Advanced Encryption Standard (AES) algorithm, producing a ciphertext in Base64 encoding. To decrypt this ciphertext in Java, we require an appropriate decryption function.
import java.util.Base64;
import javax.crypto.*;
import javax.crypto.spec.*;
public class AESDecryption {
public static String decode(String base64Text, byte[] key)
throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
byte[] inputArr = Base64.getUrlDecoder().decode(base64Text);
SecretKeySpec skSpec = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES/CFB/NoPadding");
int blockSize = cipher.getBlockSize();
IvParameterSpec iv = new IvParameterSpec(Arrays.copyOf(inputArr, blockSize));
byte[] dataToDecrypt = Arrays.copyOfRange(inputArr, blockSize, inputArr.length);
cipher.init(Cipher.DECRYPT_MODE, skSpec, iv);
byte[] result = cipher.doFinal(dataToDecrypt);
return new String(result, StandardCharsets.UTF_8);
}
public static void main(String[] args) {
// Encryption result from the Go function.
String base64Text = "c1bpFhxn74yzHQs-vgLcW6E5yL8zJfgceEQgYl0=";
// The encryption key (same as in Go).
byte[] key = "0123456789abcdef".getBytes();
try {
// Decrypt the ciphertext.
String decrypted = decode(base64Text, key);
// Print the decrypted text.
System.out.println(decrypted);
} catch (Exception e) {
e.printStackTrace();
}
}
}
import java.nio.charset.StandardCharsets
import javax.crypto._
import javax.crypto.spec.{IvParameterSpec, SecretKeySpec}
import java.util.Base64
object AESDecryption {
def decode(input: String, key: String): String = {
val cipher = Cipher.getInstance("AES/CFB/NoPadding")
val blockSize = cipher.getBlockSize
val keyBytes = key.getBytes
val inputArr = Base64.getUrlDecoder.decode(input)
val skSpec = new SecretKeySpec(keyBytes, "AES")
val iv = new IvParameterSpec(inputArr.slice(0, blockSize).toArray)
val dataToDecrypt = inputArr.slice(blockSize, inputArr.size)
cipher.init(Cipher.DECRYPT_MODE, skSpec, iv)
new String(cipher.doFinal(dataToDecrypt.toArray), StandardCharsets.UTF_8)
}
def main(args: Array[String]): Unit = {
// Encryption result from the Go function.
val base64Text = "c1bpFhxn74yzHQs-vgLcW6E5yL8zJfgceEQgYl0="
// The encryption key (same as in Go).
val key = "0123456789abcdef"
// Decrypt the ciphertext.
val decrypted = decode(base64Text, key)
// Print the decrypted text.
println(decrypted)
}
}
Both the Java and Scala decryption functions take the ciphertext and key as input, decrypt the ciphertext using the AES/CFB/NoPadding algorithm, and return the decrypted plaintext as a string.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3