질문에 제공된 AES용 Golang 암호화 기능은 AES(Advanced Encryption Standard) 알고리즘을 사용하여 문자열을 암호화합니다. Base64 인코딩으로 암호문을 생성합니다. Java에서 이 암호문을 해독하려면 적절한 해독 기능이 필요합니다.
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)
}
}
Java 및 Scala 복호화 함수는 모두 암호문과 키를 입력으로 사용하고, AES/CFB/NoPadding 알고리즘을 사용하여 암호문을 복호화하고, 복호화된 일반 텍스트를 문자열로 반환합니다.
부인 성명: 제공된 모든 리소스는 부분적으로 인터넷에서 가져온 것입니다. 귀하의 저작권이나 기타 권리 및 이익이 침해된 경우 자세한 이유를 설명하고 저작권 또는 권리 및 이익에 대한 증거를 제공한 후 이메일([email protected])로 보내주십시오. 최대한 빨리 처리해 드리겠습니다.
Copyright© 2022 湘ICP备2022001581号-3