AES-256-CBC 加密解密
//AES加密/解密
//在线AES加密解密工具。
// AES采用对称分组密码体制,
// 密钥长度支持为128/192/256bits。
// 用户密钥长度不足时,平台将以0x00自动填充。
// IV也一样,自动填充,超出部分将被忽略。
// 加密时会将明文数据按16byte进行分组,
// 不足16byte时将用特定的Padding(如PCKS7)字符进填充,
// 所以不同的Padding方式密文最后一段可能不一样。
// 如果没有特别指明平台将使用UTF8编码处理数据(如KEY/IV)。
测试网站
//https://the-x.cn/cryptography/Aes.aspx
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;namespace AesEncrypt
{/// <summary>/// Aes扩展方法/// </summary>public class AesEncryptExtension{public CipherMode Mode { get; internal set; }public PaddingMode Padding { get; internal set; }/// <summary>/// 加密/// </summary>/// <param name="Text">被加密的字符串</param>/// <param name="aesKey">对称算法的密钥</param>/// <param name="aesIV">设置对称算法的初始化向量</param>/// <returns>Base64</returns>public string Encrypt(string Text, byte[] aesKey, byte[] aesIV){byte[] encrypted;encrypted = EncryptStringToBytes_Aes(Text, aesKey, aesIV);return Convert.ToBase64String(encrypted);}private byte[] EncryptStringToBytes_Aes(string plainText, byte[] Key, byte[] IV){// Check arguments.if (plainText == null || plainText.Length <= 0){throw new ArgumentNullException("plainText");}if (Key == null || Key.Length <= 0){throw new ArgumentNullException("Key");}if (IV == null || IV.Length <= 0){throw new ArgumentNullException("IV");}byte[] encrypted;// Create an Aes object// with the specified key and IV.// using (RijndaelManaged aesAlg = new RijndaelManaged())using (Aes aesAlg = Aes.Create()){aesAlg.Key = Key;aesAlg.IV = IV;//設定 cipher 格式 AES-256-CBC//aesAlg.BlockSize = 256;//aesAlg.KeySize = 128;//aesAlg.FeedbackSize = 128;aesAlg.Padding = PaddingMode.PKCS7;aesAlg.Mode = CipherMode.CBC;//aesAlg.Key = (new SHA256Managed()).ComputeHash(Encoding.ASCII.GetBytes("IHazSekretKey"));//aesAlg.IV = System.Text.Encoding.ASCII.GetBytes("1234567890123456");// Create a decrytor to perform the stream transform.ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);// Create the streams used for encryption.using (MemoryStream msEncrypt = new MemoryStream()){using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)){//csEncrypt.FlushFinalBlock();using (StreamWriter swEncrypt = new StreamWriter(csEncrypt)){//Write all data to the stream.swEncrypt.Write(plainText);}encrypted = msEncrypt.ToArray();}}}// Return the encrypted bytes from the memory stream.return encrypted;}/// <summary>/// 解密/// </summary>/// <param name="text">被Aes加密后的Base64</param>/// <param name="aesKey">对称算法的密钥</param>/// <param name="aesIV">设置对称算法的初始化向量</param>/// <returns>字符串</returns>public string Decrypt(string text, byte[] aesKey, byte[] aesIV){string roundtrip;byte[] myByte = Convert.FromBase64String(text);roundtrip = DecryptStringFromBytes_Aes(myByte, aesKey, aesIV);return roundtrip;}private string DecryptStringFromBytes_Aes(byte[] cipherText, byte[] Key, byte[] IV){// Check arguments.if (cipherText == null || cipherText.Length <= 0){throw new ArgumentNullException("cipherText");}if (Key == null || Key.Length <= 0){throw new ArgumentNullException("Key");}if (IV == null || IV.Length <= 0){throw new ArgumentNullException("IV");}// Declare the string used to hold// the decrypted text.string plaintext;// Create an Aes object// with the specified key and IV.using (Aes aesAlg = Aes.Create()){aesAlg.Key = Key;aesAlg.IV = IV;// Create a decrytor to perform the stream transform.ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);// Create the streams used for decryption.using (MemoryStream msDecrypt = new MemoryStream(cipherText)){using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)){using (StreamReader srDecrypt = new StreamReader(csDecrypt)){// Read the decrypted bytes from the decrypting stream// and place them in a string.plaintext = srDecrypt.ReadToEnd();}}}}return plaintext;}}
}
AES-256-CBC 加密解密相关推荐
- Go Nodejs Java Aes 128 ECB加密解密结果保持一致
在多语言的生产环境下,常常是由一种语言进行加密而由另一种语言来进行解密,因此有必要保持各种语言之间加密解密算法的一致性.下面列出了Go,Nodejs,Java 的 Aes-128-Ecb的加密解密算法 ...
- aes 256 cbc java,AES256加解密java语言实现
AES256加解密java语言实现 写在前面 基于项目安全性需要,有时候我们的项目会使用AES 256加解密算法.以下,是针对实现AES256 Padding7加密算法实现的关键步骤解析以及此过程遇到 ...
- java aes cfb 256_aes加密解密源码,包括aes128、aes192、aes256位,以及cbc、cfb、ecb、ofb、pcbc模式...
AES加解密总共有以下这些 算法/模式/填充 字节加密后数据长度 不满16字节加密后长度 AES/CBC/NoPadding ...
- aes 256 ecb 加解密 pkcs7补全 python JS
python aes 256 ecb 加解密 功能 实现 Python ECB 256 JS版本 ECB 算法 JS版本 CBC 算法Pkcs7填充 SQL AES 在线验证网站 notice 功能 ...
- Vue前端和Java后端 联调使用AES 前后端加密解密
Vue前端和Java后端 联调使用AES 前后端加密解密 最近在项目中需要针对重要数据进行加密传输,在网上找了一大推加密方式 最终采用AES 加密 Java端 package com.zk.web.u ...
- angular和JAVA实现aes、rsa加密解密,前后端交互,前端加解密和后端JAVA加解密实现
今天实现了下AES和RSA加密解密,主要的功能是对前后端交互数据进行加密解密,为什么要用到两个算法呢,首先RSA默认的话加密长度是有限的100多个byte吧大约,并且需要公钥私钥,而AES加密没有限制 ...
- C#国密SM4 CBC加密解密
** C#国密SM4 CBC加密解密 在你得项目nuget引用程序集:KYSharp.SM 安装 2.0 版本,里面才有sm4的加密 ** static void Main(string[] args ...
- python aes 加盐 加密解密
python aes 加盐 加密解密 爬虫就是在学习的过程,前几天从某网站的js里学到的加密方式,通过下断点弄清加密原理后,用python复现一下,在这里和大家分享下 #--------引入模块--- ...
- AES CBC 加密解密(偏移量)
参考文章:AES加密解密(ECB模式) 工具:在线AES加密解密 使用固定的key package com.eshore.cloud.utils;import android.text.TextUti ...
- AES CBC加密/解密
简介 高级加密标准(英语:Advanced Encryption Standard,缩写:AES),在密码学中又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准.这个标准用来替代原先的D ...
最新文章
- mysql的安全管理工具_最安全mysql管理工具
- python计算两个字典的相同点(从key、value、item角度)
- 约瑟夫环双向链表c语言实,双向链表与约瑟夫环代码
- python创建txt文件-python创建txt文件
- iOS学习笔记16-SQLite应用
- 服务器操作系统策略,Windows操作系统组策略应用全攻略(2)服务器教程 -电脑资料...
- VTK:Utilities之ShareCamera
- 【C++基础】STL迭代器
- 如何在scoped不污染组件样式的前提下,实现el-input组件样式覆盖?
- LeetCode刷题(36)--Text Justification
- python算法程序_浅谈python常用程序算法
- 通过 Telnet 在 Linux 终端中观看ASCII 星球大战
- 实现MFC扩展DLL中导出类和对话框
- AD14简单使用教程
- c语言实现生成随机数
- 网页爬虫-八爪鱼Xpath自定义数字翻页
- Intel CPU发展史
- Word 如何删除分节符?
- 前端网页按钮效果的展示
- Web功能设计:登录与退出