Home >Backend Development >Golang >How to Decrypt AES-Encrypted Text in Java When Encrypted in Go?
Question:
I have successfully encrypted text using AES in Go. However, I face challenges decrypting the encrypted text in Java. Can you provide assistance in understanding the Java decryption flow and a working Java decoder?
Answer:
Java Decoder:
<code class="java">import java.nio.charset.StandardCharsets; import java.security.InvalidAlgorithmParameterException; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.util.Arrays; import java.util.Base64; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto.IvParameterSpec; import javax.crypto.NoSuchPaddingException; import javax.crypto.SecretKeySpec; public class AesDecrypter { 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); } }</code>
Scala Decoder:
<code class="scala">import java.nio.charset.StandardCharsets import javax.crypto._ import java.security.spec.IvParameterSpec import java.security.spec.SecretKeySpec import java.util.Base64 object AesDecrypter { 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)) } }</code>
Demo:
For demonstration purposes, let's encode the string "test text 123" with the encryption function written in Go and decode it with the decryption function written in Java (or Scala):
// Go encoder key := []byte("0123456789abcdef") text := "test text 123" encryptedText := encrypt(key, text) println(encryptedText) // prints c1bpFhxn74yzHQs-vgLcW6E5yL8zJfgceEQgYl0= // Java decoder byte[] keyBytes = "0123456789abcdef".getBytes(); String decodedText = AesDecrypter.decode(encryptedText, keyBytes); System.out.println(decodedText); // prints test text 123 // Scala decoder val decodedText = AesDecrypter.decode(encryptedText, "0123456789abcdef") println(decodedText) // prints test text 123
The above is the detailed content of How to Decrypt AES-Encrypted Text in Java When Encrypted in Go?. For more information, please follow other related articles on the PHP Chinese website!