我正在嘗試制作一個關于密碼管理器的控制臺應用程式。它具有加密和解密輸入的能力,以增加安全性。但是我在解密資料時遇到錯誤。
更新代碼:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Security.Cryptography;
using System.Text.RegularExpressions;
namespace ProjectNumeroUno
{
internal class Program
{
static void Main(string[] args)
{
string[] vs = { };
Console.Write("Welcome to your personal password database.\nData will be protected, and cannot be accessed directly.\nVery personalized and secure!\n\n1.New Data entry\n2.Display and search\n3.exit\nEnter your option: ");
string option = Console.ReadLine();
string path = @"D:\Pass.txt";
while (option != "3")
{
if (option == "1")
{
Console.Write("Enter website: ");
string url = Console.ReadLine();
Console.Write("Enter username: ");
string usr = Console.ReadLine();
Console.Write("Enter password: ");
string pwd = Console.ReadLine();
string mmm = "URL: " url " Username: " usr " Password: " pwd;
using (Aes Entry = Aes.Create())
{
Entry.Key = Encoding.Default.GetBytes(new string('j', 16));
Entry.IV = Encoding.Default.GetBytes(new string('j', 16));
byte[] encrypted = EncryptionOfContent(mmm, Entry.Key, Entry.IV);
using (StreamWriter sw = File.AppendText(path))
{
sw.WriteLine(Encoding.Default.GetString(encrypted));
}
}
}
else if (option == "2")
{
try
{
string[] lines = Regex.Split(File.ReadAllText(path), "\r\n|\r|\n");
byte[][] lineData = lines.Select(l => Encoding.Default.GetBytes(l)).Where(l => l.Length > 0).ToArray();
foreach (var data in lineData)
{
using (Aes entry = Aes.Create())
{
entry.Key = Encoding.Default.GetBytes(new string('j', 16));
entry.IV = Encoding.Default.GetBytes(new string('j', 16));
string decrypted = DecryptionOfContent(data, entry.Key, entry.IV);
vs.Append(decrypted);
Console.WriteLine(decrypted);
}
}
}
catch (IOException e)
{
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
}
}
Console.Write("\n1.New Data entry\n2.Display and search\n3.exit\nEnter your option: ");
option = Console.ReadLine();
}
}
static byte[] EncryptionOfContent(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;
// 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 (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
{
//Write all data to the stream.
swEncrypt.Write(plainText);
}
encrypted = msEncrypt.ToArray();
}
}
}
// Return the encrypted bytes from the memory stream.
return encrypted;
}
static string DecryptionOfContent(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;
// 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;
}
}
}
選項 1 似乎作業得很好,在顯示資料時,它在從資料陣列中洗掉尾隨的“\n”或“10”和“13”位元組后作業。但是,如果我嘗試另一個輸入,第一個資料和第二個資料之間會有一組“13”和“10”,這會增加位元組大小,并且在運行時回傳錯誤。
錯誤:

我仍然想要不同輸入之間的“\n”,以便區分輸入,而不是混淆列印。但我該怎么做呢?如何增加位元組大小,使其不會導致例外錯誤?我試過 aesAlg.Blocksize = 2; 這也不起作用。
編輯:“如果加密輸出不是單行會怎樣”問題

這就是呃,我有 4 個輸入,但是在加密之后,有一個額外的行,就像加密里面有“\n”一樣。它現在拋出另一個例外。雖然這個錯誤好像每5-10次出現一次,但有時加密本身會失控,有時它在加密后開始寫入漢字。:\
uj5u.com熱心網友回復:
您需要將加密文本拆分為單獨的陣列,而沒有行結束字符,然后解密每一行:
string[] lines = Regex.Split(File.ReadAllText(path), "\r\n|\r|\n");
byte[][] lineData = lines.Select(l => Encoding.Default.GetBytes(l)).Where(l => l.Length > 0).ToArray();
foreach (var data in lineData)
{
using Aes entry = Aes.Create();
entry.Key = Encoding.Default.GetBytes(new string('j', 16));
entry.IV = Encoding.Default.GetBytes(new string('j', 16));
string decrypted = DecryptionOfContent(data, entry.Key, entry.IV);
Console.WriteLine(decrypted);
}
uj5u.com熱心網友回復:
您可以考慮更改以下幾點:
- 您想使用換行符來分隔不同的輸入,但您的代碼并未考慮加密資料本身可能包含換行符。
- 由于您使用 寫入檔案
WriteLine,因此理想情況下應該使用讀取檔案,ReadLine這樣您就不需要自己處理換行符。
對于 #1,您可以將 Base64 編碼的字串寫入您的檔案:
byte[] encrypted = EncryptionOfContent(mmm, Entry.Key, Entry.IV);
...
sw.WriteLine(Convert.ToBase64String(encrypted));
對于#2,您可以更改讀取檔案的方式:
var inputs = File.ReadAllLines(path);
foreach (var input in inputs)
{
var data = Convert.FromBase64String(input);
...
string decrypted = DecryptionOfContent(data, Entry.Key, Entry.IV);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/342848.html
上一篇:帶有DataTemplate的ContentControl不顯示任何內容(WPFMVVM)
下一篇:您如何找到PC上的GPU數量?
