//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 加密解密相关推荐

  1. Go Nodejs Java Aes 128 ECB加密解密结果保持一致

    在多语言的生产环境下,常常是由一种语言进行加密而由另一种语言来进行解密,因此有必要保持各种语言之间加密解密算法的一致性.下面列出了Go,Nodejs,Java 的 Aes-128-Ecb的加密解密算法 ...

  2. aes 256 cbc java,AES256加解密java语言实现

    AES256加解密java语言实现 写在前面 基于项目安全性需要,有时候我们的项目会使用AES 256加解密算法.以下,是针对实现AES256 Padding7加密算法实现的关键步骤解析以及此过程遇到 ...

  3. java aes cfb 256_aes加密解密源码,包括aes128、aes192、aes256位,以及cbc、cfb、ecb、ofb、pcbc模式...

    AES加解密总共有以下这些 算法/模式/填充                 字节加密后数据长度       不满16字节加密后长度 AES/CBC/NoPadding                 ...

  4. aes 256 ecb 加解密 pkcs7补全 python JS

    python aes 256 ecb 加解密 功能 实现 Python ECB 256 JS版本 ECB 算法 JS版本 CBC 算法Pkcs7填充 SQL AES 在线验证网站 notice 功能 ...

  5. Vue前端和Java后端 联调使用AES 前后端加密解密

    Vue前端和Java后端 联调使用AES 前后端加密解密 最近在项目中需要针对重要数据进行加密传输,在网上找了一大推加密方式 最终采用AES 加密 Java端 package com.zk.web.u ...

  6. angular和JAVA实现aes、rsa加密解密,前后端交互,前端加解密和后端JAVA加解密实现

    今天实现了下AES和RSA加密解密,主要的功能是对前后端交互数据进行加密解密,为什么要用到两个算法呢,首先RSA默认的话加密长度是有限的100多个byte吧大约,并且需要公钥私钥,而AES加密没有限制 ...

  7. C#国密SM4 CBC加密解密

    ** C#国密SM4 CBC加密解密 在你得项目nuget引用程序集:KYSharp.SM 安装 2.0 版本,里面才有sm4的加密 ** static void Main(string[] args ...

  8. python aes 加盐 加密解密

    python aes 加盐 加密解密 爬虫就是在学习的过程,前几天从某网站的js里学到的加密方式,通过下断点弄清加密原理后,用python复现一下,在这里和大家分享下 #--------引入模块--- ...

  9. AES CBC 加密解密(偏移量)

    参考文章:AES加密解密(ECB模式) 工具:在线AES加密解密 使用固定的key package com.eshore.cloud.utils;import android.text.TextUti ...

  10. AES CBC加密/解密

    简介 高级加密标准(英语:Advanced Encryption Standard,缩写:AES),在密码学中又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准.这个标准用来替代原先的D ...

最新文章

  1. mysql的安全管理工具_最安全mysql管理工具
  2. python计算两个字典的相同点(从key、value、item角度)
  3. 约瑟夫环双向链表c语言实,双向链表与约瑟夫环代码
  4. python创建txt文件-python创建txt文件
  5. iOS学习笔记16-SQLite应用
  6. 服务器操作系统策略,Windows操作系统组策略应用全攻略(2)服务器教程 -电脑资料...
  7. VTK:Utilities之ShareCamera
  8. 【C++基础】STL迭代器
  9. 如何在scoped不污染组件样式的前提下,实现el-input组件样式覆盖?
  10. LeetCode刷题(36)--Text Justification
  11. python算法程序_浅谈python常用程序算法
  12. 通过 Telnet 在 Linux 终端中观看ASCII 星球大战
  13. 实现MFC扩展DLL中导出类和对话框
  14. AD14简单使用教程
  15. c语言实现生成随机数
  16. 网页爬虫-八爪鱼Xpath自定义数字翻页
  17. Intel CPU发展史
  18. Word 如何删除分节符?
  19. 前端网页按钮效果的展示
  20. Web功能设计:登录与退出

热门文章

  1. ns2 + GT-ITM
  2. QNX独特的工程目录结构
  3. 相机小孔成像模型(逐步推导详解)
  4. win10安装mosquitto注意事项
  5. user-cf算法思考-K值的确定
  6. DB2 9 独霸开辟(733 磨练)认证指南,第 9 部门: 用户定义的例程(6)
  7. 下单后,买家申请退款怎么办?(附金牌客服挽留方案及话术)
  8. 金融时间序列分析:8. MA模型实例(Python)
  9. antdesign卡片_10分钟精通Ant Design Form表单
  10. JAXB根据带继承关系的类生成soap请求的XML报文(互转)