问题描述
我正在尝试加密/解密密码,加密密码正常工作 但是解密密码返回了错误的结果.....
示例:
密码输入: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,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.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[] 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;
}
}
解决方法
显示加密的字节
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 = Convert.ToBase64String(Encrypted_Pass_byte);
}
显示解密的字符串
private void btn_decrypt_Click(object sender,EventArgs e)
{
var Encrypted_Password = Convert.FromBase64String(txt_Password_Encrypted.Text);
txt_Password_Decrpted.Text = AesExample.DecryptStringFromBytes_Aes(Encrypted_Password,IV);
}
并在加密和解密中使用StreamWriter / Reader
感谢谁帮了我..