首页 > 解决方案 > AES 256 加密解密,

问题描述

解密逻辑缺少某些内容,请您帮忙。输出未完全解密。

Java 加密逻辑:

public static void main(String[] args) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, ShortBufferException, IllegalBlockSizeException, BadPaddingException {

    try {

        String in ="This is a text message";

        byte[] input = in.toString().getBytes("utf-8");
        String ENCRYPTION_KEY = "RW50ZXIgS2V5IEhlcmU=";
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        byte[] thedigest = md.digest(ENCRYPTION_KEY.getBytes("UTF-8"));
        // SecretKeySpec skc = new SecretKeySpec(thedigest, "AES/ECB/PKCS5Padding");
        SecretKeySpec skc = new SecretKeySpec(thedigest, "AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.ENCRYPT_MODE, skc);

        byte[] cipherText = new byte[cipher.getOutputSize(input.length)];
        int ctLength = cipher.update(input, 0, input.length, cipherText, 0);
        ctLength += cipher.doFinal(cipherText, ctLength);

        // String query = Base64.encodeToString(cipherText, Base64.DEFAULT);
        String query = new String(java.util.Base64.getEncoder().encode(cipherText));
        System.out.println("query " + query);
        // String query = new String(encode(cipherText), StandardCharsets.ISO_8859_1);
    } catch(UnsupportedEncodingException e) { // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

Nodejs解密逻辑:

let crypto = require('crypto');
var decipher = crypto.createDecipher('aes-256-ecb', "RW50ZXIgS2V5IEhlcmU=");
decipher.setAutoPadding(false);
console.log(decipher.update("EncyptedText", 'base64', 'utf8') + decipher.final('utf8'));

标签: encryptioncryptographyaescryptojsnode-crypto

解决方案


推荐阅读