public static string AESEncrypt(string plainData, EncOption option) { AesCryptoServiceProvider dataencrypt = new AesCryptoServiceProvider(); byte[] bytearraytoencrypt = Encoding.UTF8.GetBytes(plainData); //Block size : Gets or sets the block size, in bits, of the cryptographic operation. dataencrypt.BlockSize = option.BLOCK_SIZE; //KeySize: Gets or sets the size, in bits, of the secret key dataencrypt.KeySize = option.KEY_SIZE; //Key: Gets or sets the symmetric key that is used for encryption and decryption. dataencrypt.Key = System.Text.Encoding.UTF8.GetBytes(option.secureKey); //IV : Gets or sets the initialization vector (IV) for the symmetric algorithm dataencrypt.IV = System.Text.Encoding.UTF8.GetBytes(option.viKey); //Padding: Gets or sets the padding mode used in the symmetric algorithm dataencrypt.Padding = PaddingMode.PKCS7; //Mode: Gets or sets the mode for operation of the symmetric algorithm dataencrypt.Mode = CipherMode.CBC; //Creates a symmetric AES encryptor object using the current key and initialization vector (IV). ICryptoTransform crypto1 = dataencrypt.CreateEncryptor(dataencrypt.Key, dataencrypt.IV); //TransformFinalBlock is a special function for transforming the last block or a partial block in the stream. //It returns a new array that contains the remaining transformed bytes. A new array is returned, because the amount of //information returned at the end might be larger than a single block when padding is added. byte[] encrypteddata = crypto1.TransformFinalBlock(bytearraytoencrypt, 0, bytearraytoencrypt.Length); crypto1.Dispose(); //return the encrypted data string encryptedString = Encoding.UTF8.GetString(encrypteddata); return(encryptedString); }
public static string AESDecrypt(string encryptedData, EncOption option) { AesCryptoServiceProvider keydecrypt = new AesCryptoServiceProvider(); byte[] bytearraytodecrypt = Encoding.UTF8.GetBytes(encryptedData); keydecrypt.BlockSize = option.BLOCK_SIZE; keydecrypt.KeySize = option.KEY_SIZE; keydecrypt.Key = Encoding.UTF8.GetBytes(option.secureKey); keydecrypt.IV = Encoding.UTF8.GetBytes(option.viKey); keydecrypt.Padding = option.pMode; keydecrypt.Mode = option.cMode; ICryptoTransform crypto1 = keydecrypt.CreateDecryptor(keydecrypt.Key, keydecrypt.IV); byte[] returnbytearray = crypto1.TransformFinalBlock(bytearraytodecrypt, 0, bytearraytodecrypt.Length); crypto1.Dispose(); string plainData = Encoding.UTF8.GetString(returnbytearray); return(plainData); }
public static string RFC2898Decrypt(string encryptedText, EncOption option) { byte[] cipherTextBytes = Convert.FromBase64String(encryptedText); byte[] keyBytes = new Rfc2898DeriveBytes(option.secureKey, Encoding.ASCII.GetBytes(option.saltKey)).GetBytes(256 / 8); var symmetricKey = new RijndaelManaged() { Mode = option.cMode, Padding = option.pMode }; var decryptor = symmetricKey.CreateDecryptor(keyBytes, Encoding.ASCII.GetBytes(option.viKey)); var memoryStream = new MemoryStream(cipherTextBytes); var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read); byte[] plainTextBytes = new byte[cipherTextBytes.Length]; int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length); memoryStream.Close(); cryptoStream.Close(); return(Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount).TrimEnd("\0".ToCharArray())); }
public static string RFC2898Encrypt(string plainText, EncOption option) { byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText); byte[] keyBytes = new Rfc2898DeriveBytes(option.secureKey, Encoding.ASCII.GetBytes(option.saltKey)).GetBytes(256 / 8); RijndaelManaged symmetricKey = new RijndaelManaged() { Mode = option.cMode, Padding = option.pMode }; var encryptor = symmetricKey.CreateEncryptor(keyBytes, Encoding.ASCII.GetBytes(option.viKey)); byte[] cipherTextBytes; using (var memoryStream = new MemoryStream()) { using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write)) { cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length); cryptoStream.FlushFinalBlock(); cipherTextBytes = memoryStream.ToArray(); cryptoStream.Close(); } memoryStream.Close(); } return(Convert.ToBase64String(cipherTextBytes)); }