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

推荐订阅源

WordPress大学
WordPress大学
Project Zero
Project Zero
Engineering at Meta
Engineering at Meta
Microsoft Azure Blog
Microsoft Azure Blog
小众软件
小众软件
T
The Blog of Author Tim Ferriss
S
SegmentFault 最新的问题
量子位
V
Visual Studio Blog
F
Full Disclosure
博客园 - 叶小钗
Recent Announcements
Recent Announcements
G
Google Developers Blog
博客园 - Franky
F
Fortinet All Blogs
有赞技术团队
有赞技术团队
B
Blog
aimingoo的专栏
aimingoo的专栏
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
N
Netflix TechBlog - Medium
L
LINUX DO - 最新话题
S
Security @ Cisco Blogs
Google DeepMind News
Google DeepMind News
B
Blog RSS Feed
Cloudbric
Cloudbric
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Last Week in AI
Last Week in AI
SecWiki News
SecWiki News
H
Hackread – Cybersecurity News, Data Breaches, AI and More
云风的 BLOG
云风的 BLOG
阮一峰的网络日志
阮一峰的网络日志
N
News and Events Feed by Topic
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Microsoft Security Blog
Microsoft Security Blog
D
DataBreaches.Net
人人都是产品经理
人人都是产品经理
Schneier on Security
Schneier on Security
Webroot Blog
Webroot Blog
T
Troy Hunt's Blog
P
Proofpoint News Feed
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Jina AI
Jina AI
PCI Perspectives
PCI Perspectives
Martin Fowler
Martin Fowler
O
OpenAI News
Hacker News: Ask HN
Hacker News: Ask HN
S
Secure Thoughts
P
Privacy International News Feed
I
InfoQ
C
Cyber Attacks, Cyber Crime and Cyber Security

博客园 - 为森

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位