Implement 3DES algorithm cryptography for xamarin forms

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.

Posts

  • PhilipOchuPhilipOchu USBeta, University ✭✭✭

    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

  • AdamPAdamP AUUniversity ✭✭✭✭✭

    @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.

  • Maharshi.5212Maharshi.5212 USMember ✭✭

    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?

  • Maharshi.5212Maharshi.5212 USMember ✭✭

    Can i use PCLcrypto in a normal c# application?

  • Maharshi.5212Maharshi.5212 USMember ✭✭

    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?

  • snaatansnaatan Member

    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;
        }
    
Sign In or Register to comment.