问题
I have used Rijndael (Encryption Algorithm) in my server side code, which is in C language. But my client is written in C# and C# provide its own Rijndael class to encrypt and decrypt.
At client side I am using the same password for key generation but the client application is unable to decrypt it. I want to encrypt a file in C and decrypt that file in .NET (C#).
Server Code : http://www.efgh.com/software/rijndael.txt
Client Code :
public static void Encrypt()
{
string password = @"4c696e6775614e6578742431302a4c6f63616c697a6174696f6e2a3949505f3030372a"; // Your Key Here
/*UnicodeEncoding UE = new UnicodeEncoding();
byte[] key = UE.GetBytes(password);*/
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes (password, new byte[] { 0x26, 0xdc, 0xff, 0x00, 0xad, 0xed, 0x7a, 0xee, 0xc5, 0xfe, 0x07, 0xaf, 0x4d, 0x08, 0x22, 0x3c });
string cryptFile = @"F:\Encoding and Decoding\ReadMe_Encrypted.txt";
FileStream fsCrypt = new FileStream(cryptFile, FileMode.Create);
RijndaelManaged RMCrypto = new RijndaelManaged();
RMCrypto.KeySize = 256;
RMCrypto.BlockSize = 256;
byte[] key = pdb.GetBytes(RMCrypto.KeySize / 8);
byte[] iv = pdb.GetBytes(RMCrypto.BlockSize / 8);
CryptoStream cs = new CryptoStream(fsCrypt,
RMCrypto.CreateEncryptor(key, iv),
CryptoStreamMode.Write);
FileStream fsIn = new FileStream(@"F:\Encoding and Decoding\ReadMe.txt", FileMode.Open);
int data;
while ((data = fsIn.ReadByte()) != -1)
cs.WriteByte((byte)data);
fsIn.Close();
cs.Close();
fsCrypt.Close();
}
public static void Decrypt()
{
string password = @"4c696e6775614e6578742431302a4c6f63616c697a6174696f6e2a3949505f3030372a"; // Your Key Here
/* UnicodeEncoding UE = new UnicodeEncoding();
byte[] key = UE.GetBytes(password);*/
// PasswordDeriveBytes pdb = new PasswordDeriveBytes(password, new byte[] { 0x26, 0xdc, 0xff, 0x00, 0xad, 0xed, 0x7a, 0xee, 0xc5, 0xfe, 0x07, 0xaf, 0x4d, 0x08, 0x22, 0x3c });
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\ReadMe_Encrypted.txt", FileMode.Open);
RijndaelManaged RMCrypto = new RijndaelManaged();
RMCrypto.KeySize = 256;
RMCrypto.BlockSize = 256;
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();
}
回答1:
In your C# code you're using Rfc2898DeriveBytes
to convert your password string into a 256-bit key (under the hood this is actually hashing the password using SHA-1), whereas in the C code you're using the key directly. You probably want to modify the C# code to use the key directly (it looks like a 256-bit key to me, not an ASCII password).
The following should be sufficient:
byte[] key = {0x4c, 0x69, 0x6e, 0x67, ...};
You'll also want to modify the C program to use the correct binary key, rather than taking one on the command line. You can use something like the following:
unsigned char key[KEYLENGTH(KEYBITS)] = {0x4c, 0x69, 0x6e, 0x67, ...};
I'm also not sure if you're using the same mode (http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation). I think the C# code may be using CBC whereas the C code is using ECB. I suggest this as there's an iv in the C# code (generated from the password), but there isn't one in the C code.
You could also try to find a library that you can use from both C and C# and use that instead, for example openssl.
来源:https://stackoverflow.com/questions/11858456/encryption-in-c-lang-and-decryption-in-c-sharp