|
In order to combine the use of SymmetricAlgorithm and AsymmetricAlgorithm for key exchange purposes, it seems that the key and block sizes must each be 256.
Otherwise, we get a "System.Security.Cryptography.CryptographicException" with additional info : "PKCS7 padding is invalid and cannot be removed."
|
|
|
|
|
In addition, if you were to use the CryptoStream object to perform Symmetric Encryption, e.g. :
public static void Encrypt_SymmetricAlgorithm_File
(
ref Stream stream_in,
ref Stream stream_out,
ref SymmetricAlgorithm sa
)
{
CryptoStream c = new CryptoStream
(
stream_out,
sa.CreateEncryptor(),
CryptoStreamMode.Write
);
byte [] buf = new byte[BUFSIZE];
int siz;
while ((siz = stream_in.Read(buf,0, BUFSIZE)) > 0)
c.Write(buf, 0, siz);
c.Close(); // This better be called.
return;
}
You better call the CryptoStream.Close() method. Otherwise, the cryptographic output to the output stream ("stream_out" in the above example), may become corrupted.
|
|
|
|
|
Also, it would be good programming practice to call CryptoStream.Close() when you use the CryptoStream for dercyption, e.g. :
public static void Decrypt_SymmetricAlgorithm_File
(
ref Stream stream_in,
ref Stream stream_out,
ref SymmetricAlgorithm sa
)
{
CryptoStream c = new CryptoStream
(
stream_in,
sa.CreateDecryptor(),
CryptoStreamMode.Read
);
byte [] buf = new byte[BUFSIZE];
int siz;
while ((siz = c.Read(buf,0, BUFSIZE)) > 0)
stream_out.Write(buf, 0, siz);
c.Close(); // This better be called.
}
|
|
|
|
|
The Close() ensures that padding is correctly performed. Without it, the stream is likely to be corrupted.
|
|
|
|
|
1. Suppose that you have created a string out of a DateTime object e.g. :
string strDateTime = DateTime.Now.ToShortDateString();
2. To later convert this DateTime string back to its original DateTime object, use the Parse() method together with DateTimeFormatInfo.CurrentInfo :
DateTime dt = DateTime.Parse(strDateTime, DateTimeFormatInfo.CurrentInfo);
3. CurrentInfo is a property of DateTimeFormatInfo which returns a read-only DateTimeFormatInfo object that formats values based on the current culture.
4. Note that because "strDateTime" was formatted according to the current culture, we can use DateTimeFormatInfo.CurrentInfo to retrieve -that- culture.
|
|
|
|