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);
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";
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);
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.