locked
Cryptography Shared Code RRS feed

  • Question

  • User214189 posted

    Hi Everyone.

    I'm trying to develop a portable library to center the cryptography code. I want to use a cryptography based in a key, but i don't know which library i should use.

    Any idea?

    Thanks.

    Peterson Seridonio


    Details. It's a full portable library. - Android - IOS - Windows Universal Apps - Asp.net

    Monday, April 11, 2016 1:14 AM

Answers

  • User2148 posted

    I have not understand. Do you need a PCL library for cryptography? Should exists pclcrypto

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Monday, April 11, 2016 5:14 AM

All replies

  • User2148 posted

    I have not understand. Do you need a PCL library for cryptography? Should exists pclcrypto

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Monday, April 11, 2016 5:14 AM
  • User214189 posted

    Let-me explain better.

    I have the following structure

    • MySolution -- MyProject.Server (Asp.Net MVC Web API) -- MyProject.Android (Android Project) -- MyProject.IOS (iOS Project) -- MyProject.Windows (Windows Universal App) -- MyProject.Cryptography (portable class library between all plataforms above)

    The project MyProject.Cryptography should have the functions to encrypt and decrypt and store a key to the funcions.

    When I try to use the assembly System.Security.Cryptography I can't find it in available namespaces and it's imposible to add a reference to this name space.

    I would like to know which namespace is available to use the PBKDF2 cryptography in this situation.

    Tuesday, April 12, 2016 3:59 AM
  • User214189 posted

    I researched about the PCLCrypto and it worked well to me.

    Now I have one project to encrypt and decrypt to all plataforms.

    Thanks to Alessandro Caliaro.

    Tuesday, April 12, 2016 6:01 AM
  • User197000 posted

    @PetersonSeridonio said: I researched about the PCLCrypto and it worked well to me.

    Now I have one project to encrypt and decrypt to all plataforms.

    Thanks to Alessandro Caliaro.

    How did u ? I have the same as you !! Did your code worked ? I have to encripty on the PCL and decripty on WebApi... If possible, post your code...

    Thanks!

    Tuesday, June 28, 2016 6:56 PM
  • User214189 posted

    Hi there.

    Here is my solution:

    ` internal static class InternalCrypto { private static readonly string passwordEncryption = "12345"; private static readonly int _iterations = 1000; private static readonly int _keyLenght = 32; private static readonly byte[] _salt = Encoding.UTF8.GetBytes("123456789"); private static readonly byte[] _derivedKey = NetFxCrypto.DeriveBytes.GetBytes(passwordEncryption, _salt, _iterations, _keyLenght);

        internal static byte[] Encrypt(string text)
        {
            ISymmetricKeyAlgorithmProvider aesCrypto = WinRTCrypto.SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithm.AesCbcPkcs7);
            ICryptographicKey symmtricKey = aesCrypto.CreateSymmetricKey(_derivedKey);
            ICryptoTransform encryptor = WinRTCrypto.CryptographicEngine.CreateEncryptor(symmtricKey);
            byte[] byteText = Encoding.UTF8.GetBytes(text);
            byte[] encryptedText = encryptor.TransformFinalBlock(byteText, 0, byteText.Length);
            return encryptedText;
        }
    
    
        internal static string Decrypt(byte[] encryptedText)
        {
            ISymmetricKeyAlgorithmProvider aesCrypto = WinRTCrypto.SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithm.AesCbcPkcs7);
            ICryptographicKey symmtricKey = aesCrypto.CreateSymmetricKey(_derivedKey);
            ICryptoTransform decryptor = WinRTCrypto.CryptographicEngine.CreateDecryptor(symmtricKey);
            byte[] decryptedText = decryptor.TransformFinalBlock(encryptedText, 0, encryptedText.Length);
            return Encoding.UTF8.GetString(decryptedText, 0, decryptedText.Length);
        }
    }
    

    `

    ` public static class Crypto { public static string EncryptToText(string text) { byte[] encryptedText = InternalCrypto.Encrypt(text); return Convert.ToBase64String(encryptedText); }

        public static byte[] EncryptToBytes(string text)
        {
            return InternalCrypto.Encrypt(text);
        }
    
        public static string DecryptFromText(string encryptedText)
        {
            return InternalCrypto.Decrypt(Encoding.UTF8.GetBytes(encryptedText));
        }
    
        public static string DecryptFromBytes(byte[] encryptedText)
        {
            return InternalCrypto.Decrypt(encryptedText);
        }
    
        public static void Teste()
        {
            byte[] bytes = EncryptToBytes("Teste");
            string text = DecryptFromBytes(bytes);
        }
    }
    

    `

    I executed the method "Teste", and it worked.

    Friday, July 1, 2016 1:12 AM