我已經在我的服務器端代碼,它是在C語言中使用Rijndael(加密算法)。但是我的客戶端是用C#編寫的,C#提供了它自己的Rijndael類來加密和解密。c lang加密。並在c#解密#
在客戶端,我爲密鑰生成使用相同的密碼,但客戶端應用程序無法對其解密。我想用C加密一個文件,並用.NET(C#)解密該文件。
服務器代碼:
客戶端代碼:
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();
}
歡迎來到StackOverflow。首先,您應該提供一些相關的代碼細節,並描述您嘗試過的以及您失敗的方式。其次,「請回復」可能被一些人視爲粗魯和注意力尋求。 StackOverflow首先是*社區*的網站,並且僅限於第二個網站:如果您的問題經過充分研究和有趣,人們會回答它,如果不是,他們不會。 – 2012-08-08 05:59:41
這不是關於您使用的語言,而是關於算法及其實現。 – 2012-08-08 06:02:34
@RenéKolařík該語言非常相關,因爲許多這類算法都是使用按位運算符以C編寫的,這在其他語言中可能不適用。 – Lundin 2012-08-08 06:09:25