I want to use the cryptography in the Portable Class Library Contrib project on codeplex but have not found any documentation on how I can use it.
I want to create a wra
It turns out that my generic wrapper for the cryptography algorithms was causing a problem. PCL Contrib contains a class called SymmetricAlgorithm which is itself a wrapper for the real SymmetricAlgorithm. If I make my wrapper class non-generic it works like this:
public sealed class AesManagedSymmetricCryptography : SymmetricCryptography<AesManaged>
{
#region Constructors
public AesManagedSymmetricCryptography()
{
}
public AesManagedSymmetricCryptography(byte[] key, byte[] iv)
: base(key, iv)
{
}
public AesManagedSymmetricCryptography(string password, string salt)
: base(password, salt)
{
}
public AesManagedSymmetricCryptography(string password, string salt, int iterations)
: base(password, salt, iterations)
{
}
#endregion
}
The docs are a little lacking, but I call this out in the FAQ:
Can I share the types from PclContrib with my platform-specific projects? No, not currently. While the types in PclContrib look and feel like their platform-specific counterparts, the runtime and compiler will see them as completely different types. While we have some ideas on how to make this work, this is a feature that we won't be looking at for the short term.
The following .net code works on Desktop implementation. First add references to Portable.Desktop and Portable.Security.Cryptography.ProtectedData
private void button2_Click(object sender, EventArgs e)
{
String encrypted = PCL.CentralClass.Encrypt("yo");
String decreypted = PCL.CentralClass.Decrypt(encrypted);
//PCL.CentralClass.
}
//https://pclcontrib.codeplex.com/documentation?FocusElement=Comment
//\Source\Portable.Security.Cryptography.ProtectedData\Security\Cryptography\ProtectedData.cs
static byte[] GetBytes(string str)
{
byte[] bytes = new byte[str.Length * sizeof(char)];
System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
return bytes;
}
static string GetString(byte[] bytes)
{
char[] chars = new char[bytes.Length / sizeof(char)];
System.Buffer.BlockCopy(bytes, 0, chars, 0, bytes.Length);
return new string(chars);
}
public static String Encrypt(String strEncrypt)
{
byte[] userData = GetBytes(strEncrypt);
byte[] optionalEntropy = null;
byte[] x = System.Security.Cryptography.ProtectedData.Protect(userData, optionalEntropy);
return GetString(x);
}
public static String Decrypt(String strDecrypt)
{
byte[] encryptedData = GetBytes(strDecrypt);
byte[] optionalEntropy = null;
byte[] x = System.Security.Cryptography.ProtectedData.Unprotect(encryptedData, optionalEntropy);
return GetString(x); ;
}