using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks; namespace AESDecryptDemo
{
public class AES
{
public static string AESEncryptASCII(string plainStr)
{
StringBuilder sb = new StringBuilder();
byte[] bKey = Encoding.UTF8.GetBytes(GetKey());
byte[] bIV = Encoding.UTF8.GetBytes(GetIV());
byte[] byteArray = Encoding.UTF8.GetBytes(plainStr); string encrypt = null;
Rijndael aes = Rijndael.Create();
using (MemoryStream mStream = new MemoryStream())
{
using (CryptoStream cStream = new CryptoStream(mStream, aes.CreateEncryptor(bKey, bIV), CryptoStreamMode.Write))
{
cStream.Write(byteArray, 0, byteArray.Length);
cStream.FlushFinalBlock();
encrypt = Encode(Convert.ToBase64String(mStream.ToArray()));
}
}
aes.Clear();
return encrypt;
}
public static string AESEncrypt(string plainStr)
{
StringBuilder sb = new StringBuilder();
byte[] bKey = Encoding.UTF8.GetBytes(GetKey());
byte[] bIV = Encoding.UTF8.GetBytes(GetIV());
byte[] byteArray = Encoding.UTF8.GetBytes(plainStr); string encrypt = null;
Rijndael aes = Rijndael.Create();
using (MemoryStream mStream = new MemoryStream())
{
using (CryptoStream cStream = new CryptoStream(mStream, aes.CreateEncryptor(bKey, bIV), CryptoStreamMode.Write))
{
cStream.Write(byteArray, 0, byteArray.Length);
cStream.FlushFinalBlock();
encrypt = Convert.ToBase64String(mStream.ToArray());
}
}
aes.Clear();
return encrypt;
}
public static string AESDecryptASCII(string encryptStr)
{
encryptStr = Decode(encryptStr);
byte[] bKey = Encoding.UTF8.GetBytes(GetKey());
byte[] bIV = Encoding.UTF8.GetBytes(GetIV());
byte[] byteArray = Convert.FromBase64String(encryptStr); string decrypt = null;
Rijndael aes = Rijndael.Create();
using (MemoryStream mStream = new MemoryStream())
{
using (CryptoStream cStream = new CryptoStream(mStream, aes.CreateDecryptor(bKey, bIV), CryptoStreamMode.Write))
{
cStream.Write(byteArray, 0, byteArray.Length);
cStream.FlushFinalBlock();
decrypt = Encoding.UTF8.GetString(mStream.ToArray());
}
}
aes.Clear();
return decrypt;
}
public static string AESDecrypt(string encryptStr)
{
byte[] bKey = Encoding.UTF8.GetBytes(GetKey());
byte[] bIV = Encoding.UTF8.GetBytes(GetIV());
byte[] byteArray = Convert.FromBase64String(encryptStr); string decrypt = null;
Rijndael aes = Rijndael.Create();
using (MemoryStream mStream = new MemoryStream())
{
using (CryptoStream cStream = new CryptoStream(mStream, aes.CreateDecryptor(bKey, bIV), CryptoStreamMode.Write))
{
cStream.Write(byteArray, 0, byteArray.Length);
cStream.FlushFinalBlock();
decrypt = Encoding.UTF8.GetString(mStream.ToArray());
}
}
aes.Clear();
return decrypt;
}
public static string Encode(string strEncode)
{
string strReturn = "";// 存储转换后的编码
foreach (short shortx in strEncode.ToCharArray())
{
strReturn += shortx.ToString("X");
}
return strReturn;
} public static string Decode(string strDecode)
{
string sResult = "";
for (int i = 0; i < strDecode.Length / 2; i++)
{
sResult += (char)short.Parse(strDecode.Substring(i * 2, 2), global::System.Globalization.NumberStyles.HexNumber);
}
return sResult;
}
private static string GetKey()
{
return @")O[NB]6,YF}+efcaj{+oESb9d8>Z'e9M";
} /// <summary>
/// 获取向量
/// </summary>
private static string GetIV()
{
return @"L+\~f4,Ir)b$=pkf";
}
}
}

  

c# AES加解密并转ASCII码的更多相关文章

  1. c# Aes加解密和对象序列化

    aes加解密 public class AesCryptto { private string key = "hjyf57468jhmuist"; private string i ...

  2. Java、C#双语版配套AES加解密示例

      这年头找个正经能用的东西那是真难,网上一搜索一大堆,正经能用的没几个,得,最后还是得靠自己,正巧遇上需要AES加解密的地方了,而且还是Java和C#间的相互加解密操作,这里做个备忘 这里采用的加解 ...

  3. AES加解密算法Qt实现

    [声明] (1) 本文源码 在一位未署名网友源码基础上,利用Qt编程,实现了AES加解密算法,并添加了文件加解密功能.在此表示感谢!该源码仅供学习交流,请勿用于商业目的. (2) 图片及描述 除图1外 ...

  4. 转载:Java、C#双语版配套AES加解密示例

    转载,原文出处 http://www.cnblogs.com/lzrabbit/p/3639503.html 这年头找个正经能用的东西那是真难,网上一搜索一大堆,正经能用的没几个,得,最后还是得靠自己 ...

  5. C#与java中的AES加解密互解算法

    一.C#版AES加解密算法 public class AESCode { public string Key { get; set; } public string Encrypt(string va ...

  6. [转帖]前端 crypto-js aes 加解密

    前端 crypto-js aes 加解密 2018.04.13 11:37:21字数 891阅读 59767 https://www.jianshu.com/p/a47477e8126a 原来前端也有 ...

  7. java AES加解密

    AES加解密工具类 package com.yan.demo; import org.apache.commons.lang3.StringUtils; import sun.misc.BASE64D ...

  8. aes加解密前后端-后台

    一.web.xml: <filter> <filter-name>fastLoginFilter</filter-name> <filter-class> ...

  9. DES,AeS加解密,MD5,SHA加密

    1.DES一共就有4个参数参与运作:明文.密文.密钥.向量.其中这4者的关系可以理解为: 密文=明文+密钥+向量: 明文=密文-密钥-向量: 为什么要向量这个参数呢?因为如果有一篇文章,有几个词重复, ...

随机推荐

  1. 洛谷 P1015 回文数 Label:续命模拟QAQ

    题目描述 若一个数(首位不为零)从左向右读与从右向左读都一样,我们就将其称之为回文数. 例如:给定一个10进制数56,将56加65(即把56从右向左读),得到121是一个回文数. 又如:对于10进制数 ...

  2. LBS基站数据解析接口

    http://www.cellocation.com/interfac/#hybrid http://www.cellid.cn/ https://www.juhe.cn/docs/api/id/8

  3. About_PHP

    所谓PHP: 超文本预处理器 外文名称 Hypertext Preprocessor 编程范型 面向对象.命令式编程 php就是比js更高端的一种语言. 语法有两种: <?php      ?& ...

  4. MVVM deep dive

    You can get a different instance each time by passing a different key to the GetInstance method. How ...

  5. HttpWebRequest Post callback

    public void GetValueFromRequest(string postData) { var request = (HttpWebRequest) WebRequest.CreateH ...

  6. onselectstart

    以前在做图片滚动时,在双击左右箭头,快速切换图片滚动时,会选择附近区域的文字,感觉不是很好,今天在同事在分享时,讲到了这个问题, 试了一下,不错,解决了问题IE及Chrome下的方法一样,对相应的元素 ...

  7. MaterialCalendarView使用时遇到的问题

    一.概述 MaterialCalendarView是一个开源项目.功能强大支持多选.单选.标注等. 二.问题 1.其继承自ViewGroup,故与CalendarView半毛钱关系都没有,完全是一个新 ...

  8. c# select标签绑定枚举,并以Description做Text显示

    今天在做项目时遇到一个问题: 开发中有些字段是枚举类型如 Dept 企业表中可能有个字段 Property 性质 0:事业单位,1:私企,2:外企,但有时我们不会单独为性质这个字段定义一张表, 而是在 ...

  9. thinkphp打印执行的sql

    打印下执行的sql,也就是在执行add或save语句下面写下: echo M()->getlastsql();

  10. php中英字符串截取

    <?php @header('Content-type: text/html; charset=UTF-8'); function Ctruncate($str = '', $len = 0, ...