对称加密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 ...
随机推荐
- APP支付(.NET版)
---恢复内容开始--- APP支付(.NET版) 一. 支付宝支付 1. 有一个支付账号,在蚂蚁金服开放平台中登录账号→选择“管理中心”→在“开发者中心”下选择“网页&移动应用”→然后按 ...
- Python 的 14 张思维导图汇总
本文主要涵盖了 Python 编程的核心知识(暂不包括标准库及第三方库,后续会发布相应专题的文章). 首先,按顺序依次展示了以下内容的一系列思维导图:基础知识,数据类型(数字,字符串,列表,元组,字典 ...
- 29. pt-table-usage
pt-table-usage --query="select * from t01 join t02 on t01.id=t02.id where t01.code=2" pt-t ...
- mr微博内容推荐
第一次迭代 1 package com.laoxiao.mr.weibo; import java.io.StringReader; import org.apache.commons.lan ...
- Quartz的自定义插件
quartz本身插件: LoggingJobHistoryPlugin,LoggingTriggerHistoryPlugin分别可以打印scheduler容器管理的所有triggers和jobDet ...
- thymeleaf拆分头部(head)显示异常问题
问题描述: 刚用thymeleaf不久,考虑到公共头部的导入css,js代码,需要拆分. 拆分之后,bootstrap-select下拉多选框出现“样式异常”,本认为是头部拆分问题,css样式未导入成 ...
- 移动端各种滚动场景需求的插件better-scroll
移动端各种滚动场景需求的插件: 文档地址: better-scroll:https://ustbhuangyi.github.io/better-scroll/doc/zh-hans/#better- ...
- JAVA 8 主要新特性 ----------------(七)新时间日期 API ----- Duration “时间”间隔
Duration:用于计算两个“时间”间隔 简介: 用法: 1.Zero常量 实例: Duration duration = Duration.ZERO; System.out.println(&qu ...
- 让Spring Boot项目启动时可以根据自定义配置决定初始化哪些Bean
让Spring Boot项目启动时可以根据自定义配置决定初始化哪些Bean 问题描述 实现思路 思路一 [不符合要求] 思路二[满足要求] 思路三[未试验] 问题描述 目前我工作环境下,后端主要的框架 ...
- linux shell 变量子串
linx变量子串 在本例子中,变量 test=https://www.//cnblogs./com//jjmaokk/p/10135401.html 1,${#parameter} 返回变量$para ...