zoukankan      html  css  js  c++  java
  • 步步为营-30-AES加密与解密

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Security.Cryptography;
    
    namespace AES
    {
        public partial class Form1 : Form
        {
            
            public Form1()
            {
                InitializeComponent();            
            }
          
            private void btnEncrypt_Click(object sender, EventArgs e)
            {
               string toEncrypt = txtString.Text.Trim().ToString();
                string key = txtKey.Text.Trim().ToString();
                string iv = txtIv.Text.Trim().ToString();
               lblResult.Text = Encrypt(toEncrypt, key, iv);
            }
    
            private void btnDecrypt_Click(object sender, EventArgs e)
            {
                string toEncrypt = txtString.Text.Trim().ToString();
                string key = txtKey.Text.Trim().ToString();
                string iv = txtIv.Text.Trim().ToString();
                lblResult.Text = Decrypt(toEncrypt, key, iv);
            }
    
            public static string Encrypt(string toEncrypt, string key, string iv)
            {
    
    
                byte[] keyArray = UTF8Encoding.UTF8.GetBytes(key);
                byte[] ivArray = UTF8Encoding.UTF8.GetBytes(iv);
                byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);
    
                RijndaelManaged rDel = new RijndaelManaged();
                rDel.Key = keyArray;
                rDel.IV = ivArray;
                rDel.Mode = CipherMode.CBC;
                rDel.Padding = PaddingMode.Zeros;
    
                ICryptoTransform cTransform = rDel.CreateEncryptor();
                byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
    
                return Convert.ToBase64String(resultArray, 0, resultArray.Length);
            }
    
            public static string Decrypt(string toDecrypt, string key, string iv)
            {
                byte[] keyArray = UTF8Encoding.UTF8.GetBytes(key);
                byte[] ivArray = UTF8Encoding.UTF8.GetBytes(iv);
                byte[] toEncryptArray = Convert.FromBase64String(toDecrypt);
    
                RijndaelManaged rDel = new RijndaelManaged();
                rDel.Key = keyArray;
                rDel.IV = ivArray;
                rDel.Mode = CipherMode.CBC;
                rDel.Padding = PaddingMode.Zeros;
    
                ICryptoTransform cTransform = rDel.CreateDecryptor();
                byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
    
                return UTF8Encoding.UTF8.GetString(resultArray);
            }
        }
    }
    View Code
  • 相关阅读:
    Eq Eqv Equal
    list append 总是复制前面的参数,而不复制最后一个参数
    Teach Yourself Scheme in Fixnum Days 13 Jump跳转
    python 操作 office
    python ImportError: DLL load failed: %1 不是有效的 Win32 应用程序
    pywin32 安装错误 ImportError: DLL load failed: 不是有效的 Win32 应用程序
    Python version 2.7 required, which was not found in the registry
    scheme 解释器Guile 使用
    Teach Yourself Scheme in Fixnum Days 6 recursion递归
    求一个正则表达式,字母不能重复
  • 原文地址:https://www.cnblogs.com/YK2012/p/6746745.html
Copyright © 2011-2022 走看看