WEB技术论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

搜索
热搜: ASP asp.net php
查看: 126|回复: 0
打印 上一主题 下一主题

C# 数据通讯均采用AES/CBC/PKCS5Padding模式加密

[复制链接]

534

主题

541

帖子

2044

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
2044
跳转到指定楼层
楼主
发表于 2018-8-12 16:56:37 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Web;

/// <summary>
/// AES 的摘要说明
/// </summary>
public class AES
{

    public static string md5(String str)
    {
        return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5").ToUpper();
    }

    public static string SkillCurl(/string url, string parameters/index.html)
    {


        byte[] bytes = Encoding.UTF8.GetBytes(parameters);

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        request.Method = "POST";
        request.Timeout = 10000;
        request.ContentType = "application/x-www-form-urlencoded";
        request.ContentLength = bytes.Length;

        Stream stream = request.GetRequestStream();
        stream.Write(bytes, 0, bytes.Length);
        stream.Close();

        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        Stream stream1 = response.GetResponseStream();
        if (stream1 == null) return "";

        StreamReader reader = new StreamReader(stream1, Encoding.UTF8);

        return reader.ReadToEnd();
    }

         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);
      string res=  UTF8Encoding.UTF8.GetString(resultArray);
      return res;
    }

}

爱仿站的中年人
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

|手机版|码农集营 ( ) 深圳龙岗交友群

GMT+8, 2018-11-7 18:27 , Processed in 0.084759 second(s), 22 queries .

Powered by 码农集营 X3.4

快速回复 返回顶部 返回列表