当前位置:网站首页>C AES CBC mode encryption and decryption
C AES CBC mode encryption and decryption
2022-07-21 18:18:00 【Tiantian code code Tiantian】
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
namespace Test
{
class Program
{
static void Main(string[] args)
{
String encrypt = Encrypt("lw112190 Test information ");
Console.WriteLine(encrypt);
String decrypt = Decrypt(encrypt);
Console.WriteLine(decrypt);
Console.ReadKey();
}
static string key = "1234567890abcdef";
static string IV = "1234567890abcdef";
/// <summary>
/// AES encryption
/// </summary>
/// <param name="encryptStr"> Plaintext </param>
/// <returns></returns>
static private string Encrypt(string encryptStr)
{
var _aes = new AesCryptoServiceProvider();
_aes.BlockSize = 128;
_aes.KeySize = 256;
_aes.Key = Encoding.UTF8.GetBytes(key);
_aes.IV = Encoding.UTF8.GetBytes(IV);
_aes.Padding = PaddingMode.PKCS7;
_aes.Mode = CipherMode.CBC;
var _crypto = _aes.CreateEncryptor(_aes.Key, _aes.IV);
byte[] encrypted = _crypto.TransformFinalBlock(Encoding.UTF8.GetBytes(encryptStr), 0, Encoding.UTF8.GetBytes(encryptStr).Length);
_crypto.Dispose();
return System.Convert.ToBase64String(encrypted);
}
/// <summary>
/// AES Decrypt
/// </summary>
/// <param name="decryptStr"> Ciphertext </param>
/// <returns></returns>
static private string Decrypt(string decryptStr)
{
var _aes = new AesCryptoServiceProvider();
_aes.BlockSize = 128;
_aes.KeySize = 256;
_aes.Key = Encoding.UTF8.GetBytes(key);
_aes.IV = Encoding.UTF8.GetBytes(IV);
_aes.Padding = PaddingMode.PKCS7;
_aes.Mode = CipherMode.CBC;
var _crypto = _aes.CreateDecryptor(_aes.Key, _aes.IV);
byte[] decrypted = _crypto.TransformFinalBlock(System.Convert.FromBase64String(decryptStr), 0, System.Convert.FromBase64String(decryptStr).Length);
_crypto.Dispose();
return Encoding.UTF8.GetString(decrypted);
}
}
}
边栏推荐
- 使用zdog实现瞭望灯塔动画js特效
- LabelImg(目标检测标注工具)的安装与使用教程
- IDEA 忽略target目录
- Question 133 of Li Kou: numbers that appear only once
- 使用优炫数据库时,如何查看系统是否支持某组件
- 科软 信息安全 期末考试回忆
- 不知道 MySQL 咋学?刷完牛客这 50 道题就够了!(第七篇)
- Don't know how to learn MySQL? It's enough to finish the 50 questions of Niuke! (Part VIII)
- leetcode:1125. The smallest necessary team [shape pressing DP board + set covering board]
- Knowledge atlas enables digital economy. The 16th National knowledge atlas and Semantic Computing Conference (CCKS 2022) is about to be held
猜你喜欢
PKI certificate system
IP协议号大全
Zdog漂浮旋转动画js特效代码
Don't know how to learn MySQL? It's enough to finish the 50 questions of Niuke (Chapter 10)
CLWY权限管理(五)--- 用户登录
网络模拟器 eNSP、EVE-NG、GNS3、Packet Tracert
“为了买台手机,研究大半个月后仍然无从选择”
DNS域名解析服务
FigDraw 16. SCI 文章绘图之树形图(Dendrogram)
02_ UE4 advanced_ HP strip and blood deduction mechanism
随机推荐
物体识别桌的应用都具备哪些功能
Yolox ---- human doubts!
[C]语言动态内存管理
Use the esp32 download tool to burn firmware for esp32
CLWY权限管理(五)--- 用户登录
NFS FTP PXE
Network Security Learning (Qianfeng network security notes) 3 -- batch preparation
“为了买台手机,研究大半个月后仍然无从选择”
复盘:智能座舱系列文一,他到底是什么
Baize_foc esp32无刷电机驱动错误问题记录贴
解决CNN固有缺陷, CCNN凭借单一架构,实现多项SOTA
六.实战——Excel表格的导入和导出
CAD browsing mode and drawing mode, how CAD prints hundreds of drawings at one time
Don't know how to learn MySQL? It's enough to finish the 50 questions of Niuke! (Part IX)
MySQL BETWEEN 语法
Qdu summer training first week limited time training 1
Browsersync web server setup
网络安全学习(千锋网络安全笔记)4--用户与组管理
科软 信息安全 期末考试回忆
Resume: smart cockpit series article 3 - the touch of five interactive technologies behind it