Search code examples
c#encryption3descbc-mode

File_Decryption - JUNK Character found in decrypted file


I have a encryption tool to encrypt the file, when I study the encrypted file, found it is writing name of .PEM inside the encrypted file.

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.

I write C# Code as below, it is fine but for big file am getting some junk character in middle like,

aaaaaaaaaaaaaaaa
??M'yaaaaaaaaaa?

my decryption code is like:-

 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) -code is working fine almost but somewhere I found 8 byte of junk character replacing real text. [ main problem]

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

2) - don't know what padding scheme is using during encryption process, I tried decryption with zero padding mode.

    ----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 

NOTE - FILE CONTAIN only character a (HEX VALUE = 61) any advice here would be great to hear


Solution

  • Finally found this encyption tool is taking input in the form of block of n byte. For n byte block there was no padding while any block less than nbyte is padded by 80 followed by 00 to make it multiple of 8.

    I try to decrypt in same way, devide full file in form of n byte block and then decrypt each block and save output in a buffer,

    and at last convert full buffer into string and paste in a file.