Home  >  Article  >  Backend Development  >  How to Decrypt AES-Encrypted Text in Java and Scala, Given Golang Encryption?

How to Decrypt AES-Encrypted Text in Java and Scala, Given Golang Encryption?

Susan Sarandon
Susan SarandonOriginal
2024-10-26 07:55:30779browse

How to Decrypt AES-Encrypted Text in Java and Scala, Given Golang Encryption?

AES Encryption in Golang and Its Decryption in Java

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.

Java Decryption Function

<code class="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();
        }
    }
}</code>

Scala Decryption Function

<code class="scala">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)
  }
}</code>

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.

The above is the detailed content of How to Decrypt AES-Encrypted Text in Java and Scala, Given Golang Encryption?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn