0

こんにちは、C# プログラマーです。C# を使用して DLL を構築し、VBS スクリプトで VBS ファイルを暗号化および復号化しようとしています。

私のコードはこれです:

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.IO;

namespace DELTAGE
{
    [System.Runtime.InteropServices.ComVisible(true)]
    [System.Runtime.InteropServices.ProgId("DELTAGE.DLL")]
    [System.Runtime.InteropServices.Guid("aaaaaaaa-0000-bbbb-1111-cccccccccccccc")]
    public class DeltaGeIO
    {
        // This constant string is used as a "salt" value for the PasswordDeriveBytes function calls.
        // This size of the IV (in bytes) must = (keysize / 8).  Default keysize is 256, so the IV must be
        // 32 bytes long.  Using a 16 character string here gives us 32 bytes when converted to a byte array.
        private const string initVector = "aa00bb11cc22dd33";

        // This constant is used to determine the keysize of the encryption algorithm.
        private const int keysize = 256;

        private string debug;

        public string cryptDecryptScript(string nameFileToCrypt)
        {
            try
            {
                string prova = writeVBScriptEncrypt(nameFileToCrypt, "");
                prova = writeVBScriptDecrypt("testCrypt.txt");
                return prova;
            }
            catch
            {
                return debug;
            }
        }

        public string writeVBScriptEncrypt(string nameFile, string nameScript)
        {
            byte[] bytes = System.IO.File.ReadAllBytes(nameFile);
            string[] lines = new string[1];
            lines[0] = this.EncryptBytes(bytes, "test");
            try
            {
                System.IO.File.Delete("testCrypt.txt");
            }
            catch
            {
                debug = "WVBSSE - i cannot delete testCrypt.txt file";
            }
            System.IO.File.WriteAllLines("testCrypt.txt", lines);
            return "OK file Encrypted";
        }

        public string writeVBScriptDecrypt(string nameFile)
        {
            byte[] bytes = System.IO.File.ReadAllBytes(nameFile);
            string[] lines = new string[1];
            lines[0] = this.DecryptBytes(bytes, "test");
            System.IO.File.WriteAllLines("testDecrypt.vbs", lines);
            try
            {
                System.IO.File.Delete("testCrypt.txt");
            }
            catch
            {
                debug = "WVBSSD - i cannot delete testCrypt.txt file";
            }
            return "OK file Decrypted";
        }

        private string Encrypt(string plainText, string passPhrase)
        {
            byte[] initVectorBytes = Encoding.UTF8.GetBytes(initVector);
            byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);
            PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, null);
            byte[] keyBytes = password.GetBytes(keysize / 8);
            RijndaelManaged symmetricKey = new RijndaelManaged();
            symmetricKey.Mode = CipherMode.CBC;
            ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes);
            MemoryStream memoryStream = new MemoryStream();
            CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write);
            cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
            cryptoStream.FlushFinalBlock();
            byte[] cipherTextBytes = memoryStream.ToArray();
            memoryStream.Close();
            cryptoStream.Close();
            return Convert.ToBase64String(cipherTextBytes);
        }

        private string Decrypt(string cipherText, string passPhrase)
        {
            byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
            byte[] cipherTextBytes = Convert.FromBase64String(cipherText);
            PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, null);
            byte[] keyBytes = password.GetBytes(keysize / 8);
            RijndaelManaged symmetricKey = new RijndaelManaged();
            symmetricKey.Mode = CipherMode.CBC;
            ICryptoTransform decryptor = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes);
            MemoryStream memoryStream = new MemoryStream(cipherTextBytes);
            CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);
            byte[] plainTextBytes = new byte[cipherTextBytes.Length];
            int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
            memoryStream.Close();
            cryptoStream.Close();
            return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);
        }

        private string EncryptBytes(byte[] plainText, string passPhrase)
        {
            byte[] initVectorBytes = Encoding.UTF8.GetBytes(initVector);
            byte[] plainTextBytes = plainText;
            PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, null);
            byte[] keyBytes = password.GetBytes(keysize / 8);
            RijndaelManaged symmetricKey = new RijndaelManaged();
            symmetricKey.Mode = CipherMode.CBC;
            ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes);
            MemoryStream memoryStream = new MemoryStream();
            CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write);
            cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
            cryptoStream.FlushFinalBlock();
            byte[] cipherTextBytes = memoryStream.ToArray();
            memoryStream.Close();
            cryptoStream.Close();
            return Convert.ToBase64String(cipherTextBytes);
        }

        private string DecryptBytes(byte[] cipherText, string passPhrase)
        {
            byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
            byte[] cipherTextBytes = cipherText;
            PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, null);
            byte[] keyBytes = password.GetBytes(keysize / 8);
            RijndaelManaged symmetricKey = new RijndaelManaged();
            symmetricKey.Mode = CipherMode.CBC;
            ICryptoTransform decryptor = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes);
            MemoryStream memoryStream = new MemoryStream(cipherTextBytes);
            CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);
            byte[] plainTextBytes = new byte[cipherTextBytes.Length];
            debug = "here 1"; 
            int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
            debug = "here 1";
            memoryStream.Close();
            debug = "here 3" + decryptedByteCount.ToString();
            cryptoStream.Close();
            debug = "here 4";
            return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);
        }
    }
}

したがって、Encrypt と Decrypt を使用して文字列をデコードおよびデコードしようとすると、正常に動作します。

しかし、EncryptBytes と DecryptBytes を使用してファイルを暗号化し、暗号化されたファイルを復号化しようとすると、次のコード行にエラーが発生します。

int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);

何か案は?ありがとう

dll を呼び出す Ps VBS コードは次のとおりです。

Dim mObj, strResult
set mObj = CreateObject("DELTAGE.DLL")
strResult = mObj.cryptDecryptScript("file.vbs")
MsgBox "Result: " + strResult
4

1 に答える 1