【问题标题】:File_Decryption - JUNK Character found in decrypted fileFile_Decryption - 在解密文件中发现垃圾字符
【发布时间】:2016-12-21 03:18:45
【问题描述】:

我有一个加密工具来加密文件,当我研究加密文件时,发现它在加密文件中写入了.PEM的名称。

I found encryption logic is commonly used as below,
it supporting encryption of any file, it means RSA keys can not be use for encryption so here
it is creating a key(K) and encrypt it with RSA public   key and then using key(K) for encrypting the file.

我编写 C# 代码如下,这很好,但对于大文件,中间会出现一些垃圾字符,

aaaaaaaaaaaaaaaa
??M'yaaaaaaaaaa?

我的解密代码是这样的:-

 System.Security.Cryptography.TripleDESCryptoServiceProvider tripleDES = new System.Security.Cryptography.TripleDESCryptoServiceProvider();
 tripleDES.Key = result; // 16 byte of key
 tripleDES.Mode = System.Security.Cryptography.CipherMode.CBC;
 byte[] IV = { (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 };
 tripleDES.IV = IV;
 tripleDES.Padding = System.Security.Cryptography.PaddingMode.Zeros;
 System.Security.Cryptography.ICryptoTransform cTransform = tripleDES.CreateDecryptor();
 byte[] resultArray = cTransform.TransformFinalBlock(enc_data, 0, enc_data.Length);
 //string s = Convert.ToBase64String(resultArray);
 string x = System.Text.Encoding.ASCII.GetString(resultArray);
 System.IO.File.WriteAllText(@"D:\570_f.txt", x);
 tripleDES.Clear();

1) - 代码几乎可以正常工作,但在某处我发现 8 字节的垃圾字符替换了真实文本。 [主要问题]

.................okokokookokok8bytejunkokokokokokokookko..............8bytjunkokokokokokokokokokoko............

2) - 不知道在加密过程中使用什么填充方案,我尝试使用零填充模式解密。

    ----testing with different length file-----
(A)
     input_file  |encrypted_file with_tool | decrypted_file_with_above_code
     10224 byte  |          x              | 10232 byte        
     ok data + last 8 hex byte 3F 00 00 00 00 00 00 00 
(b)
     input_file  |encrypted_file with_tool | decrypted_file_with_above_code
     10242 byte  |          x              | 10248 byte        
     ok data + last 8 hex byte 0D 3F 3F 3F 3C 56 31 65

(C)
     input_file  |encrypted_file with_tool | decrypted_file_with_above_code
     10258 byte  |          x              | 10264 byte        
     ok data + last 24 hex byte 
     0A 3F 3F 14 4D 27 79 0F 61 61 61 61 61 61 61 61 
     61 61 3F 00 00 00 00 00 

注意 - 文件仅包含字符 a(十六进制值 = 61) 这里的任何建议都会很高兴听到

【问题讨论】:

    标签: c# encryption 3des cbc-mode


    【解决方案1】:

    终于发现这个加密工具是以n字节块的形式输入的。对于 n 字节块没有填充,而任何小于 nbyte 的块都用 80 后跟 00 填充,使其成为 8 的倍数。

    我尝试以相同的方式解密,以 n 字节块的形式划分完整文件,然后解密每个块并将输出保存在缓冲区中,

    最后将整个缓冲区转换为字符串并粘贴到文件中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多