Skip to content

Instantly share code, notes, and snippets.

@adrianstevens
Created October 28, 2015 17:42
Show Gist options
  • Save adrianstevens/2fe4aa940204c2b4e0ab to your computer and use it in GitHub Desktop.
Save adrianstevens/2fe4aa940204c2b4e0ab to your computer and use it in GitHub Desktop.
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
// Perform any additional setup after loading the view, typically from a nib.
var password = "9a55W0rd!!";
var salt = Encoding.Unicode.GetBytes("5a1t");
byte[] keymaterial = NetFxCrypto.DeriveBytes.GetBytes(password, salt, 1000, 32);
byte[] data = Encoding.UTF8.GetBytes ("Some very important text");//because you should
var provider = WinRTCrypto.SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithm.AesCbcPkcs7);
var key = provider.CreateSymmetricKey(keymaterial);
byte[] iv = null; // this is optional, but must be the same for both encrypting and decrypting
byte[] cipherText = WinRTCrypto.CryptographicEngine.Encrypt(key, data, iv);
byte[] plainText = WinRTCrypto.CryptographicEngine.Decrypt(key, cipherText, iv);
var decoded = Encoding.UTF8.GetString(plainText, 0, plainText.Length);
Debug.WriteLine (String.Format("decoded: {0}", decoded));
}
@adrianstevens
Copy link
Author

Quick encryption/decryption example using PCLCrypto (tested on Xamarin.iOS)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment