【问题标题】:AES encryption in Node.js and decryption on ScalaNode.js 中的 AES 加密和 Scala 中的解密
【发布时间】:2019-02-13 13:54:40
【问题描述】:

我正在尝试在 Scala 上解密 AES(使用 javax.crypto.Cipher),密文在 Node.js 应用程序中被加密(使用 aes-js)。并得到一个错误

  • 我正在使用电子密码本

节点(加密)

var aesjs = require("aes-js");

// An example 128-bit key
var key = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 ];
var key_128_array = new Uint8Array(key);

var key_128_hex = aesjs.utils.hex.fromBytes(key_128_array);
console.log(key_128_hex);
// 0102030405060708090a0b0c0d0e0f10


// Convert text to bytes
var text = 'TextMustBe16Byte';
var textBytes = aesjs.utils.utf8.toBytes(text);

var aesEcb = new aesjs.ModeOfOperation.ecb(key_128_array);
var encryptedBytes = aesEcb.encrypt(textBytes);

// To print or store the binary data, you may convert it to hex
var encryptedHex = aesjs.utils.hex.fromBytes(encryptedBytes);
console.log(encryptedHex);
// "a7d93b35368519fac347498dec18b458"

Scala(解密)

import org.bouncycastle.jce.provider.BouncyCastleProvider
import javax.crypto.Cipher

val cipher = Cipher.getInstance("AES", new BouncyCastleProvider())

val key = "0102030405060708090a0b0c0d0e0f10";
val aesKeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES")
cipher.init(Cipher.DECRYPT_MODE, aesKeySpec)

val msg = "a7d93b35368519fac347498dec18b458"
val decrypted = cipher.doFinal(msg.getBytes())
log.debug(s"decrypted data ${decrypted}")

我收到以下错误:

javax.crypto.BadPaddingException: pad block corrupted
        at org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher$BufferedGenericBlockCipher.doFinal(Unknown Source)
        at org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher.engineDoFinal(Unknown Source)
        at javax.crypto.Cipher.doFinal(Cipher.java:2164)
...

知道我做错了什么吗?任何帮助将不胜感激。

【问题讨论】:

    标签: node.js scala encryption aes


    【解决方案1】:

    问题在于密钥和消息格式。密钥表示为十六进制字符串,调用.getBytes 将返回UTF-8 中的字符字节,而不是实际的密钥字节。 Java 8 有javax.xml.bind.DatatypeConverter,可以将十六进制字符串转换为正确的Array[Byte]。和msg一样,要正确转换成Array[Byte]

    您必须指定密码 AES/ECB/NoPadding,就像在 node-js 中使用 ecb 一样。

    import javax.crypto.spec.SecretKeySpec
    import javax.xml.bind.DatatypeConverter
    
    object DecryptApp extends App {
    
      import org.bouncycastle.jce.provider.BouncyCastleProvider
      import javax.crypto.Cipher
    
      val cipher = Cipher.getInstance("AES/ECB/NoPadding", new BouncyCastleProvider())
    
      val key = "0102030405060708090a0b0c0d0e0f10"
      val keyBytes = DatatypeConverter.parseHexBinary(key)
      val aesKeySpec = new SecretKeySpec(keyBytes, "AES")
      cipher.init(Cipher.DECRYPT_MODE, aesKeySpec)
    
      val msg = "a7d93b35368519fac347498dec18b458"
      val decrypted = cipher.doFinal(DatatypeConverter.parseHexBinary(msg))
      println(s"decrypted data ${decrypted.map(_.toChar).mkString}")
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-31
      • 2011-08-27
      • 2013-06-22
      • 2019-03-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多