The following code will crash the G120 and starts to output low level MCU exceptions to COM1… Remark that the encryption code is not called in this demo. So it has to do with low level init code…
using System;
using System.Security.Cryptography;
using System.Text;
using Microsoft.SPOT;
namespace G120Encryption
{
public class Program
{
public static void Main()
{
Debug.Print("CryptoTest... actually doing nothing");
}
}
public class BlockEncryption
{
public byte[] Encrypt(string stringToEncrypt)
{
return Encrypt(Encoding.UTF8.GetBytes(stringToEncrypt));
}
public byte[] Encrypt(byte[] dataToEncrypt)
{
try
{
using (var rsa = new RSACryptoServiceProvider())
{
return RsaEncrypt(dataToEncrypt, rsa.ExportParameters(false));
}
}
catch (ArgumentNullException)
{
Debug.Print("Encryption failed");
return null;
}
}
public byte[] DecryptByteArray(byte[] dataToDecrypt)
{
using (var rsa = new RSACryptoServiceProvider())
{
return RsaDecrypt(dataToDecrypt, rsa.ExportParameters(true));
}
}
private static byte[] RsaEncrypt(byte[] dataToEncrypt, RSAParameters rsaKeyInfo)
{
try
{
byte[] encryptedData;
using (var rsa = new RSACryptoServiceProvider())
{
rsa.ImportParameters(rsaKeyInfo);
encryptedData = rsa.Encrypt(dataToEncrypt);
}
return encryptedData;
}
catch (CryptographicException e)
{
Debug.Print(e.Message);
return null;
}
}
private static byte[] RsaDecrypt(byte[] dataToDecrypt, RSAParameters rsaKeyInfo)
{
byte[] decryptedData = null;
try
{
using (var rsa = new RSACryptoServiceProvider())
{
rsa.ImportParameters(rsaKeyInfo);
decryptedData = rsa.Decrypt(dataToDecrypt);
}
}
catch (CryptographicException e)
{
Debug.Print(e.ToString());
}
return decryptedData;
}
}
}