【发布时间】:2020-08-28 14:19:50
【问题描述】:
我正在尝试加密/解密密码,加密密码工作正常 但是解密密码返回错误的结果.....
示例:
密码输入:P@ssword
密码加密:V????0B??y+?Em
密码解密:�/T:/�b, 我从那里上课
https://docs.microsoft.com/en-us/dotnet/api/system.security.cryptography.aesmanaged?view=netcore-3.1 我的代码: AesManaged 类using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Solution_Report
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
byte[] IV = Encoding.ASCII.GetBytes("9316FF86D31244AC");
byte[] Key = Encoding.ASCII.GetBytes("28FDDEF86DA4244ACCC0A4FE3B316F26");
private void Form1_Load(object sender, EventArgs e)
{
}
private void btn_Encrypt_Click(object sender, EventArgs e)
{
byte [] Encrypted_Pass_byte = AesExample.EncryptStringToBytes_Aes(txt_Password.Text, Key , IV);
txt_Password_Encrypted.Text = Encoding.ASCII.GetString(Encrypted_Pass_byte);
}
private void btn_decrypt_Click(object sender, EventArgs e)
{
byte[] Encrypted_Password = Encoding.ASCII.GetBytes(txt_Password_Encrypted.Text);
txt_Password_Decrpted.Text = AesExample.DecryptStringFromBytes_Aes(Encrypted_Password, Key, IV);
}
}
}
class AesExample
{
public static void en()
{
string original = "Here is some data to encrypt!";
// Create a new instance of the Aes
// class. This generates a new key and initialization
// vector (IV).
using (Aes myAes = Aes.Create())
{
// Encrypt the string to an array of bytes.
byte[] encrypted = EncryptStringToBytes_Aes(original, myAes.Key, myAes.IV);
// Decrypt the bytes to a string.
string roundtrip = DecryptStringFromBytes_Aes(encrypted, myAes.Key, myAes.IV);
//Display the original data and the decrypted data.
Console.WriteLine("Original: {0}", original);
Console.WriteLine("Round Trip: {0}", roundtrip);
}
}
public static 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 (Aes aesAlg = Aes.Create())
{
aesAlg.Key = Key;
aesAlg.IV = IV;
aesAlg.Padding = PaddingMode.Zeros;
// Create an encryptor 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))
{
using (BinaryWriter swEncrypt = new BinaryWriter(csEncrypt))
{
//Write all data to the stream.
swEncrypt.Write(plainText);
}
encrypted = msEncrypt.ToArray();
}
}
}
// Return the encrypted bytes from the memory stream.
return encrypted;
}
public static 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 = null;
// Create an Aes object
// with the specified key and IV.
using (Aes aesAlg = Aes.Create())
{
aesAlg.Key = Key;
aesAlg.IV = IV;
aesAlg.Padding = PaddingMode.Zeros;
// Create a decryptor 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;
}
}
【问题讨论】:
-
密码从不加密,它们总是经过哈希处理 - 加盐后,使用强大的加密算法,进行大量迭代(ASP.NET Identity 中为 10K),以确保暴力破解它们将非常昂贵。根据定义,加密允许 decryption
-
至于出了什么问题,您将 8 位字节视为 7 位 ASCII 文本。至少一半的字节将被破坏,任何高于 127 的字节值都将替换为
?。当你加密某些东西时,你会得到一个包含所有可能值的 字节数组,而不仅仅是 7 位字母。这包括像0x00这样不可打印的值。这就是为什么二进制值在需要作为文本传输时转换为 Base64 的原因。如果要显示加密数据,请使用Convert.ToBase64String -
对于生产,不要使用这个代码。对于测试,使用
Encoding.UTF8将用户输入转换为字节,而不是ASCII。要显示加密字节,请使用txtEncrypted.Text = Convert.ToBase64String(encryptedBytes);。解密使用var bytes=Convert.FromBase64String(txtEncrypted.Text); -
您使用 BinaryWriter 和 StreamReader 似乎很可疑。我会期待 BinaryWriter/Reader 或 StreamWriter/Reader。
-
PanagiotisKanavos ...它正在工作,谢谢亲
标签: c# encryption aes