C# AES加密解密

做事过程种 遇到171位的加密字符串  经过大量分析  找到其AES加密JS  但是不能直接利用  

查了多方资料  

找到一种C# 模拟AES加密的方法

原代码网址  http://rextester.com/RNR94562

using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Security.Cryptography;


namespace AES加密
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        private void Form1_Load(object sender, EventArgs e)
        {
            //string key = "59b50e345cab8b6d421b161918ea3fbd7e5921eea7d43d1ac54fa92cca452bb5";
            //string iv = "00000000000000000000000000000000";
            //string message = "3b16601d0a7e283c1f24d30ec214676885096cb0bbf3998012a2be87c5a58d89";
            //string encryptedReferenceOFB = "578946591ce2d787cbe41bec77a58dac66e6007fb722b1af847ecc3bf4212cea";
            //string encryptedReferenceECB = "d0aa1d38f2338de417a919cc5380c81d9a5f90b8cd505a4839eaa30ab524fd82";


            //var encryptedString = AesCsp.EncryptHexStrings(message, key, iv);
            //var decryptedString = AesCsp.DecryptHexStrings(encryptedString, key, iv);
            //Debug.Assert(message.ToUpper() == decryptedString.ToUpper());
            //Debug.Assert(encryptedString.ToUpper() == encryptedReferenceECB.ToUpper());
            // Console.WriteLine(encryptedString);
            //Console.WriteLine(encryptedReferenceECB);


        }


        private void btnRun_Click(object sender, EventArgs e)
        {
            string key =txtKey.Text;
            string iv = "00000000000000000000000000000000";
            string message = txtMd5.Text;
            var encryptedString = AesCsp.EncryptHexStrings(message, key, iv);


            txtResult.Text = encryptedString.ToString().ToLower();


        }


    }
    static class AesCsp
    {










        public static byte[] /*output*/ EncryptBytes(byte[] input, byte[] aesKey, byte[] aesIV)
        {
            Aes aes = new AesCryptoServiceProvider();
            aes.Padding = PaddingMode.None;
            aes.Mode = CipherMode.ECB;
            var enc = aes.CreateEncryptor(aesKey, aesIV);
            return enc.TransformFinalBlock(input, 0, input.Length);
        }


        public static byte[] DecryptBytes(byte[] encryptedOutput, byte[] aesKey, byte[] aesIV)
        {
            Aes aes = new AesCryptoServiceProvider();
            aes.Padding = PaddingMode.None;
            var dec = aes.CreateDecryptor(aesKey, aesIV);
            return dec.TransformFinalBlock(encryptedOutput, 0, encryptedOutput.Length);
        }


        public static string EncryptHexStrings(string input, byte[] aesKey, byte[] aesIV)
        {
            byte[] bytes = HexStringToByteArray(input);
            byte[] encBytes = EncryptBytes(bytes, aesKey, aesIV);
            return ByteArrayToHexString(encBytes);
        }


        public static string DecryptHexStrings(string encryptedOutput, byte[] aesKey, byte[] aesIV)
        {
            byte[] bytes = HexStringToByteArray(encryptedOutput);
            byte[] decBytes = DecryptBytes(bytes, aesKey, aesIV);
            return ByteArrayToHexString(decBytes);
        }


        public static string EncryptHexStrings(string input, string aesKey, string aesIV)
        {
            byte[] key = HexStringToByteArray(aesKey);
            byte[] iv = HexStringToByteArray(aesIV);
            return EncryptHexStrings(input, key, iv);
        }


        public static string DecryptHexStrings(string encryptedOutput, string aesKey, string aesIV)
        {
            byte[] key = HexStringToByteArray(aesKey);
            byte[] iv = HexStringToByteArray(aesIV);
            return DecryptHexStrings(encryptedOutput, key, iv);
        }


        public static byte[] HexStringToByteArray(string s)
        {
            byte[] ret = new byte[s.Length / 2];
            for (int i = 0; i < s.Length; i += 2)
            {
                ret[i / 2] = Convert.ToByte(s.Substring(i, 2), 16);
            }
            return ret;
        }


        public static string ByteArrayToHexString(byte[] bytes)
        {
            var sb = new StringBuilder();
            foreach (byte b in bytes)
                sb.AppendFormat("{0:X2}", b);
            return sb.ToString();
        }


    }

}


测试过关  make

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值