Un utility in C# per codificare (encrypt) e decodificare (decrypt) una stringa:

C#: Classe Helper

using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace Sgart.Base
{
    public static class EncryptHelper
    {
        // per sicurezza cambiate la COMMON_KEY con un valore diverso
        // deve essere di 16 caratteri
        private const string COMMON_KEY = "DA82179464E44E67B8006317BB5C3838";

        /// <summary>
        /// prende una stringa e la cripta restituendo un cipherText
        /// </summary>
        /// <param name="text">testo in chiaro da cifrare</param>
        /// <param name="keyString">se null usa quella comune definita nella classe</param>
        /// <returns></returns>
        public static string EncryptString(string text, string keyString = null)
        {
            if (keyString == null) keyString = COMMON_KEY;

            var key = Encoding.UTF8.GetBytes(keyString);


            using var aesAlg = Aes.Create();
            var iv = aesAlg.IV;
            using var encryptor = aesAlg.CreateEncryptor(key, iv);
            using var msEncrypt = new MemoryStream();
            using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
            using (var swEncrypt = new StreamWriter(csEncrypt))
            {
                swEncrypt.Write(text);
            }

            var decryptedContent = msEncrypt.ToArray();

            var result = new byte[iv.Length + decryptedContent.Length];

            Buffer.BlockCopy(iv, 0, result, 0, iv.Length);
            Buffer.BlockCopy(decryptedContent, 0, result, iv.Length, decryptedContent.Length);

            return Convert.ToBase64String(result);
        }

        /// <summary>
        /// passando il cipherText restituisce la stringa in chiaro
        /// </summary>
        /// <param name="cipherText">testo cifrato da decifrare</param>
        /// <param name="keyString">se null usa quella comune definita nella classe</param>
        /// <returns></returns>
        public static string DecryptString(string cipherText, string keyString = null)
        {
            if (keyString == null) keyString = COMMON_KEY;

            var fullCipher = Convert.FromBase64String(cipherText);

            var iv = new byte[16];
            var cipher = new byte[fullCipher.Length - iv.Length];

            Buffer.BlockCopy(fullCipher, 0, iv, 0, iv.Length);
            Buffer.BlockCopy(fullCipher, iv.Length, cipher, 0, cipher.Length);
            var key = Encoding.UTF8.GetBytes(keyString);

            using var aesAlg = Aes.Create();
            using var decryptor = aesAlg.CreateDecryptor(key, iv);
            string result;
            using (var msDecrypt = new MemoryStream(cipher))
            {
                using var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read);
                using var srDecrypt = new StreamReader(csDecrypt);
                result = srDecrypt.ReadToEnd();
            }

            return result;
        }

    }
}
da usare come

C#: Esempio d'uso

var data = "stringa da secretare";

// encryption
var enc = EncryptHelper.EncryptString(data);
Console.WriteLine(enc);

// decryption
var dec = EncryptHelper.DecriptString(enc);
Console.WriteLine(dec);
Tags:
C#236 Esempi225
Potrebbe interessarti anche: