2

我有这个方法:

    public static string DecryptFile(string sInputFilename,
       string sKey)
    {
        DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
        DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
        DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);

        FileStream fsread = new FileStream(sInputFilename,
           FileMode.Open,
           FileAccess.Read);
        ICryptoTransform desdecrypt = DES.CreateDecryptor();

        CryptoStream cryptostreamDecr = new CryptoStream(fsread,
           desdecrypt,
           CryptoStreamMode.Read);

        return new StreamReader(cryptostreamDecr).ReadToEnd();
    }

这是加密方法:

    public static void EncryptFile(string sInputFilename,
       string sOutputFilename,
       string sKey)
    {
        FileStream fsInput = new FileStream(sInputFilename,
           FileMode.Open,
           FileAccess.Read);

        FileStream fsEncrypted = new FileStream(sOutputFilename,
           FileMode.Create,
           FileAccess.Write);
        DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
        DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
        DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
        ICryptoTransform desencrypt = DES.CreateEncryptor();
        CryptoStream cryptostream = new CryptoStream(fsEncrypted,
           desencrypt,
           CryptoStreamMode.Write);

        byte[] bytearrayinput = new byte[fsInput.Length];
        fsInput.Read(bytearrayinput, 0, bytearrayinput.Length);
        cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length);
        cryptostream.Close();
        fsInput.Close();
        fsEncrypted.Close();
    }

在 crypt 文件中,我将字符串与包含数组的逗号放在一起。这是提取该数据的代码:

sSecretKey = Crypt.GenerateKey();
GCHandle gch = GCHandle.Alloc(sSecretKey, GCHandleType.Pinned);

string data = Crypt.DecryptFile(dencrypt, sSecretKey);
Crypt.ZeroMemory(gch.AddrOfPinnedObject(), sSecretKey.Length * 2);
gch.Free();

string[] arr = data.Split(',');

现在,在我运行它之后,我将断点放在 var 的行上,然后我看到它是 null 并且不包含任何内容..

有人知道是什么问题吗?

4

0 回答 0