Node.js解密不适用于其他密码的密文

编程入门 行业动态 更新时间:2024-10-26 02:37:22
本文介绍了Node.js解密不适用于其他密码的密文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我使用javax.crypto.Cipher org.apachemons.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.apachemons.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)

任何想法?

解决方案

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

I created ciphertext with javax.crypto.Cipher org.apachemons.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.apachemons.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 github/joyent/node/issues/20983#issuecomment-100885608

更多推荐

Node.js解密不适用于其他密码的密文

本文发布于:2023-11-28 19:47:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1643723.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不适用于   密码   Node   js

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!