这是我的第一篇文章,如果我对格式进行了错误,我非常抱歉。安全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