简体   繁体   English

我正在尝试将 Java AES 加密转换为 NodeJ。 这是我迄今为止尝试过的

[英]I am trying to convert Java AES encryption to NodeJs. This is what I have tried so far

Java encryption code. Java 加密代码。

import java.security.spec.KeySpec;
import java.util.Base64;
import java.util.List;
import java.util.Map;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;

import org.json.simple.JSONObject;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.jarvis.accountopeningv2.model.PartChanRequestDTO;
import com.jarvis.accountopeningv2.model.TemplateDTO;

public class SecurityService {

    private static final String ALGORITHM = "AES/CBC/PKCS5PADDING";
    
    
    public static String encryptpayload(String value, String key) {
        String encryptedData = null;
        try {
            
            
            IvParameterSpec iv = new IvParameterSpec(key.substring(0, 16).getBytes("UTF-8"));
            SecretKeySpec skeySpec = new SecretKeySpec(key.substring(0, 32).getBytes("UTF-8"), "AES");
    
            Cipher cipher = Cipher.getInstance(ALGORITHM);
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);

            byte[] encrypted = cipher.doFinal(value.getBytes());
            encryptedData = Base64.getEncoder().encodeToString(encrypted);
        } catch (Exception ex) {
            ex.printStackTrace();
            encryptedData = null;
        }
        return encryptedData;
    }
    
}

This is what I have tried.这是我尝试过的。

function SecurityFunc(request) {

        const digest = 'SHA256';
            var secretkey = 'fgbnhgfcjhgfcvjkhgfcvjkhgfcvbjbnvcjhnbvcfghjnbvc';     // 256 character
            var iv = Buffer.from(secretkey.substring(0,16), 'utf-8', 'aes');
            const key = crypto.pbkdf2Sync(secretkey, 'boooooo!!', 65536, 32, digest);
            const cipher = crypto.createCipheriv('aes-256-cbc', key, iv);
            let encrypted = cipher.update(`${request}`, 'ascii', 'base64');
            encrypted += cipher.final('base64');
            return encrypted;

}

I have searched a lot about this.我对此进行了很多搜索。 I got these questions on stack overflow我在堆栈溢出时遇到了这些问题

Difference in key lengths between crypto.pbkdf2 (node.js) and PBEKeySpec, AES/CBC/PKCS5PADDING IV - Decryption in NodeJs (Encrypted in Java) I have tried both of them but none of them resolved my query. crypto.pbkdf2 (node.js) 和 PBEKeySpec、AES/CBC/PKCS5PADDING IV 之间的密钥长度差异 - NodeJs 中的解密(用 Java 加密)我已经尝试了它们,但它们都没有解决我的查询。

Any help will be highly appreciated.任何帮助将不胜感激。

Are you trying to make the 2 compatible or just port the code from Java to JavaScript?您是想让 2 兼容还是只是将代码从 Java 移植到 JavaScript?

Update: apparently PKCS#5 will work the same as for PKCS#7 so that isn't your issue.更新:显然 PKCS#5 与 PKCS#7 的工作方式相同,所以这不是你的问题。

The key length should be 32-bytes in both cases.在这两种情况下,密钥长度都应该是 32 字节。

If you don't need backwards compatibility, I'd recommend using AES in GCM mode rather than CBC as it includes tamper protection.如果您不需要向后兼容性,我建议在 GCM 模式下使用 AES 而不是 CBC,因为它包含篡改保护。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AES加密我正在尝试使用AES加密进行加密 - AES Encryption I am trying to encrypt using AES encryption 我试图以小时和分钟(00:00)的形式在我的代码中输出时间,并且需要帮助。 这就是我到目前为止 - I am trying to output the times in my code in hours and mins (00:00) and need help. This is what i have so far 如何通过 selenium webdriver (java) 使用 Tor 浏览器? 到目前为止,我已经尝试过以下代码,但收到消息:'tor failed to start' - How to use tor browser using selenium webdriver (java)? I have tried below code so far but getting message: 'tor failed to start' 我想打印到目前为止我创建的所有帐号和帐号; 我正在使用 java - I want to print all the account numbers and account names that I have created so far ; I am using java 我正在尝试使用Java进行“ AES / CCM / PKCS5Padding”加密,但是我遇到了一些例外情况,有人可以帮助我如何使用CCM代码进行加密 - i am trying “AES/CCM/PKCS5Padding” encryption in java but i am getting some exception can any one help me how to do encryption using CCM code 我在javascript中卡住了AES加密,然后在JAVA spring中解密了 - I have stuck for AES Encryption in javascript and then decryption in JAVA spring 如何将Java AES ECB加密代码转换为Node.js - How to convert Java AES ECB Encryption Code to Nodejs 我正在尝试下载android studio,但它一直告诉我要安装Java。 请告诉我该怎么办? - I am trying to download android studio but it keeps telling me to install Java which i have done so. Please tell me what i should do? 试图用Java设计数据库。 到目前为止,我有一个可序列化的对象,可以在需要时读取和更新。 好主意还是坏主意? - Trying to design a database in Java. So far I have a serializable object which I read and update when I need to. Good or bad idea? 请求的资源上不存在“Access-Control-Allow-Origin”header。 我试过在这里查看其他解决方案,但到目前为止没有成功 - No 'Access-Control-Allow-Origin' header is present on the requested resource. I have tried looking at other solutions on here but so far unsuccessful
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM