Home > Backend Development > Golang > How to decrypt AES encrypted text generated in Golang using Java and Scala?

How to decrypt AES encrypted text generated in Golang using Java and Scala?

Barbara Streisand
Release: 2024-10-29 20:02:02
Original
781 people have browsed it

How to decrypt AES encrypted text generated in Golang using Java and Scala?

AES Encryption in Golang and Decryption in Java

Decrypting AES Encrypted Text in Java

To decrypt the AES encrypted text generated by the Golang function, a Java implementation is required. The Java code below demonstrates the decryption process:

<code class="java">public class AESDecryption {

    public static String decode(String base64Text, byte[] key) throws Exception {
        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) {
        try {
            String encryptedText = "c1bpFhxn74yzHQs-vgLcW6E5yL8zJfgceEQgYl0=";
            byte[] key = "0123456789abcdef".getBytes();
            String decryptedText = decode(encryptedText, key);
            System.out.println("Decrypted text: " + decryptedText);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}</code>
Copy after login

This Java code should successfully decrypt the encrypted text generated by the Golang function and return the original plaintext.

Scala Version

The Scala version of the decryption code is as follows:

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

    def main(args: Array[String]): Unit = {
        val encryptedText = "c1bpFhxn74yzHQs-vgLcW6E5yL8zJfgceEQgYl0="
        val key = "0123456789abcdef"
        val decryptedText = decode(encryptedText, key)
        println("Decrypted text: " + decryptedText)
    }
}</code>
Copy after login

This Scala code will also decrypt the AES encrypted text and print the plaintext.

The above is the detailed content of How to decrypt AES encrypted text generated in Golang using Java and Scala?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template