Maison > développement back-end > Golang > le corps du texte

Comment déchiffrer du texte crypté AES en Java et Scala, compte tenu du cryptage Golang ?

Susan Sarandon
Libérer: 2024-10-26 07:55:30
original
777 Les gens l'ont consulté

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

Chiffrement AES en Golang et son décryptage en Java

La fonction de chiffrement Golang pour AES fournie dans la question chiffre une chaîne à l'aide de l'algorithme Advanced Encryption Standard (AES), produire un texte chiffré en codage Base64. Pour déchiffrer ce texte chiffré en Java, nous avons besoin d'une fonction de décryptage appropriée.

Fonction de décryptage Java

<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>
Copier après la connexion

Fonction de décryptage Scala

<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>
Copier après la connexion

Le décryptage Java et Scala les fonctions prennent le texte chiffré et la clé en entrée, déchiffrent le texte chiffré à l'aide de l'algorithme AES/CFB/NoPadding et renvoient le texte brut déchiffré sous forme de chaîne.

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Derniers articles par auteur
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!