c# - Secure XOR encryption attempt -
this first post , sorry if made errors format.
i trying write program encrypt kinds of file via xor in secure way. know xor isn't secure encryption method wanted give try. please have on methode , tell me if complete bullshit or not :) password string, chosen user. in beginning xored file password, leading easy decryption if parts of password guessed correctly.
here procedure:
- tmpfile = file xor (hash of password combined pw.length.tostring) //to make sure password elements in right order
- tmpfile = tmpfile xor (xor byte composed each byte of password)//ensure password decode has right chars.
- tmpfile= tmpfile xor initial_password
could encrypted text decrypted self-xor-shifting technique?
thanks advice! :)
edit: here code:
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 password (encryption key)"); string password = getpassword(); console.writeline(""); while (true) { console.writeline(""); console.writeline("-----------------------"); console.writeline(""); //get file encrypt console.writeline("file 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); } (int = 0; < 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 = 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; } } }
you can ignore answer if you're trying encrypt files learning exercise in cryptography, if you're looking real-world solution securing file data, read on.
i'd recommend use file encryption built .net framework sort of thing if you're looking real-world solution keeping file data secure.
from microsoft @ https://msdn.microsoft.com/en-us/library/system.io.file.encrypt(v=vs.110).aspx
using system; using system.io; using system.security.accesscontrol; namespace filesystemexample { class fileexample { public static void main() { try { string filename = "test.xml"; console.writeline("encrypt " + filename); // encrypt file. addencryption(filename); console.writeline("decrypt " + filename); // decrypt file. removeencryption(filename); console.writeline("done"); } catch (exception e) { console.writeline(e); } console.readline(); } // encrypt file. public static void addencryption(string filename) { file.encrypt(filename); } // decrypt file. public static void removeencryption(string filename) { file.decrypt(filename); } } }
it hard sure need, because other things may need taken consideration such whether need pass file between different clients/servers etc, how data you're encrypting in each file.
again, if you're looking real-world cryptography using c#, can't stress enough should looking built-in .net encryption rather trying roll own- if don't have formal training in subject matter. recommend pore through microsoft documentation on .net framework encryption if you're interested in securing data in production:
https://msdn.microsoft.com/en-us/library/0ss79b2x(v=vs.110).aspx
here nice walkthrough creating example file encrypting windows form application:
https://msdn.microsoft.com/en-us/library/bb397867(v=vs.110).aspx
Comments
Post a Comment