惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园_首页
C
Cybersecurity and Infrastructure Security Agency CISA
C
Cyber Attacks, Cyber Crime and Cyber Security
Project Zero
Project Zero
P
Proofpoint News Feed
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
Cisco Blogs
V
Vulnerabilities – Threatpost
G
GRAHAM CLULEY
N
News | PayPal Newsroom
NISL@THU
NISL@THU
雷峰网
雷峰网
J
Java Code Geeks
Latest news
Latest news
aimingoo的专栏
aimingoo的专栏
Microsoft Azure Blog
Microsoft Azure Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Cisco Talos Blog
Cisco Talos Blog
Hacker News: Ask HN
Hacker News: Ask HN
AWS News Blog
AWS News Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
T
The Exploit Database - CXSecurity.com
P
Privacy International News Feed
C
CXSECURITY Database RSS Feed - CXSecurity.com
Vercel News
Vercel News
Spread Privacy
Spread Privacy
V2EX - 技术
V2EX - 技术
S
Schneier on Security
K
Kaspersky official blog
Recent Announcements
Recent Announcements
T
Threat Research - Cisco Blogs
B
Blog RSS Feed
S
SegmentFault 最新的问题
Security Archives - TechRepublic
Security Archives - TechRepublic
Stack Overflow Blog
Stack Overflow Blog
Hugging Face - Blog
Hugging Face - Blog
Apple Machine Learning Research
Apple Machine Learning Research
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
W
WeLiveSecurity
PCI Perspectives
PCI Perspectives
The GitHub Blog
The GitHub Blog
The Last Watchdog
The Last Watchdog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
博客园 - 【当耐特】
Engineering at Meta
Engineering at Meta
Scott Helme
Scott Helme
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
量子位
A
Arctic Wolf

博客园 - 为森

NPM设置镜像 WCF 自托管、无配置文件实现jsonp(跨域)的访问 关于nodejs4.0 npm乱码以及离线全局安装时要注意的问题 C# 正则表达式 转自-每日一bo 关于async & await(TAP)异步模型的异常捕获 一个Public的字段引起的,谈谈继承中的new 解决Bootstrap 附加导航(Affix)的问题和使用时若干注意事项 一次小异常的排查,悲剧的无以复加!!! async & await 小扩展大用处,自己扩展一个ForeachRead吧 转 常用工具和技术 implicit和 explicit关键字 MVC 自定义 以表达式树为参数的htmlhelper EF 的一些不常用的功能 详细说明 配置 Sublime Text 开发node.js(windows)包括sub2和sub3的区别 sql 判断 数据库 表 字段 是否存在 转 WCF中同步和异步通讯总结 关于WCF 解决EF一对一或多对一的删除
解决解密时出现"要解密的数据的长度无效" 或 "填充无效无法被移除" 的错误
为森 · 2014-08-26 · via 博客园 - 为森

1、首先排除数据库中读取加密后的字段是否被强制截断。

2、AES加密后的byte[]首先应用base64( Convert.ToBase64String)编码一次,若直接用utf8的话会报上述错误,若用unicode编码的话会解密成乱码,原因是加密后的byte数组用其他编码方式编码的话会丢失字符。

3、base编码后的字符串恢复为数组可用Convert.FromBase64String。

加密:

public static byte[] AESEncrypt(string plainText)
{
            SymmetricAlgorithm des = Rijndael.Create();
            byte[] inputByteArray = Encoding.UTF8.GetBytes(plainText);            
            des.Key = Encoding.UTF8.GetBytes(keys);
            des.IV = _key1;

ICryptoTransform cTransform
= des.CreateEncryptor(); return cTransform.TransformFinalBlock(inputByteArray, 0, inputByteArray.Length); }

解密:

public static byte[] AESDecrypt(byte[] cipherText)
{
            SymmetricAlgorithm des = Rijndael.Create();
            des.Key = Encoding.UTF8.GetBytes(keys);
            des.IV = _key1;

            ICryptoTransform cTransform = des.CreateDecryptor();
            return cTransform.TransformFinalBlock(cipherText, 0, cipherText.Length);
}
public static byte[] _key1 = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
public static string keys = "dongbinhuiasxiny";//密钥,128位