如何使用 Node JS 解密使用 C# 加密的文件

How to use Node JS to decrypt a file that was encrypted using C#

我有用 C# 加密的文件(使用 DES、PKCS7)。我需要在 Node JS 中解码这些文件。

我用 C# 解密的代码如下所示:

public string SSFile_Reader( string fileToDecrypt )
{
  DESCryptoServiceProvider provider = new DESCryptoServiceProvider
  {
    Key = Encoding.UTF8.GetBytes( "13^07v90" ),
    IV = Encoding.UTF8.GetBytes( "13^07v90" ),
    Padding = PaddingMode.PKCS7
  };
  
  using( FileStream streamToDecrypt = new FileStream( fileToDecrypt, FileMode.Open, FileAccess.Read, FileShare.ReadWrite ) )
  {
    ICryptoTransform cryptoTransform = provider.CreateDecryptor();
    string outputString = "";

    using( CryptoStream stream2 = new CryptoStream( streamToDecrypt, cryptoTransform, CryptoStreamMode.Read ) )
    {
      try
      {
        using( StreamReader reader = new StreamReader( stream2 ) )
        {
          try
          {
            outputString = reader.ReadToEnd();
          }
          catch
          {
           //handle error here
          }
          stream2.Close();
          streamToDecrypt.Close();

          return outputString;
        }
      }
      catch( Exception exception )
      {
        //handle error here
      }
    }
  }

  return '';
}

我确实需要将以上内容转换为 Node JS。我试过下面的 Node JS 代码,但输出只是一些随机的东西,而不是原始的加密字符串:

const { Readable } = require("stream");
const { scrypt, scryptSync, randomFill, createCipheriv, createDecipheriv } = require('crypto');
const fs = require('fs');

const [, , pathToEncryptedFile] = process.argv;

if (!pathToEncryptedFile) {
  console.log('No file to decrypt')
  exit()
}

const keyAndIv = '31335e3037763930'; //hex equivalence of 13^07v90
const key = Buffer.from(keyAndIv, 'hex');
const iv = Buffer.from(keyAndIv, 'hex');

const decryptedData = '';

const decipher = createDecipheriv('des', key, iv);

const readableStream = Readable.from(fs.createReadStream(pathToEncryptedFile)
  .pipe(decipher));

readableStream.on("data", (chunk) => {
  decryptedData += chunk.toString()
})

readableStream.on('end', function () {
  console.log({decryptedData})
});

readableStream.on('error', function (err) {
  console.log({err})
});

我也试过使用 crypto-js to no avail (https://github.com/brix/crypto-js/issues/396).

这是我需要解密的文件之一的示例:https://files.fm/u/6pewftkk2

如果上面给出的用于解密的C#代码不够用,我也可以给出加密的C#代码

一种可能的变体是从文件系统中完全加载密文并解密:

var crypto = require('crypto')
var fs = require('fs')

const algorithm = 'des'; // defaults to 'des-cbc';
const password = 'Password used to generate key';
const key = '13^07v90';
const iv = '13^07v90'; 

const ciphertext = fs.readFileSync('<path to high_classes.ssdata>');
const decipher = crypto.createDecipheriv(algorithm, key, iv);
const plaintext = decipher.update(ciphertext, '', 'utf8') + decipher.final();
console.log(plaintext);

输出以下明文(对于链接文件):

SSS1
SSS2
SSS3

或者,特别是对于大数据,明文也可以流式传输到文件中。为此,将最后一个块替换为:

const decipher = crypto.createDecipheriv(algorithm, key, iv);
const readStream = fs.createReadStream('<path to high_classes.ssdata>');
const writeStream = fs.createWriteStream('<path to file where decrypted data should be saved>');
readStream.pipe(decipher).pipe(writeStream);

创建一个包含解密数据的文件。


请注意,如今 DES 已过时且不安全。使用密钥作为 IV 也是不安全的。通常,在加密过程中会生成一个随机 IV,并与密文(通常是串联的)一起传递给另一方。