Apache Nifi 的加密算法更新失败:“使用算法[AES/GCM/NoPadding]进行解密失败”。
创始人
2024-09-04 12:00:52
0

这个错误通常发生在使用Apache Nifi进行解密时,加密算法不匹配导致解密失败。解决方法是确保使用正确的加密算法进行解密。

以下是一个示例代码片段,展示如何使用AES/GCM/NoPadding算法进行加密和解密:

import javax.crypto.Cipher;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

public class EncryptionUtils {

    private static final String ALGORITHM = "AES/GCM/NoPadding";
    private static final int TAG_LENGTH_BIT = 128;
    private static final int IV_LENGTH_BYTE = 12;

    public static String encrypt(String plaintext, String key) throws Exception {
        byte[] iv = generateIV();
        byte[] cipherText;
        byte[] keyBytes = key.getBytes(StandardCharsets.UTF_8);
        SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, "AES");

        Cipher cipher = Cipher.getInstance(ALGORITHM);
        GCMParameterSpec gcmParameterSpec = new GCMParameterSpec(TAG_LENGTH_BIT, iv);
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, gcmParameterSpec);

        cipherText = cipher.doFinal(plaintext.getBytes(StandardCharsets.UTF_8));

        byte[] encrypted = new byte[iv.length + cipherText.length];
        System.arraycopy(iv, 0, encrypted, 0, iv.length);
        System.arraycopy(cipherText, 0, encrypted, iv.length, cipherText.length);

        return Base64.getEncoder().encodeToString(encrypted);
    }

    public static String decrypt(String encryptedText, String key) throws Exception {
        byte[] decoded = Base64.getDecoder().decode(encryptedText);
        byte[] iv = new byte[IV_LENGTH_BYTE];
        byte[] cipherText = new byte[decoded.length - IV_LENGTH_BYTE];

        System.arraycopy(decoded, 0, iv, 0, iv.length);
        System.arraycopy(decoded, iv.length, cipherText, 0, cipherText.length);

        byte[] keyBytes = key.getBytes(StandardCharsets.UTF_8);
        SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, "AES");

        Cipher cipher = Cipher.getInstance(ALGORITHM);
        GCMParameterSpec gcmParameterSpec = new GCMParameterSpec(TAG_LENGTH_BIT, iv);
        cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, gcmParameterSpec);

        byte[] decryptedText = cipher.doFinal(cipherText);

        return new String(decryptedText, StandardCharsets.UTF_8);
    }

    private static byte[] generateIV() {
        byte[] iv = new byte[IV_LENGTH_BYTE];
        // Generate random IV
        // You should use a secure random generator
        for (int i = 0; i < IV_LENGTH_BYTE; ++i) {
            iv[i] = (byte) (Math.random() * 0xFF);
        }
        return iv;
    }

}

使用上述代码,你可以调用encrypt方法对明文进行加密,然后将返回的密文存储或传输。在需要解密时,调用decrypt方法将密文解密为原始明文。

确保在调用加密和解密方法时使用相同的密钥,这样才能成功解密。

相关内容

热门资讯

七分钟专业!荆州晃晃辅助,小唐... 七分钟专业!荆州晃晃辅助,小唐家乐园辅助(果然真的有挂)-哔哩哔哩;1、进入到小唐家乐园辅助黑科技之...
6分钟揭幕!好友赣南能装挂吗,... 6分钟揭幕!好友赣南能装挂吗,竹间茶馆辅助(原来是有挂)-哔哩哔哩6分钟揭幕!好友赣南能装挂吗,竹间...
第1分钟关于!uuganmes... 第1分钟关于!uuganmes辅助器,潮汕激k脚本(都是有挂)-哔哩哔哩1、uuganmes辅助器a...
第9分钟外挂!欢乐达人怎么开辅... 第9分钟外挂!欢乐达人怎么开辅助,玖游卡五星辅助(原来有挂)-哔哩哔哩1、欢乐达人怎么开辅助系统规律...
两分钟关于!打哈儿床将辅助最新... 两分钟关于!打哈儿床将辅助最新,浙江宝宝游戏辅助软件(真是有挂)-哔哩哔哩1、进入到浙江宝宝游戏辅助...
4分钟揭露!夜猫十三道辅助器,... 4分钟揭露!夜猫十三道辅助器,创思维激k辅助下载(原来是真的挂)-哔哩哔哩1、下载好创思维激k辅助下...
第5分钟揭露!杭州都莱辅助软件... 第5分钟揭露!杭州都莱辅助软件,广西老友玩方片破解(本来真的是有挂)-哔哩哔哩1、完成广西老友玩方片...
三分钟总结!广东雀神挂件怎么样... 三分钟总结!广东雀神挂件怎么样,新老夫子开挂系统(切实是有挂)-哔哩哔哩广东雀神挂件怎么样辅助器中分...
第五分钟专业!广东雀神挂件去那... 第五分钟专业!广东雀神挂件去那买,哈糖大菠萝诀窍(一贯真的是有挂)-哔哩哔哩1、全新机制【广东雀神挂...
第8分钟了解!大当家脚本一键挂... 第8分钟了解!大当家脚本一键挂机,丽水都莱辅助软件(一贯真的有挂)-哔哩哔哩大当家脚本一键挂机是一种...