在vc++中解密已在c# . net中加密的字符串

本文关键字:加密 字符串 net vc++ 解密 | 更新日期: 2023-09-27 18:15:10

我想解密已经被c#应用程序加密的字符串。

      public static class clsEncryptions
{
    public static string GetKey()
    {
        var key = new { key = "MyKey" };
        return key.key;
    }
    public static string Encrypt(this string EncryptString)
    {
        if (EncryptString == string.Empty)
            return string.Empty;
        byte[] clearBytes =
          System.Text.Encoding.Unicode.GetBytes(EncryptString);
        PasswordDeriveBytes pdb = new PasswordDeriveBytes(GetKey(),
            new byte[] {0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 
        0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76});
        MemoryStream ms = new MemoryStream();
        Rijndael alg = Rijndael.Create();
        alg.Key = pdb.GetBytes(32);
        alg.IV = pdb.GetBytes(16);
        CryptoStream cs = new CryptoStream(ms, alg.CreateEncryptor(),CryptoStreamMode.Write);
        cs.Write(clearBytes, 0, clearBytes.Length);
        cs.Close();
        return Convert.ToBase64String(ms.ToArray());
    }

    public static string Decrypt(this string DecryptString)
    {
        if (DecryptString == string.Empty)
            return string.Empty;
        byte[] cipherBytes = Convert.FromBase64String(DecryptString);
        PasswordDeriveBytes pdb = new PasswordDeriveBytes(GetKey(),
                        new byte[] {0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 
        0x64, 0x76, 0x65, 0x64, 0x65, 0x76});
        MemoryStream ms = new MemoryStream();
        Rijndael alg = Rijndael.Create();
        alg.Key = pdb.GetBytes(32);
        alg.IV = pdb.GetBytes(16);
        CryptoStream cs = new CryptoStream(ms, alg.CreateDecryptor(), CryptoStreamMode.Write);
        cs.Write(cipherBytes, 0, cipherBytes.Length);
        cs.Close();
        return System.Text.Encoding.Unicode.GetString(ms.ToArray());
    }
}

请帮助我将解密逻辑转换为vc++ (MFC项目),以便我能够在我的vc++应用程序中读取加密文件。

在vc++中解密已在c# . net中加密的字符串

你有没有试过看那些

  1. http://www.cryptopp.com/这是一个c++加密库,可以帮助你
  2. http://pocoproject.org/这是一个c++框架,里面有你需要的加密类

我的建议是使用cryptopp来获得更小的应用程序大小,但是poco在代码编写中更容易,并且在静态链接中产生更大的exe大小,我注意到这可能不正确。