Node.js解密不适用于其他密码的密文 [英] Node.js decipher not works for other ciphers' ciphertext

查看:148
本文介绍了Node.js解密不适用于其他密码的密文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用javax.crypto.Cipher org.apache.commons.codec.binary.Base64创建了密文,并使用Java,如下所示:

  import java.security.NoSuchAlgorithmException; 
import java.security.NoSuchProviderException;

import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public final class PwdCipher {

private static final Logger LOG = LoggerFactory.getLogger(PwdCipher.class);

private static final int BASE64_ARG0 = 32;

private static final String SECRET =tvnw63ufg9gh5392;

private static byte [] linebreak = {};
private static SecretKey key;
私有静态密码密码;
私有静态Base64编码器;

static {
key = new SecretKeySpec(SECRET.getBytes(),AES);
try {
cipher = Cipher.getInstance(AES / ECB / PKCS5Padding,SunJCE);
} catch(NoSuchAlgorithmException | NoSuchProviderException | NoSuchPaddingException e){
LOG.debug(Erro ao criar encriptador,e);
}
coder = new Base64(BASE64_ARG0,linebreak,true);
}

私人PwdCipher(){
}

public static synchronized String encrypt(String plainText){
try {
cipher.init(Cipher.ENCRYPT_MODE,key);
byte [] cipherText = cipher.doFinal(plainText.getBytes());
return new String(coder.encode(cipherText));
} catch(Exception e){
throw new GdocException(Erro ao encriptar senha。,e);
}
}

public static synchronized String decrypt(String codedText){
try {
byte [] encypted = coder.decode(codedText.getBytes ());
cipher.init(Cipher.DECRYPT_MODE,key);
byte [] decryptpted = cipher.doFinal(encypted);
return new String(decryptpted);
} catch(Exception e){
throw new GdocException(Erro ao decriptar senha。,e);
}
}

}

我需要使用Node.js解密文本(用PwdCipher类加密),但是当我使用以下代码时:

  exports.decryptLdapPassword = function(password){
var key ='tvnw63ufg9gh5392';

var ciphertext = password;

var crypto = require('crypto');
var decipher = crypto.createDecipher('aes-128-ecb',key);

var decryptpted = decipher.update(ciphertext,'base64','utf8');
decrypted + = decipher.final('utf8');

返回解密;
};

结果是以下异常:

 > crypto.js:323 var ret = this._binding.final(); 
> ^ TypeError:error:00000000:lib(0):func(0):reason(0)
>在Decipher.Cipher.final(crypto.js:323:27)
> (Object) )
>在Promise。< anonymous> (C:\ProjetosTFS\GDOC\fSuporte\GDoc\Versionado\Fontes\woodstock\server\api\service\service.controller.js:111:34)
> ;在Promise。< anonymous> (C:\ProjetosTFS\GDOC\fSuporte\GDoc\Versionado\Fontes\woodstock\\\
ode_modules\mongoose\\\
ode_modules\mpromise\lib\promise.js:177:8)
>在Promise.emit(events.js:95:17)
>在Promise.emit(C:\ProjetosTFS\GDOC\fSuporte\GDoc\Versionado\Fontes\woodstock\\\
ode_modules\mongoose\\\
ode_modules\mpromise\lib\promise.js:84 :38)
>在Promise.fulfill(C:\ProjetosTFS\GDOC\fSuporte\GDoc\Versionado\Fontes\woodstock\\\
ode_modules\mongoose\\\
ode_modules\mpromise\lib\promise.js:97 :20)
>在Promise.resolve(C:\ProjetosTFS\GDOC\fSuporte\GDoc\Versionado\Fontes\woodstock\\\
ode_modules\mongoose\lib\promise.js:114:23)
>在Promise。< anonymous> (C:\ProjetosTFS\GDOC\fSuporte\GDoc\Versionado\Fontes\woodstock\\\
ode_modules\mongoose\\\
ode_modules\mpromise\lib\promise.js:177:8)
>在Promise.emit(events.js:95:17)

任何想法?

解决方案

工作!解决方案发布在 https://github.com/joyent/node/issues/20983# issuecomment-100885608


I created ciphertext with javax.crypto.Cipher org.apache.commons.codec.binary.Base64 and using Java like following:

import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;

import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public final class PwdCipher {

    private static final Logger LOG = LoggerFactory.getLogger(PwdCipher.class);

    private static final int BASE64_ARG0 = 32;

    private static final String SECRET = "tvnw63ufg9gh5392";

    private static byte[] linebreak = {};
    private static SecretKey key;
    private static Cipher cipher;
    private static Base64 coder;

    static {
            key = new SecretKeySpec(SECRET.getBytes(), "AES");
            try {
                cipher = Cipher.getInstance("AES/ECB/PKCS5Padding", "SunJCE");
            } catch (NoSuchAlgorithmException | NoSuchProviderException | NoSuchPaddingException e) {
                LOG.debug("Erro ao criar encriptador.", e);
            }
            coder = new Base64(BASE64_ARG0, linebreak, true);
    }

    private PwdCipher(){
    }

    public static synchronized String encrypt(String plainText) {
        try {
            cipher.init(Cipher.ENCRYPT_MODE, key);
            byte[] cipherText = cipher.doFinal(plainText.getBytes());
            return new String(coder.encode(cipherText));
        } catch (Exception e) {
            throw new GdocException("Erro ao encriptar senha.", e);
        }
    }

    public static synchronized String decrypt(String codedText) {
        try {
            byte[] encypted = coder.decode(codedText.getBytes());
            cipher.init(Cipher.DECRYPT_MODE, key);
            byte[] decrypted = cipher.doFinal(encypted);
            return new String(decrypted);
        } catch (Exception e) {
            throw new GdocException("Erro ao decriptar senha.", e);
        }
    }

}

I need to decrypt the text (encrypted with class PwdCipher) using Node.js, but when I use the following code:

exports.decryptLdapPassword = function(password){
  var key = 'tvnw63ufg9gh5392';

  var ciphertext = password;

  var crypto = require('crypto');
  var decipher = crypto.createDecipher('aes-128-ecb', key);

  var decrypted = decipher.update(ciphertext, 'base64', 'utf8');
  decrypted += decipher.final('utf8');

  return decrypted;
};

It turns out the following exception:

> crypto.js:323   var ret = this._binding.final();
>                           ^ TypeError: error:00000000:lib(0):func(0):reason(0)
>     at Decipher.Cipher.final (crypto.js:323:27)
>     at Object.exports.decryptLdapPassword (C:\ProjetosTFS\GDOC\fSuporte\GDoc\Versionado\Fontes\woodstock\server\api\service\service.util.js:14:25)
>     at Promise.<anonymous> (C:\ProjetosTFS\GDOC\fSuporte\GDoc\Versionado\Fontes\woodstock\server\api\service\service.controller.js:111:34)
>     at Promise.<anonymous> (C:\ProjetosTFS\GDOC\fSuporte\GDoc\Versionado\Fontes\woodstock\node_modules\mongoose\node_modules\mpromise\lib\promise.js:177:8)
>     at Promise.emit (events.js:95:17)
>     at Promise.emit (C:\ProjetosTFS\GDOC\fSuporte\GDoc\Versionado\Fontes\woodstock\node_modules\mongoose\node_modules\mpromise\lib\promise.js:84:38)
>     at Promise.fulfill (C:\ProjetosTFS\GDOC\fSuporte\GDoc\Versionado\Fontes\woodstock\node_modules\mongoose\node_modules\mpromise\lib\promise.js:97:20)
>     at Promise.resolve (C:\ProjetosTFS\GDOC\fSuporte\GDoc\Versionado\Fontes\woodstock\node_modules\mongoose\lib\promise.js:114:23)
>     at Promise.<anonymous> (C:\ProjetosTFS\GDOC\fSuporte\GDoc\Versionado\Fontes\woodstock\node_modules\mongoose\node_modules\mpromise\lib\promise.js:177:8)
>     at Promise.emit (events.js:95:17)

Any idea?

解决方案

Worked! Solution published in https://github.com/joyent/node/issues/20983#issuecomment-100885608

这篇关于Node.js解密不适用于其他密码的密文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆