Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
I think I found an issue between Monotouch (iOS) and .NET 4. Using an encryption algorithm I have differents outputs due to a default value set in .NET and not in MonoTouch.
// This file is shared both with a MonoTouch iOS and ASP.NET MVC 3 + .NET 4 project.
// The result of Encrypt differs because of the default selected mode.
// See line 94 for information.
public static class EncryptionHelper
{
/// <summary>
/// Default key
/// </summary>
private static string cryptoKey = "j7gdft5'(eqA84Mo";
private const char FillCharacter = '_';
private const int KeyLength = 16;
public static void Initialize(string encryptionKey)
{
if (encryptionKey.Length > KeyLength)
{
throw new ArgumentException("Encryption key must be 16 chars max!");
}
cryptoKey = encryptionKey;
}
/// <summary>
///
/// </summary>
/// <param name="cipheredtext">Texte en base 64</param>
/// <returns></returns>
public static String Decrypt(String cipheredText)
{
string finalKey = cryptoKey.PadRight(KeyLength, FillCharacter);
RijndaelManaged crypto = null;
MemoryStream mStream = null;
ICryptoTransform decryptor = null;
CryptoStream cryptoStream = null;
try
{
byte[] cipheredData = Convert.FromBase64String(cipheredText);
crypto = new RijndaelManaged();
crypto.KeySize = 128;
crypto.Padding = PaddingMode.PKCS7;
decryptor = crypto.CreateDecryptor(Encoding.UTF8.GetBytes(finalKey), Encoding.UTF8.GetBytes(finalKey));
mStream = new System.IO.MemoryStream(cipheredData);
cryptoStream = new CryptoStream(mStream, decryptor, CryptoStreamMode.Read);
StreamReader creader = new StreamReader(cryptoStream, Encoding.UTF8);
String data = creader.ReadToEnd();
return data;
//return Encoding.UTF8.GetString(Encoding.Default.GetBytes(data));
}
finally
{
if (crypto != null)
{
crypto.Clear();
}
if (cryptoStream != null)
{
cryptoStream.Close();
}
}
}
/// <summary>
///
/// </summary>
/// <param name="plainText"></param>
/// <param name="key"></param>
/// <returns></returns>
public static String Encrypt(String plainText)
{
string finalKey = cryptoKey.PadRight(KeyLength, FillCharacter);
RijndaelManaged crypto = null;
MemoryStream mStream = null;
ICryptoTransform encryptor = null;
CryptoStream cryptoStream = null;
byte[] plainBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
try
{
crypto = new RijndaelManaged();
crypto.KeySize = 128;
crypto.Padding = PaddingMode.PKCS7;
#if IOS
// HERE IS THE "ISSUE"
// The default mode on .NET 4 is CBC, wich seems not to be the case on MonoTouch.
crypto.Mode = CipherMode.CBC;
#endif
encryptor = crypto.CreateEncryptor(Encoding.UTF8.GetBytes(finalKey), Encoding.UTF8.GetBytes(finalKey));
mStream = new MemoryStream();
cryptoStream = new CryptoStream(mStream, encryptor, CryptoStreamMode.Write);
cryptoStream.Write(plainBytes, 0, plainBytes.Length);
}
finally
{
if (crypto != null)
crypto.Clear();
cryptoStream.Close();
}
return Convert.ToBase64String(mStream.ToArray());
}
}
# Windows
- .NET 4
- Visual Studio 2012 Express for the Web
# MonoTouch iOS
- "MonoTouch is up to date" -> Last Xamarin Studio update (03/14/2013)
- Xamarin.iOS (Version: 6.2.0.65 (Trial Edition))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment