locked
Implement 3DES algorithm cryptography for xamarin forms RRS feed

  • Question

  • User166393 posted

    My PCL project does not support System.Security.Cryptography , I am trying to use 3DES algorithm for encryption and decryption. But PCL Crypto does not support 3DES. what is the work around in this scenario.

    Friday, February 26, 2016 5:35 AM

All replies

  • User2012 posted

    If your Platform specific project's version of .NET supports 3DES then you can write an abstraction that you expose to your PCL.

    See https://developer.xamarin.com/guides/xamarin-forms/dependency-service/ for examples

    Friday, February 26, 2016 5:45 AM
  • User76916 posted

    @Maharshi.5212 - Here is an example of someone self implementing on Windows Phone 7 but should be easily enough implemented in PCL or at least natively per platform. https://gist.github.com/amrishodiq/1622737

    However if 3DES is just because of a personal preference or outdated security recommendations, AES is generally recommended over 3DES.

    Friday, February 26, 2016 5:53 AM
  • User166393 posted

    Can i use the PCl crypto library in the normal asp.net app . The encryption is getting done in the server. My app needs to decrypt it.

    There is a code recipe i found in the PCl crypto library.

    byte[] keyMaterial; byte[] data; 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);

    Can i do it in a normal C# project?

    Friday, February 26, 2016 7:37 AM
  • User166393 posted

    Can i use PCLcrypto in a normal c# application?

    Monday, February 29, 2016 12:30 PM
  • User166393 posted

    Hi, I have written this code for pcl encryption .

    static class Program { static void Main(string[] args) {

            string data = "Examples";
    
            string key = "AAECAwQFBgcICQoLDA0ODw==";
            string cipherText = SymmetricEncrypt(data, key);
    
    
            string plainText= SymmetricDecrypt(cipherText, key);
        }
    
    
        private static string SymmetricDecrypt(this string cipherText, string key)
        {
            byte[] keyBuffer = Convert.FromBase64String(key);
            byte[] cipherTextBuffer = Convert.FromBase64String(cipherText);
    
            ISymmetricKeyAlgorithmProvider symmetricAlgorithm = WinRTCrypto.SymmetricKeyAlgorithmProvider.OpenAlgorithm(PCLCrypto.SymmetricAlgorithm.AesEcbPkcs7);
    
            var symmetricKey = symmetricAlgorithm.CreateSymmetricKey(keyBuffer);
            var decryptor = WinRTCrypto.CryptographicEngine.CreateDecryptor(symmetricKey);
            byte[] plainTextBuffer = decryptor.TransformFinalBlock(cipherTextBuffer, 0, cipherTextBuffer.Length);
            return Convert.ToBase64String(plainTextBuffer, 0, plainTextBuffer.Length);
        }
    
        public static string SymmetricEncrypt(this string plaintext, string key)
        {
            byte[] keyBuffer = Convert.FromBase64String(key);
            byte[] plainTextBuffer = Convert.FromBase64String(plaintext);
    
    
            ISymmetricKeyAlgorithmProvider symmetricAlgorithm = WinRTCrypto.SymmetricKeyAlgorithmProvider.OpenAlgorithm(PCLCrypto.SymmetricAlgorithm.AesEcbPkcs7);
    
            var symmetricKey = symmetricAlgorithm.CreateSymmetricKey(keyBuffer);
            var encryptor = WinRTCrypto.CryptographicEngine.CreateEncryptor(symmetricKey);
            byte[] cipherBuffer = encryptor.TransformFinalBlock(plainTextBuffer, 0, plainTextBuffer.Length);
            return Convert.ToBase64String(cipherBuffer);
        }
    
    }
    

    But I cannot change the length of the sample string. What shall i do in this scenario?

    Friday, March 4, 2016 10:09 AM
  • User370196 posted

    This work for me ...

    public static string SymmetricEncrypt(this string plaintext) { string data = Convert.ToBase64String(Encoding.UTF8.GetBytes(plaintext)); string key = "AAECAwQFBgcICQoLDA0ODw=="; byte[] keyBuffer = Convert.FromBase64String(key); byte[] plainTextBuffer = Convert.FromBase64String(data);

            ISymmetricKeyAlgorithmProvider symmetricAlgorithm = WinRTCrypto.SymmetricKeyAlgorithmProvider.OpenAlgorithm(PCLCrypto.SymmetricAlgorithm.AesEcbPkcs7);
    
            var symmetricKey = symmetricAlgorithm.CreateSymmetricKey(keyBuffer);
            var encryptor = WinRTCrypto.CryptographicEngine.CreateEncryptor(symmetricKey);
            byte[] cipherBuffer = encryptor.TransformFinalBlock(plainTextBuffer, 0, plainTextBuffer.Length);
            return Convert.ToBase64String(cipherBuffer);
        }
    
    public static string SymmetricDecrypt(this string cipherText)
        {
            string key = "AAECAwQFBgcICQoLDA0ODw==";
            byte[] keyBuffer = Convert.FromBase64String(key);
            byte[] cipherTextBuffer = Convert.FromBase64String(cipherText);
    
            ISymmetricKeyAlgorithmProvider symmetricAlgorithm = WinRTCrypto.SymmetricKeyAlgorithmProvider.OpenAlgorithm(PCLCrypto.SymmetricAlgorithm.AesEcbPkcs7);
    
            var symmetricKey = symmetricAlgorithm.CreateSymmetricKey(keyBuffer);
            var decryptor = WinRTCrypto.CryptographicEngine.CreateDecryptor(symmetricKey);
            byte[] plainTextBuffer = decryptor.TransformFinalBlock(cipherTextBuffer, 0, cipherTextBuffer.Length);
            var test = Convert.FromBase64String(Convert.ToBase64String(plainTextBuffer, 0, plainTextBuffer.Length));
            string jsonBack = System.Text.Encoding.UTF8.GetString(test, 0, test.Length);
            return jsonBack;
        }
    
    Tuesday, June 26, 2018 1:08 PM