Javaへの類似ノードJSでのAES暗号化/復号化

Prashant・クマール・シャルマ:

私は、複製しようとしていたJavaのコードAES暗号化と復号ノードJSを

Javaコード

    SecretKeySpec skeySpec;
    String key = "a4e1112f45e84f785358bb86ba750f48";

    public void encryptString(String key) throws Exception {
        try {
            skeySpec = new SecretKeySpec(key.getBytes(), "AES");
            cipher = Cipher.getInstance("AES");
            cipher.init(1, skeySpec);
            byte encstr[] = cipher.doFinal(message.getBytes());
            String encData = new String(encstr, "UTF-8");
            System.out.println(encData);
        } catch (NoSuchAlgorithmException nsae) {
            throw new Exception("Invalid Java Version");
        } catch (NoSuchPaddingException nse) {
            throw new Exception("Invalid Key");
        }
    }

ノードJS

    var encryptKey = function (text) {
        var cipher = crypto.createCipher('aes256', 'a4e1112f45e84f785358bb86ba750f48');
        var crypted = cipher.update(text,'utf8', 'hex')
        crypted += cipher.final('hex');
        console.log(crypted);
        return crypted;
    }

私は正確に取得することができません暗号文におけるノードJS私は取得しています、Javaの

Prashant・クマール・シャルマ:

最後に確認した後、JavaのドキュメントノードJS暗号ドキュメントは、結果を得ることができました。私たちは、使用する必要がcrypto.createCipheriv()代わりにcrypto.createCipherIVと。ここIVになりますnull

コード:

    let crypto = require('crypto');

    var iv = new Buffer.from('');   //(null) iv 
    var algorithm = 'aes-256-ecb';
    var password = 'a4e1112f45e84f785358bb86ba750f48';      //key password for cryptography

    function encrypt(buffer){
        var cipher = crypto.createCipheriv(algorithm,new Buffer(password),iv)
        var crypted = Buffer.concat([cipher.update(buffer),cipher.final()]);
        return crypted;
    }

    console.log(encrypt(new Buffer('TextToEncrypt')).toString())

おすすめ

転載: http://10.200.1.11:23101/article/api/json?id=478797&siteId=1