对称加密AES
static void Main(string[] args)
{
//string str = "rqiJI7eEICT+YZmScpAdbjzLnA4mgL3uU5uHXLBeaE6s8P3noNriVQZ/fO52ar8I9BSkVZIiyP8ArdG/KkXZi5Cn8rZaOVyEjgqhfFlTRIg=";
string str = "{ \"UserID\":1,\"Email\":null,\"LoginTime\":\"2018 - 12 - 12T11: 25:38.6208949 + 08:00\"";
string result = EncodeAES(str, "yhkZvOJSnTHG9hKT", "yhkZvOJSnTHG9hKT");
Console.WriteLine(result);
//Console.WriteLine(DecodeAES(str, "ptQJqRKxICCTeo6w", "O3vZvOJSnQDP9hKT"));
Console.ReadKey();
}
加密代码 需要引用System.Security.Cryptography命名空间
public static string EncodeAES(string text, string key, string iv)
{
RijndaelManaged rijndaelCipher = new RijndaelManaged();
rijndaelCipher.Mode = CipherMode.CBC;
rijndaelCipher.Padding = PaddingMode.Zeros;
rijndaelCipher.KeySize = ;
rijndaelCipher.BlockSize = ;
byte[] pwdBytes = System.Text.Encoding.UTF8.GetBytes(key);
byte[] keyBytes = new byte[];
int len = pwdBytes.Length;
if (len > keyBytes.Length)
len = keyBytes.Length;
System.Array.Copy(pwdBytes, keyBytes, len);
rijndaelCipher.Key = keyBytes;
rijndaelCipher.IV = Encoding.UTF8.GetBytes(iv);
ICryptoTransform transform = rijndaelCipher.CreateEncryptor();
byte[] plainText = Encoding.UTF8.GetBytes(text);
byte[] cipherBytes = transform.TransformFinalBlock(plainText, , plainText.Length);
return Convert.ToBase64String(cipherBytes);
}
解密
public static string DecodeAES(string text, string key, string iv)
{
RijndaelManaged rijndaelCipher = new RijndaelManaged();
rijndaelCipher.Mode = CipherMode.CBC;
rijndaelCipher.Padding = PaddingMode.Zeros;
rijndaelCipher.KeySize = ;
rijndaelCipher.BlockSize = ;
byte[] encryptedData = Convert.FromBase64String(text);
byte[] pwdBytes = System.Text.Encoding.UTF8.GetBytes(key);
byte[] keyBytes = new byte[];
int len = pwdBytes.Length;
if (len > keyBytes.Length)
len = keyBytes.Length;
System.Array.Copy(pwdBytes, keyBytes, len);
rijndaelCipher.Key = keyBytes;
rijndaelCipher.IV = Encoding.UTF8.GetBytes(iv);
ICryptoTransform transform = rijndaelCipher.CreateDecryptor();
byte[] plainText = transform.TransformFinalBlock(encryptedData, , encryptedData.Length);
return Encoding.UTF8.GetString(plainText);
}
原:https://blog.csdn.net/blueplus/article/details/80512438
对称加密AES的更多相关文章
- iOS CommonCrypto 对称加密 AES ecb,cbc
CommonCrypto 为苹果提供的系统加密接口,支持iOS 和 mac 开发: 不仅限于AES加密,提供的接口还支持其他DES,3DES,RC4,BLOWFISH等算法, 本文章主要讨论AES在i ...
- 对称加密----AES和DES加密、解密
目前主流的加密方式有:(对称加密)AES.DES (非对称加密)RSA.DSA 调用AES/DES加密算法包最精要的就是下面两句话: Cipher cipher = Cipher.get ...
- 密码 | 对称加密 - AES
一.AES 算法简介 高级加密标准(英语:Advanced Encryption Standard,缩写:AES),在密码学中又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准,用来替换 ...
- PHP对称加密-AES加密、DES加密
对称加密 对称加密算法是指,数据发信方将明文(原始数据)和密钥一起经过加密处理后,使其变成复杂的加密密文发送出去.收信方收到密文后,若要解读原文,则需要使用加密密钥及相关算法的逆算法对密文进行解密,使 ...
- 对称加密-AES
假设有一个发送方在向接收方发送消息.如果没有任何加密算法,接收方发送的是一个明文消息:“我是小灰”. 如果消息被中间人截获到,即使中间人无法篡改消息,也可以窥探到消息的内容,从而暴露了通信双方的私密. ...
- [Node.js] 对称加密、公钥加密和RSA
原文地址:http://www.moye.me/2015/06/14/cryptography_rsa/ 引子 对于加解密,我一直处于一种知其然不知其所以然的状态,项目核心部分并不倚重加解密算法时,可 ...
- 对称加密和分组加密中的四种模式(ECB、CBC、CFB、OFB)
一. AES对称加密: AES加密 分组 二. 分组密码的填充 分组密码的填充 e.g.: PKCS#5填充方式 三. 流密码: 四. 分组密码加密中的四种模式: 3.1 ECB模式 优点: 1. ...
- 加密算法--->对称加密与非对称加密算举例说明
目前主流的加密方式有:(对称加密)AES.DES (非对称加密)RSA.DSA 对称加密例子:des对称加密 des对称加密,对称加密,是一种比较传统的加密方式,其加密运算.解密运算使用 ...
- AES —— JAVA中对称加密和解密
package demo.security; import java.io.IOException; import java.io.UnsupportedEncodingException; impo ...
随机推荐
- NumPy学习_01 ndarray相关概念
1.NumPy库 NumPy = Numerical Python 是高性能科学计算和数据分析的基础库. pandas库充分借鉴了NumPy的相关概念,先行掌握NumPy库的用法,才能把pandas的 ...
- LR 11录制IE起不来
注:LR 11一般使用的是IE8或IE9 1.在录制脚本时Start Recoding中,默认如下,这样有可能IE打不开,需要更改路径,到对应的IE路径再尝试. 2.降低IE版本到IE8或者9 3.I ...
- centos 批量杀死进程
ps aux | grep 进程名| grep -v grep | awk '{print $2}' | xargs kill -9
- python基础之Day20part2
面向过程:核心是过程,解决问题的步骤,先干嘛后干嘛,机械流水线 复杂问题流程化简单化但扩展性差 面向对象:核心是对象,是特征与对象的集合体,基于该思想编写程序,就好比上帝造世界,上帝式的思维方式 扩展 ...
- MVC Razor视图下ViewData传递html内容被转义
页面输出: html源码: <div style="width: 90%; height: auto;"> <p>付凯航 阿豆 ADOU</p> ...
- union: php/laravel command
#########Laravel###############2018-01-09 16:46:26 # switch to maintenance mode php artisan down # s ...
- HDU-4763 Theme Section KMP
题意:求最长的子串E,使母串满足EAEBE的形式,A.B可以任意,并且不能重叠. 题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=4763 思 ...
- truncate和delete命令的优缺点和原理解析,适用场合
1.相同点:truncate和delete只删除数据不删除表的结构2.不同点:truncate.drop是数据库定义语言,操作立即生效,元数据不会放到rollback segement中,不能回滚,操 ...
- 解决Chrome 70及以上版本的证书问题:Failed to load resource: net::ERR_CERT_SYMANTEC_LEGACY
1.桌面必须要有Chrome 快捷方式 2.进入快捷方式属性 3.修改目标为:"C:\Program Files (x86)\Google\Chrome\Application\chrome ...
- keras常见参数input_dim、input_length理解
在看keras文档embedding层的时候,不太理解其中的input_dim 和input_length 这两个参数,查阅了一下资料,记录下来. keras.layers.Embedding(inp ...