843 messaggi dal 08 aprile 2009
Dovrei utilizzare la criptazione con AES in ambiente UWP.
Ho utilizzato il seguente codice:
    class CriptoSystem
    {
        private static string AES_Key = "xxxxxxxxxxxxxxxx";
        private static string AES_IV = "yyyyyyyy";


        public static void AesEnDecryption(out IBuffer m_iv,out CryptographicKey m_key)
        {

            IBuffer key = Convert.FromBase64String(AES_Key).AsBuffer();
            m_iv = Convert.FromBase64String(AES_IV).AsBuffer();
            SymmetricKeyAlgorithmProvider provider = SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.AesCbcPkcs7);
            m_key = provider.CreateSymmetricKey(key);
        }

        public static string AES_encrypt(string Input)
        {

            IBuffer m_iv;
            CryptographicKey m_key;
            AesEnDecryption(out m_iv, out m_key);

            byte[] bytearray = Encoding.UTF8.GetBytes(Input);


            IBuffer bufferMsg = CryptographicBuffer.CreateFromByteArray(bytearray);
            IBuffer bufferEncrypt = CryptographicEngine.Encrypt(m_key, bufferMsg, m_iv);
            byte[] xBuff = bufferEncrypt.ToArray();

            string Output = Convert.ToBase64String(xBuff);
            return Output;

        }

       
        public static string AES_decrypt(string Input)
        {
            
            IBuffer m_iv;
            CryptographicKey m_key;
            AesEnDecryption(out m_iv, out m_key);
            byte[] xXml = Encoding.UTF8.GetBytes(Input);

            IBuffer bufferDecrypt = CryptographicEngine.Decrypt(m_key, xXml.AsBuffer(), m_iv);
            byte[] xBuff = bufferDecrypt.ToArray();

            string Output = Encoding.UTF8.GetString(xBuff, 0, xBuff.Length);

            return Output;
        }
    }



ma in fase di
CryptographicEngine.Encrypt(m_key, bufferMsg, m_iv)
mi da il seguente errore:
Il buffer utente fornito non è valido per l'operazione richiesta. (Exception from HRESULT: 0x800706F8)

Dove sbaglio?
11.886 messaggi dal 09 febbraio 2002
Contributi
Ciao Laura,
prova a vedere se funziona questo:
https://github.com/lukevenediger/statsd.net/blob/master/statsd.net.shared/Encryption/SimplerAES.cs

Oppure confrontalo con la tua implementazione. Magari stai fornendo una chiave o un vettore che non sono della lunghezza necessaria.

ciao,
Moreno

Enjoy learning and just keep making
843 messaggi dal 08 aprile 2009
Si infatti erano sbagliate le lunghezze.
Cercavo un metodo con chiave che andasse bene sia per UWP che per altri sistemi in modo tale che la criptazione risultasse la stessa.
E quindi ho optato per questa soluzione che mi permette di impostare una chiave con la stessa lunghezza:
UWP
    class CriptoSystem
    {
        private static string AES_Key = "xxxxxxxxxxxxxxxxxxxxxxxx";
        private static string AES_IV = "yyyyyyyy";

        public static string TDES_encrypt(string Input)
        {
            try
            {
                using (var tdes = System.Security.Cryptography.TripleDES.Create())
                {
                    tdes.Key = UTF8Encoding.UTF8.GetBytes(AES_Key);
                    tdes.IV = UTF8Encoding.UTF8.GetBytes(AES_IV);
                    tdes.Mode = System.Security.Cryptography.CipherMode.ECB;
                    tdes.Padding = PaddingMode.PKCS7;



                    ICryptoTransform cTransform = tdes.CreateEncryptor();
                    byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(Input);
                    byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

                    return Convert.ToBase64String(resultArray, 0, resultArray.Length);

                }

            }
            catch (Exception ex)
            {
                throw new Exception("Errore durante la criptazione della chiave (TripleDES)");
            }




        }

        public static string TDES_decrypt(string Input)
        {

            try
            {
                using (var tdes = System.Security.Cryptography.TripleDES.Create())
                {
                    tdes.Key = UTF8Encoding.UTF8.GetBytes(AES_Key);
                    tdes.IV = UTF8Encoding.UTF8.GetBytes(AES_IV);
                    tdes.Mode = CipherMode.ECB;
                    tdes.Padding = PaddingMode.PKCS7;

                    ICryptoTransform cTransform = tdes.CreateDecryptor();
                    byte[] toDencryptArray = Convert.FromBase64String(Input);
                    byte[] resultArray = cTransform.TransformFinalBlock(toDencryptArray, 0, toDencryptArray.Length);
                    return UTF8Encoding.UTF8.GetString(resultArray, 0, resultArray.Length);

                }

            }
            catch (Exception ex)
            {
                throw new Exception("Errore durante la decriptazione della chiave (TripleDES)");
            }


        }

    }



ALTRO
    class CriptoSystem
    {


        private static string AES_Key = "xxxxxxxxxxxxxxxxxxxxxxxx";
        private static string AES_IV = "yyyyyyyy";




        public static string TDES_encrypt(string Input)
        {


            try
            {
                using (TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider())
                {
                    tdes.Key = UTF8Encoding.UTF8.GetBytes(AES_Key);
                    tdes.IV = UTF8Encoding.UTF8.GetBytes(AES_IV);
                    tdes.Mode = CipherMode.ECB;
                    tdes.Padding = PaddingMode.PKCS7;

                    ICryptoTransform cTransform = tdes.CreateEncryptor();
                    byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(Input);
                    byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
                    tdes.Clear();
                    return Convert.ToBase64String(resultArray, 0, resultArray.Length);

                }

            }
            catch (Exception ex)
            {
                throw new Exception("Errore durante la criptazione della chiave (TripleDES)");
            }


        }

        public static string TDES_decrypt(string Input)
        {


            try
            {
                using (TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider())
                {
                    tdes.Key = UTF8Encoding.UTF8.GetBytes(AES_Key);
                    tdes.IV = UTF8Encoding.UTF8.GetBytes(AES_IV);
                    tdes.Mode = CipherMode.ECB;
                    tdes.Padding = PaddingMode.PKCS7;

                    ICryptoTransform cTransform = tdes.CreateDecryptor();
                    byte[] toDencryptArray = Convert.FromBase64String(Input);
                    byte[] resultArray = cTransform.TransformFinalBlock(toDencryptArray, 0, toDencryptArray.Length);
                    tdes.Clear();
                    return UTF8Encoding.UTF8.GetString(resultArray, 0, resultArray.Length);

                }

            }
            catch (Exception ex)
            {
                throw new Exception("Errore durante la decriptazione della chiave (TripleDES)");
            }

          
        }
    }

11.886 messaggi dal 09 febbraio 2002
Contributi
Grazie per aver condiviso il codice

Enjoy learning and just keep making

Torna al forum | Feed RSS

ASPItalia.com non è responsabile per il contenuto dei messaggi presenti su questo servizio, non avendo nessun controllo sui messaggi postati nei propri forum, che rappresentano l'espressione del pensiero degli autori.