I have got to my self these two functions (Encryption and Decryption):
public static byte[] Encrypt(string plaintext, byte[] key)
{
using (Rijndael desObj = Rijndael.Create())
{
desObj.Key = key;
desObj.Mode = CipherMode.CFB;
desObj.Padding = PaddingMode.PKCS7;
using (MemoryStream ms = new MemoryStream())
{
ms.Write(desObj.IV, 0, desObj.IV.Length);
using (CryptoStream cs = new CryptoStream(ms, desObj.CreateEncryptor(), CryptoStreamMode.Write))
{
byte[] plainTextBytes = Encoding.UTF8.GetBytes(plaintext);
cs.Write(plainTextBytes, 0, plainTextBytes.Length);
}
return ms.ToArray();
}
}
}
public static string Decrypt(byte[] cyphertext, byte[] key)
{
using (MemoryStream ms = new MemoryStream(cyphertext))
using (var desObj = Rijndael.Create())
{
desObj.Key = key;
desObj.Mode = CipherMode.CFB;
desObj.Padding = PaddingMode.PKCS7;
//Read the IV from the front of the stream and assign it to our object.
var iv = new byte[16];
var offset = 0;
while (offset < iv.Length)
{
offset += ms.Read(iv, offset, iv.Length - offset);
}
desObj.IV = iv;
//Read the bytes to be decrypted
using (var cs = new CryptoStream(ms, desObj.CreateDecryptor(), CryptoStreamMode.Read))
using (var sr = new StreamReader(cs, Encoding.UTF8))
{
return sr.ReadToEnd();
}
}
}
This cryptography works but I would like to know whether I wrote it good and if there are any suggestion to improve. In addition, I would also like to ask if I can make a function in Python that can decrypt what these C# function encrypted (and in the opposite direction too) and how can I do that?
EDIT: These two functions are meant to encrypt or decrypt the messages to/from a socket that is connected to a server. That's why I get this as byte array in the decrypt function and return it as one in the encrypt function. The server is written in Python (I was told the python can be easy to write servers with, especially because the server is a simple one). And, I want to decrypt the messages sent from the client in the server too.
CreateEncryptor()
andCreateDecryptor()
both return objects which implementIDisposable
, so they should be in their ownusing
block before passed to theCryptoStream
constructor. \$\endgroup\$