這是我的第一篇文章,如果我對格式進行了錯誤,我非常抱歉。安全XOR加密嘗試
我正試圖編寫一個程序,以安全的方式通過XOR加密各種文件。我知道異或並不是最安全的加密方法,但我想嘗試一下。 所以請看看我的方法,並告訴我,如果它是完全廢話或不:) 密碼是一個字符串,由用戶選擇。 在開始時,我只將該文件與密碼異或,導致密碼的部分被正確猜測時易於解密。
這裏是我的方法:
- TMPFILE =文件XOR(密碼與pw.length.toString結合的散列)//確保密碼元素是按照正確的順序
- TMPFILE = TmpFile XOR(由密碼的每個字節組成的XOR字節)//確保解碼密碼恰好具有正確的字符。
- TMPFILE = TMPFILE XOR initial_password
可否加密文本與自XOR移技術來解密?
感謝您的諮詢! :)
編輯:這裏是代碼:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security;
using System.IO;
using System.Windows;
namespace EncodeEverything
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("FileEncrypter v01 \n \n");
//get password
Console.WriteLine("Enter your Password (encryption key)");
string password = getPassword();
Console.WriteLine("");
while (true)
{
Console.WriteLine("");
Console.WriteLine("-----------------------");
Console.WriteLine("");
//get file to encrypt
Console.WriteLine("File to encrypt/decrypt:");
Console.Write(" ");
string path = Console.ReadLine();
//-------------------------------
//load, encrypt & save file
//-------------------------------
try {
Byte[] tmpBArr = encrypt(File.ReadAllBytes(path), getCustomHash(password));
File.WriteAllBytes(path, encrypt(tmpBArr, password));
Console.WriteLine(" done.");
}
catch(System.Exception e)
{
Console.WriteLine("!! Error while processing. Path correct? !!");
}
}
}
private static string getCustomHash(string word)
{
string output = "";
output += word.Length.ToString();
output += word.GetHashCode();
return output;
}
//encrypt bzw decrypt Byte[]
public static Byte[] encrypt(byte[] s, string key)
{
List<Byte> output = new List<byte>();
Byte[] codeword = Encoding.UTF8.GetBytes(key);
Byte keybyte =(Byte)(codeword[0]^ codeword[0]);
foreach(Byte b in codeword)
{
keybyte = (Byte)(b^keybyte);
}
for (int i = 0; i < s.Length; i++)
{
output.Add((Byte)(s[i]^codeword[i % codeword.Length]^keybyte));
}
return output.ToArray();
}
public static string getPassword()
{
Console.Write(" ");
string pwd = "";
while (true)
{
ConsoleKeyInfo i = Console.ReadKey(true);
if (i.Key == ConsoleKey.Enter)
{
break;
}
else if (i.Key == ConsoleKey.Backspace)
{
if (pwd.Length > 0)
{
pwd= pwd.Remove(pwd.Length - 1);
Console.Write("\b \b");
}
}
else
{
pwd+=(i.KeyChar);
Console.Write("*");
}
}
return pwd;
}
}
}
你可以把一個示例代碼,以便它變得更清楚的問題。 –
您必須進一步指定您的算法!例如,當文件的大小與密碼長度一樣大時,您還沒有提到完成的操作。我假設從頭再次開始(這是使這種不安全的部分)。 XOR本身不一定是不安全的。考慮Vernam Chiffre(也稱爲一次性鍵盤):您使用一個完全隨機的鍵,它與消息的長度和XOR的長度一致。如果您不重複使用經證明是安全的鑰匙! – flammi88
Btw一個簡短的免責聲明:永遠不要爲真實世界的應用程序自己實現加密,這些應用程序必須是安全的......很容易弄錯事情。考慮一次性...如果你沒有正確地擺脫關鍵材料,它可能駐留在主內存中...因爲你不能控制C#中的內存佈局,我認爲這是非常難以確定密鑰的每次出現都已在內存中清零......您可以注意到,退出後您的內存已被釋放。但考慮一下,操作系統在退出後不會清零內存。使用正確的工具可以恢復它! – flammi88