public bool isPackage(string path, keyIterator key) { if (!MainClass.isFile(path)) { if (debug) { Console.WriteLine("{0} is not a file", path); } return(false); } using (FileStream inFile = new FileStream(path, FileMode.Open)) { byte[] check = new byte[64]; byte[] checksum = new byte[64]; for (int i = 0; i < 64; i++) { check[i] = (byte)(inFile.ReadByte() ^ key.getKeyByte()); } for (int i = 0; i < 64; i++) { checksum[i] = (byte)(inFile.ReadByte() ^ key.getKeyByte()); } check = SHA512.Create().ComputeHash(check); key.resetKey(); if (!check.SequenceEqual(checksum)) { if (debug) { Console.WriteLine("{0} failed checksum test", path); } return(false); } return(true); } }
public void encrypt(keyIterator key) { foreach (String path in fullNames) { if (debug) { sw.Start(); } //first, get the file's name String name = Path.GetFileName(path); Console.WriteLine("Encrypting " + path); //we also need the bytes of the name byte[] nameBytes = Encoding.Unicode.GetBytes(name); byte lengthByte = (byte)nameBytes.Length; //Should always be sub 255, most of the time if (debug) { Console.WriteLine("Name Length is: {0}", lengthByte); } //generate a byte for the amount of salt that is to be produced byte saltByte = 0; //Kind of random? It's close enough for this foreach (byte b in nameBytes) { saltByte = (byte)(saltByte ^ b); } if (debug) { Console.WriteLine("Salt Byte is {0}", saltByte); } var salter = new RNGCryptoServiceProvider(); int prefixLength = saltByte % 16; byte[] prefix = new byte[prefixLength]; salter.GetNonZeroBytes(prefix); int suffixLength = ((saltByte - (saltByte % 16)) / 16); byte[] suffix = new byte[suffixLength]; salter.GetNonZeroBytes(suffix); if (debug) { Console.WriteLine("Prefix and Suffix lengths: {0}, {1}", prefixLength, suffixLength); } //Put the salt on the ends of the name List <byte> saltedName = new List <byte>(); for (int i = 0; i < prefixLength; i++) { saltedName.Add(prefix[i]); } saltedName.AddRange(nameBytes); for (int i = 0; i < suffixLength; i++) { saltedName.Add(suffix[i]); } if (debug) { Console.WriteLine("Length of salted name is: " + saltedName.Count); } //Start putting together the new file MD5 nameGen = MD5.Create(); String outName = BitConverter.ToString(nameGen.ComputeHash(nameBytes)).Replace("-", "").ToLower(); //Create the "header" and write it List <byte> headerBytes = new List <byte>(); headerBytes.Add(lengthByte); headerBytes.Add(saltByte); headerBytes.AddRange(saltedName.ToArray()); if (debug) { Console.WriteLine(("Writing to: " + Path.GetDirectoryName(path) + "\\" + outName + "\n")); } using (FileStream outFile = File.Open((Path.GetDirectoryName(path) + "\\" + outName), FileMode.Append, FileAccess.Write)) { foreach (byte b in headerBytes) { outFile.WriteByte((byte)(b ^ key.getKeyByte())); } //Write in the rest of the file, byte by byte using (FileStream inFile = File.Open(path, FileMode.Open, FileAccess.Read)) { FileInfo inFileSize = new FileInfo(path); for (int i = 0; i < inFileSize.Length; i++) { outFile.WriteByte((byte)(inFile.ReadByte() ^ key.getKeyByte())); } } } File.Delete(path); key.resetKey(); if (debug) { sw.Stop(); Console.WriteLine("Elapsed time: {0}\n", sw.Elapsed); sw.Reset(); } } }
public void decrypt(keyIterator key) { foreach (String path in fullNames) { if (debug) { sw.Start(); } Console.WriteLine("Decrypting {0}", path); using (FileStream inFile = File.Open(path, FileMode.Open, FileAccess.Read)) { String outName = ""; List <byte> decBytes = new List <byte>(); //get the name length int nameLength = inFile.ReadByte() ^ key.getKeyByte(); if (debug) { Console.WriteLine("Length of file name is: {0}", nameLength); } //get the name out of the file List <byte> byteNameRaw = new List <byte>(); List <byte> byteName = new List <byte>(); int saltStuff = inFile.ReadByte() ^ key.getKeyByte(); if (debug) { Console.WriteLine("Salt Byte is {0}", saltStuff); } int prefixLength = saltStuff % 16; int suffixLength = (saltStuff - (saltStuff % 16)) / 16; if (debug) { Console.WriteLine("Prefix and Suffix lengths: {0}, {1}", prefixLength, suffixLength); } for (int i = 0; i < nameLength + prefixLength + suffixLength; i++) { byteNameRaw.Add((byte)(inFile.ReadByte() ^ key.getKeyByte())); } //take only the filename out for (int i = 0; i < nameLength; i++) { byteName.Add(byteNameRaw[i + prefixLength]); } outName = Encoding.Unicode.GetString(byteName.ToArray()); if (debug) { Console.WriteLine("Extracted name is: {0}", outName); } //now get the rest of the file if (debug) { Console.WriteLine(("Writing to: " + Path.GetDirectoryName(path) + "\\" + outName + "\n")); } using (FileStream outFile = File.Open((Path.GetDirectoryName(path) + "\\" + outName), FileMode.Append)) { FileInfo inFileSize = new FileInfo(path); for (int i = 2 + prefixLength + nameLength + suffixLength; i < inFileSize.Length; i++) //fileToDecrypt.Length; i++) { outFile.WriteByte((byte)(inFile.ReadByte() ^ key.getKeyByte())); } } } File.Delete(path); key.resetKey(); if (debug) { sw.Stop(); Console.WriteLine("Elapsed time: {0}\n", sw.Elapsed); sw.Reset(); } } }
public void listPackage(keyIterator key, string path) { root = new DirectoryInfo(Path.GetDirectoryName(root.FullName)); Int64 fileIndex = 0; if (debug) { sw.Start(); } using (FileStream inFile = File.Open(path, FileMode.Open, FileAccess.Read)) { Int64 packSize = inFile.Length; //read in the checksum and compare byte[] check = new byte[64]; byte[] checksum = new byte[64]; for (int i = 0; i < 64; i++) { check[i] = (byte)(inFile.ReadByte() ^ key.getKeyByte()); } for (int i = 0; i < 64; i++) { checksum[i] = (byte)(inFile.ReadByte() ^ key.getKeyByte()); } check = SHA512.Create().ComputeHash(check); if (!check.SequenceEqual(checksum)) { Console.WriteLine("File {0} is not an encrypted file, or an incorrect key has been selected.", path); return; } else { Console.WriteLine("Listing {0}", path); } fileIndex += 128; while (fileIndex < packSize) { string outName = ""; List <byte> decBytes = new List <byte>(); //get the name length /* * Uint16 namelength | byte saltByteP | byte saltByteS | string saltedName (up to 260 chars + 512 salt) | Uint32 fileSize | byte[] file | <repeats> * 2 | 1 | 1 | up to 772 | 4 | up to 4GB | = 780 bytes */ //int nameLength = (int)(inFile.ReadByte() ^ key.getKeyByte()); byte[] nameLengthBytes = new byte[2]; for (int i = 0; i < 2; i++) { nameLengthBytes[i] = (byte)(inFile.ReadByte() ^ key.getKeyByte()); } fileIndex += 2; UInt16 nameLength = BitConverter.ToUInt16(nameLengthBytes, 0); if (debug) { Console.WriteLine("Length of file name is: {0}", nameLength); } //get the name out of the file List <byte> byteNameRaw = new List <byte>(); List <byte> byteName = new List <byte>(); int prefixLength = (inFile.ReadByte() ^ key.getKeyByte()); int suffixLength = (inFile.ReadByte() ^ key.getKeyByte()); fileIndex += 2; if (debug) { Console.WriteLine("Prefix and Suffix lengths: {0}, {1}", prefixLength, suffixLength); } for (int i = 0; i < nameLength + prefixLength + suffixLength; i++) { byteNameRaw.Add((byte)(inFile.ReadByte() ^ key.getKeyByte())); } fileIndex += nameLength + prefixLength + suffixLength; //take only the filename out for (int i = 0; i < nameLength; i++) { byteName.Add(byteNameRaw[i + prefixLength]); } outName = Encoding.Unicode.GetString(byteName.ToArray()); byte[] fileSizeBytes = new byte[4]; for (int i = 0; i < 4; i++) { fileSizeBytes[i] = (byte)(inFile.ReadByte() ^ key.getKeyByte()); } fileIndex += 4; UInt32 inFileSize = BitConverter.ToUInt32(fileSizeBytes, 0); Console.WriteLine("{0}: {1}", outName, MainClass.BytesToString(inFileSize)); //Attempt to create directory, just to be sure //Directory.CreateDirectory(Path.GetDirectoryName(root + "\\" + outName)); //now get the rest of the file inFile.Position += inFileSize; key.incrementBy(inFileSize); fileIndex += inFileSize; } } //File.Delete(path); if (debug) { sw.Stop(); Console.WriteLine("Elapsed time: {0}\n", sw.Elapsed); sw.Reset(); } key.resetKey(); }
public void decrypt(keyIterator key) { /* * Add in a check to make sure this is actually decrypting a real encrypted file * */ foreach (string path in fullNames) //should only ever be one, hopefully { Int64 fileIndex = 0; if (debug) { sw.Start(); } using (FileStream inFile = File.Open(path, FileMode.Open, FileAccess.Read)) { Int64 packSize = inFile.Length; //read in the checksum and compare byte[] check = new byte[64]; byte[] checksum = new byte[64]; for (int i = 0; i < 64; i++) { check[i] = (byte)(inFile.ReadByte() ^ key.getKeyByte()); } for (int i = 0; i < 64; i++) { checksum[i] = (byte)(inFile.ReadByte() ^ key.getKeyByte()); } check = SHA512.Create().ComputeHash(check); if (!check.SequenceEqual(checksum)) { Console.WriteLine("File {0} is not an encrypted file, or an incorrect key has been selected.", path); continue; } else { Console.WriteLine("Decrypting {0}", path); } fileIndex += 128; while (fileIndex < packSize) { string outName = ""; List <byte> decBytes = new List <byte>(); //get the name length /* * Uint16 namelength | byte saltByteP | byte saltByteS | string saltedName (up to 260 chars + 512 salt) | Uint32 fileSize | byte[] file | <repeats> * 2 | 1 | 1 | up to 772 | 4 | up to 4GB | = 780 bytes */ //int nameLength = (int)(inFile.ReadByte() ^ key.getKeyByte()); byte[] nameLengthBytes = new byte[2]; for (int i = 0; i < 2; i++) { nameLengthBytes[i] = (byte)(inFile.ReadByte() ^ key.getKeyByte()); } fileIndex += 2; UInt16 nameLength = BitConverter.ToUInt16(nameLengthBytes, 0); if (debug) { Console.WriteLine("Length of file name is: {0}", nameLength); } //get the name out of the file List <byte> byteNameRaw = new List <byte>(); List <byte> byteName = new List <byte>(); int prefixLength = (inFile.ReadByte() ^ key.getKeyByte()); int suffixLength = (inFile.ReadByte() ^ key.getKeyByte()); fileIndex += 2; if (debug) { Console.WriteLine("Prefix and Suffix lengths: {0}, {1}", prefixLength, suffixLength); } for (int i = 0; i < nameLength + prefixLength + suffixLength; i++) { byteNameRaw.Add((byte)(inFile.ReadByte() ^ key.getKeyByte())); } fileIndex += nameLength + prefixLength + suffixLength; //take only the filename out for (int i = 0; i < nameLength; i++) { byteName.Add(byteNameRaw[i + prefixLength]); } outName = Encoding.Unicode.GetString(byteName.ToArray()); Console.WriteLine("Extracting {0}", outName); byte[] fileSizeBytes = new byte[4]; for (int i = 0; i < 4; i++) { fileSizeBytes[i] = (byte)(inFile.ReadByte() ^ key.getKeyByte()); } fileIndex += 4; UInt32 inFileSize = BitConverter.ToUInt32(fileSizeBytes, 0); //Attempt to create directory, just to be sure Directory.CreateDirectory(Path.GetDirectoryName(root + "\\" + outName)); //now get the rest of the file if (debug) { Console.WriteLine(("Writing to: " + Path.GetDirectoryName(path) + "\\" + outName + "\n")); } using (FileStream outFile = File.Open((Path.GetDirectoryName(path) + "\\" + outName), FileMode.Append)) { //FileInfo inFileSize = new FileInfo(path); for (int i = 0; i < inFileSize; i++) //fileToDecrypt.Length; i++) { outFile.WriteByte((byte)(inFile.ReadByte() ^ key.getKeyByte())); } } fileIndex += (int)inFileSize; if (debug) { Console.WriteLine("File index is {0}", fileIndex); Console.WriteLine("Length of extracted Data is {0}", 4 + byteNameRaw.Count + 4 + inFileSize); } } } File.Delete(path); if (debug) { sw.Stop(); Console.WriteLine("Elapsed time: {0}\n", sw.Elapsed); sw.Reset(); } key.resetKey(); } }