用C加密并用C#解密 [英] Encrypt in c and decrypt in c#

查看:102
本文介绍了用C加密并用C#解密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我想使用Rijndael算法在c或c ++中加密文件并在c#中对其解密.

C#应用程序解密文件但未显示正确的输出:它仅显示垃圾字符.

C代码:

Hi all,

I want to encrypt file in c or c++ and decrypt it in c# by using Rijndael algorithm.

C# app decrypt the file but not showing correct output: it shows junk chars only.

c code :

void main()
{
 CRijndael oRijndael;
  oRijndael.MakeKey("512345678901234561234567890123456", CRijndael::sm_chain0,32,16);
  char szDataIn1[49] = "sunil pol";
  char szDataIn[49];
  char szDataOut[49];
  memset(szDataIn, 0, 49);
  memset(szDataOut, 0, 49);

  //Test ECB
  strcpy(szDataIn, szDataIn1);
  memset(szDataOut, 0, 49);
  oRijndael.Encrypt(szDataIn, szDataOut, 48, CRijndael::ECB);
  cout << szDataOut << endl;
  FILE *fp =NULL ;
 fp = fopen("F:\\Encoding and Decoding\\CEncrypt.txt","ab+") ;
 fwrite(szDataOut,strlen(szDataOut),1,fp);
 fclose(fp) ;
  memset(szDataIn, 0, 49);
  oRijndael.Decrypt(szDataOut, szDataIn, 48, CRijndael::ECB);
  cout << szDataIn << endl;*/

}


净代码:


.Net code :

public static void Decrypt()
        {
            string password = @"512345678901234561234567890123456"; //Your Key Here

            Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(password, new byte[] { 0x26, 0xdc, 0xff, 0x00, 0xad, 0xed, 0x7a, 0xee, 0xc5, 0xfe, 0x07, 0xaf, 0x4d, 0x08, 0x22, 0x3c });

            FileStream fsCrypt = new FileStream(@"F:\Encoding and Decoding\CEncrypt.txt", FileMode.Open);

            RijndaelManaged RMCrypto = new RijndaelManaged();
            RMCrypto.Mode=CipherMode.ECB;
            RMCrypto.Padding = PaddingMode.None;
            RMCrypto.KeySize = 256;
            RMCrypto.BlockSize = 128;
            
            byte[] key = pdb.GetBytes(RMCrypto.KeySize / 8);
            byte[] iv = pdb.GetBytes(RMCrypto.BlockSize /8);         

            CryptoStream cs = new CryptoStream(fsCrypt, RMCrypto.CreateDecryptor(key, iv), CryptoStreamMode.Read);
         //   CryptoStream cs = new CryptoStream(fsCrypt, RMCrypto.CreateDecryptor(), CryptoStreamMode.Read);


            FileStream fsOut = new FileStream(@"F:\Encoding and Decoding\ReadMe_Decrypted.txt", FileMode.Create);

            int data;
            while ((data = cs.ReadByte()) != -1)
                fsOut.WriteByte((byte)data);

            fsOut.Close();
            cs.Close();
            fsCrypt.Close();
        }



你能帮我发现这里发生了什么吗?

预先谢谢您.



Could you help me finding what is happening here?

Thank you in advance.

推荐答案

在不了解Rijndael的C#实现的任何情况下,我看到了几件事引起了我的注意:

a)加密文本的写入不正确:

Without knowing anything about the C# implementation of Rijndael, I see a couple things that caught my attention:

a) The writing of the encrypted text is done incorrectly:

fwrite(szDataOut,strlen(szDataOut),1,fp);



szDataOut不是以NUL结尾的字符串,而是二进制数据.因此,您无法通过strlen确定其长度.您可能想将整个块写完整.

b)输出文件以"ab +"模式打开,因此,每次运行测试应用程序时,您都将附加到该文件.您可能需要"wb"代替.

c)密钥生成在C ++方面对每个字符串使用8位,而在C#方面对Unicode字符串进行操作.我不确定这两种算法是否会达到相同的二进制键值.

希望对您有所帮助.



szDataOut is not a NUL-terminated character string, but binary data. Hence you cannot determine its length by strlen. You probably want to write the entire block in its full length.

b) The ouput file is opened with mode "ab+", hence you will be appending to the file each time you run your test application. You probably want "wb" instead.

c) The key generation operates on 8-bit per character string on the C++ side and on a Unicode string on the C# side. I am not sure, whether both algorithms will arrive at the same binary key value.

Hope that helps.


这是我的建议(不是解决方案):

1.默认情况下,dotNET使用unicode字符串进行操作,因此在您的C ++代码中,尝试不对CHAR进行编码,而对WCHAR进行编码.

2. dotNET在其编码/解码实现中使用了Crypt API包装(不仅对于其他算法也使​​用AES),因此您可以尝试在C ++应用程序中使用Crypt API,或者为Crypt API中的一些功能创建托管包装并使用它们同样在托管代码中,因此通过这种方式,您可以确保代码相似.

问候,
Maxim.
Here is my suggestions (not the solution):

1. dotNET operate with unicode strings by default, so in your C++ code try to encode not CHAR but WCHAR.

2. dotNET uses Crypt API wrapping inside their implementation of encoding/decoding (not only AES for for other algorithms too), so you can try using Crypt API in your C++ app, or make managed wrapper for few functions in Crypt API and use them in managed code too, so this way you will be sure that code is similar.

Regards,
Maxim.


这篇关于用C加密并用C#解密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆