给定密钥加密和解密字符串

编程入门 行业动态 更新时间:2024-10-24 14:28:13
本文介绍了给定密钥加密和解密字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我继承了下面的代码。不幸的是,hello_world的解密值不是:

I inherited the code below. Unfortunately, the decrypted value of hello_world is not:

hello world

但是(以我为例):

&�|ktR���ڼ��S����%��< ���8�

有什么想法吗?似乎每次的结果都不同,这在给定代码的情况下是显而易见的。我可以更改此设置,以便发送一次加密的数据,以后再解密吗?谢谢!

Any ideas? It also appears that the result is different every time, which is kind of obvious given the code. Could I change this so that I can send the data encryted once and then decrypt in the future again? Thanks!

代码:

using System; using System.IO; using System.Security.Cryptography; namespace crypt { class Program { static void Main(string[] args) { var key = @"abcdefghijklmnopqrstuvw=="; using (var aesAlg = Aes.Create()) { aesAlg.Mode = CipherMode.CBC; aesAlg.Padding = PaddingMode.PKCS7; aesAlg.Key = Convert.FromBase64String(key); aesAlg.GenerateIV(); var encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV); var enc_hello_world = EncryptProperty(encryptor, "hello world"); var hello_world = DecryptProperty(encryptor, enc_hello_world); } } private static string EncryptProperty(ICryptoTransform encryptor, string valueToEncrypt) { byte[] encrypted; using (var msEncrypt = new MemoryStream()) { using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)) { using (var swEncrypt = new StreamWriter(csEncrypt)) { swEncrypt.Write(valueToEncrypt); } encrypted = msEncrypt.ToArray(); } } return Convert.ToBase64String(encrypted); } private static string DecryptProperty(ICryptoTransform decryptor, string valueToDecrypt) { string decrypted; using (var msDecrypt = new MemoryStream(Convert.FromBase64String(valueToDecrypt))) { using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) { using (var srDecrypt = new StreamReader(csDecrypt)) { decrypted = srDecrypt.ReadToEnd(); } } } return decrypted; } } }

推荐答案

AES-CBC需要两个变量来编码和解码数据:密钥和IV(初始化向量)。初始化向量可以纯文本形式发送,不会导致加密的安全性变差。

AES-CBC requires two variables to encode and decode data: key and IV (initialization vector). Initialization vector can be sent in plaintext and it doesn't result in worse security of your encryption.

aesAlg.GenerateIV();

这是创建IV的位置,您需要存储此位置(我将其添加到

This is where your IV gets created, you need to store this (I do this by prepending that to the resulting data) and then access it and set the IV when decrypting.

您也可以使用空的IV,但这将使攻击者更容易公开您的密钥,因此,不建议这样做。

You could also use an empty IV but this will make it easier for attackers to expose your key, so this is not recommended.

以下是C#中AES的一个很好的示例: gist.github/mark-adams/87aa34da3a5ed48ed0c7 (似乎正在使用我提到的方法)。

Here is a good example of AES in C#: gist.github/mark-adams/87aa34da3a5ed48ed0c7 (it seems to be using the method I've mentioned).

更多推荐

给定密钥加密和解密字符串

本文发布于:2023-11-13 01:45:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1583071.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:密钥   字符串

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!