/// <summary> /// Saves a database's data into the stream. If you create a new database instance, LoadDatabase() must be called to load the data. /// </summary> public void SaveDatabase() { EmptyTargetStream(); using (var intermediateStream = new MemoryStream()) { switch (SerializationSystem) { case DatabaseSerializationSystem.MsgPack: Serializer.Pack(intermediateStream, Tables); break; case DatabaseSerializationSystem.Json: var serializedObj = JsonConvert.SerializeObject(Tables); using (var intermediateStreamWriter = new StreamWriter(intermediateStream)) { intermediateStreamWriter.Write(serializedObj); } break; } //Encrypt stream var databaseData = intermediateStream.ToArray(); var encryptedData = PowerAES.Encrypt(databaseData.GetString(), EncryptionKey).GetBytes(); _targetWriter.Write(encryptedData); _targetWriter.Flush(); } }
public static void CreateNewVolume(string location, string label, string password, string version) { string fnwoext = Path.GetFileNameWithoutExtension(location); //filenamewithout extension string volN = Path.GetDirectoryName(location) + Path.DirectorySeparatorChar + fnwoext + ".vault" + Path.DirectorySeparatorChar + fnwoext + ".FireCrypt"; string vaultL = Path.GetDirectoryName(volN); Directory.CreateDirectory(Path.GetDirectoryName(volN)); string vid = Guid.NewGuid().ToString(); Dictionary <string, string> volMeta = new Dictionary <string, string>(); volMeta["UID"] = vid; volMeta["Label"] = label; volMeta["VolumeVersion"] = version; string ed = Path.GetTempPath() + Path.DirectorySeparatorChar + Guid.NewGuid(); Directory.CreateDirectory(ed); switch (version) { case "1.0": string unlLoc = ed; string DecVolumeLocation = unlLoc + ".dec"; ZipFile.CreateFromDirectory(unlLoc, DecVolumeLocation); string dVolume = File.ReadAllBytes(DecVolumeLocation).GetString(); File.WriteAllBytes(volN, PowerAES.Encrypt(dVolume, password).GetBytes()); break; default: throw new InvalidOperationException("Cannot perform operation on unsupported volume version!"); } string metaS = new JavaScriptSerializer().Serialize(volMeta); File.WriteAllText(vaultL + Path.DirectorySeparatorChar + "vault.metadata", metaS); }
public static void CreateNewVolume(string location, string label, string password) { string fnwoext = Path.GetFileNameWithoutExtension(location); //filenamewithout extension string volN = Path.GetDirectoryName(location) + "\\" + fnwoext + ".vault\\" + fnwoext + ".firecrypt"; string vaultL = Path.GetDirectoryName(volN); Directory.CreateDirectory(Path.GetDirectoryName(volN)); string vid = Guid.NewGuid().ToString(); Dictionary <string, string> volMeta = new Dictionary <string, string>(); volMeta["UID"] = vid; volMeta["Label"] = label; string ed = Path.GetTempPath() + "\\" + Guid.NewGuid(); Directory.CreateDirectory(ed); string unlLoc = ed; string DecVolumeLocation = unlLoc + ".dec"; ZipFile.CreateFromDirectory(unlLoc, DecVolumeLocation); string dVolume = File.ReadAllBytes(DecVolumeLocation).GetString(); File.WriteAllBytes(volN, PowerAES.Encrypt(dVolume, password).GetBytes()); string metaS = new JavaScriptSerializer().Serialize(volMeta); File.WriteAllText(vaultL + "\\vault.metadata", metaS); }
public void UnlockVolume(string key) { string eVolume = File.ReadAllBytes(VolumeLocation).GetString(); string unlockName = UnlockLocation + UID; string DecVolumeLocation = unlockName + ".dec"; if (!Directory.Exists(UnlockLocation)) { Directory.CreateDirectory(UnlockLocation); } if (Directory.Exists(unlockName)) { Directory.Delete(unlockName, true); } File.WriteAllBytes(DecVolumeLocation, PowerAES.Decrypt(eVolume, key).GetBytes()); ZipFile.ExtractToDirectory(DecVolumeLocation, unlockName); FileWiper fw = new FileWiper(); fw.PassInfoEvent += (e) => {}; fw.SectorInfoEvent += (e) => {}; fw.WipeDoneEvent += (e) => {}; fw.WipeErrorEvent += (e) => {}; fw.WipeFile(DecVolumeLocation, 1); _unlocked = true; _unlockPath = unlockName; }
void CheckBox1CheckedChanged(object sender, EventArgs e) { bool rk = checkBox1.Checked; if (rk) { foreach (Control c in panel1.Controls) { c.Enabled = true; } textBox2.Enabled = false; textBox3.Enabled = false; textBox3.Text = textBox2.Text = ""; textBox4.Enabled = true; textBox4.Text = PowerAES.GenerateRandomString(1024); ValidateNextButton(); } else { textBox2.Enabled = true; textBox3.Enabled = true; foreach (Control c in panel1.Controls) { c.Enabled = false; } textBox4.Text = ""; checkBox1.Enabled = true; ValidateNextButton(); } }
public static string EncryptIntData(int[] intData, string password) { string b64 = GetBase64FromIntData(intData); string key = PowerAES.SHA512Hash(password); string aes = PowerAES.Encrypt(b64, key); string b64data = Base64.Base64Encode(aes); return(b64data); }
public static int[] DecryptIntData(string encryptedIntData, string password) { string dB = Base64.Base64Decode(encryptedIntData); string key = PowerAES.SHA512Hash(password); string decryptedIntData = PowerAES.Decrypt(dB, key); int[] nIntdata = GetIntDataFromBase64(decryptedIntData); return(nIntdata); }
private void Lock_1_0(string key) { string unlockName = UnlockLocation+UID; string DecVolumeLocation = unlockName+".dec"; if (File.Exists(DecVolumeLocation)) { File.Delete(DecVolumeLocation); } ZipFile.CreateFromDirectory(unlockName, DecVolumeLocation); fw.RecursivelyWipeDirectory(unlockName); string dVolume = File.ReadAllBytes(DecVolumeLocation).GetString(); fw.WipeFile(DecVolumeLocation, 1); File.WriteAllBytes(VolumeLocation, PowerAES.Encrypt(dVolume, key).GetBytes()); _unlocked = false; _unlockPath = null; DeleteDirectory(unlockName); }
public void Unlock_1_0(string key) { string unlockName = UnlockLocation+UID; string DecVolumeLocation = unlockName+".dec"; if (!Directory.Exists(UnlockLocation)) { Directory.CreateDirectory(UnlockLocation); } if (Directory.Exists(unlockName)) { fw.RecursivelyWipeDirectory(unlockName); Directory.Delete(unlockName,true); } string eVolume = File.ReadAllBytes(VolumeLocation).GetString(); File.WriteAllBytes(DecVolumeLocation, PowerAES.Decrypt(eVolume,key).GetBytes()); ZipFile.ExtractToDirectory(DecVolumeLocation, unlockName); fw.WipeFile(DecVolumeLocation, 1); _unlocked = true; _unlockPath = unlockName; }
public void Unlock_2_0(string key) { string unlockName = UnlockLocation + UID; string DecVolumeLocation = unlockName + ".dec"; if (!Directory.Exists(UnlockLocation)) { Directory.CreateDirectory(UnlockLocation); } if (Directory.Exists(unlockName)) { fw.RecursivelyWipeDirectory(unlockName); Directory.Delete(unlockName, true); } int bufSize = SelectBufferSize(); byte[] encryptedFileBuffer = new byte[bufSize]; //128 Mebibytes, 134.2 Megabytes byte[] decryptionBuffer1; //128 Mebibytes, 134.2 Megabytes //Buffered-read and decrypt and write to the output file using (var encryptedFileStream = File.Open(VolumeLocation, FileMode.Open, FileAccess.ReadWrite)) { using (var rawFileStream = File.Open(DecVolumeLocation, FileMode.Create, FileAccess.ReadWrite)) { using (var encryptedFileReader = new BinaryReader(encryptedFileStream)) { using (var rawFileWriter = new BinaryWriter(rawFileStream)) { int bytesRead; while ((bytesRead = encryptedFileReader.Read(encryptedFileBuffer, 0, bufSize)) > 0) { //Slice buffer decryptionBuffer1 = PowerAES.Decrypt(encryptedFileBuffer.Take(bytesRead).ToArray().GetString(), key).GetBytes(); rawFileWriter.Write(decryptionBuffer1, 0, decryptionBuffer1.Length); } } } } } ZipFile.ExtractToDirectory(DecVolumeLocation, unlockName); fw.WipeFile(DecVolumeLocation, 1); _unlocked = true; _unlockPath = unlockName; }
private void Lock_2_0(string key) { string unlockName = UnlockLocation + UID; string DecVolumeLocation = unlockName + ".dec"; if (File.Exists(DecVolumeLocation)) { File.Delete(DecVolumeLocation); } ZipFile.CreateFromDirectory(unlockName, DecVolumeLocation); fw.RecursivelyWipeDirectory(unlockName); int bufSize = SelectBufferSize(); byte[] rawFileBuffer = new byte[bufSize]; //128 Mebibytes, 134.2 Megabytes byte[] encryptionBuffer1; //128 Mebibytes, 134.2 Megabytes //Buffered-read and encrypt and write to the output file using (var rawFileStream = File.Open(DecVolumeLocation, FileMode.Open, FileAccess.ReadWrite)) { using (var encryptedFileStream = File.Open(VolumeLocation, FileMode.Create, FileAccess.ReadWrite)) { using (var rawFileReader = new BinaryReader(rawFileStream)) { using (var encryptedFileWriter = new BinaryWriter(encryptedFileStream)) { int bytesRead; while ((bytesRead = rawFileReader.Read(rawFileBuffer, 0, bufSize)) > 0) { //Slice buffer to only data that was read encryptionBuffer1 = PowerAES.Encrypt(rawFileBuffer.Take(bytesRead).ToArray().GetString(), key).GetBytes(); encryptedFileWriter.Write(encryptionBuffer1, 0, encryptionBuffer1.Length); encryptionBuffer1 = null; } } } } } fw.WipeFile(DecVolumeLocation, 1); _unlocked = false; _unlockPath = null; DeleteDirectory(unlockName); }
public void LockVolume(string key) { string unlockName = UnlockLocation + UID; string DecVolumeLocation = unlockName + ".dec"; if (File.Exists(DecVolumeLocation)) { File.Delete(DecVolumeLocation); } ZipFile.CreateFromDirectory(unlockName, DecVolumeLocation); Directory.Delete(unlockName, true); string dVolume = File.ReadAllBytes(DecVolumeLocation).GetString(); FileWiper fw = new FileWiper(); fw.PassInfoEvent += (e) => {}; fw.SectorInfoEvent += (e) => {}; fw.WipeDoneEvent += (e) => {}; fw.WipeErrorEvent += (e) => {}; fw.WipeFile(DecVolumeLocation, 1); File.WriteAllBytes(VolumeLocation, PowerAES.Encrypt(dVolume, key).GetBytes()); _unlocked = false; _unlockPath = null; }
/// <summary> /// Loads any existing data into the database. /// </summary> public void LoadDatabase() { _targetStream.Position = 0; var encryptedData = _targetReader.ReadBytes((int)_targetReader.BaseStream.Length); var decryptedData = PowerAES.Decrypt(encryptedData.GetString(), EncryptionKey).GetBytes(); using (var decryptedStream = new MemoryStream(decryptedData)) { switch (SerializationSystem) { case DatabaseSerializationSystem.MsgPack: Tables = Serializer.Unpack(decryptedStream) as KDBDataStructure; break; case DatabaseSerializationSystem.Json: using (var decryptedStreamReader = new StreamReader(decryptedStream)) { var jsonString = decryptedStreamReader.ReadToEnd(); Tables = JsonConvert.DeserializeObject <KDBDataStructure>(jsonString); } break; } } }
public static void Main(string[] args) { const int rsaKeySize = 728; //Smaller key sizes are easier to generate while testing var prsa = new PowerRSA(rsaKeySize, PowerRSA.PHashAlgorithm.SHA256); const string p = "this is n"; var c = prsa.EncryptStringWithPublicKey(p); Console.WriteLine(c); var d = prsa.DecryptStringWithPrivateKey(c); Console.WriteLine(d); var x = prsa.PublicKey; Console.WriteLine("RSAProvider Data: " + prsa.PrivateKey); Console.WriteLine("Exporting Private key to PKCS format:"); var priPemKey = RSAExtensions.ConvertPrivateKeyToPKCS(prsa); Console.WriteLine(priPemKey); Console.Write("PKCS Signing..."); const string signData = "Hello, World!"; var signature = RSAExtensions.SignWithPKCSPrivateKey(signData, prsa); Console.WriteLine(signature); Console.Write("Verifying..."); var verification = RSAExtensions.VerifyWithPKCSPublicKey(signData, signature, prsa); Console.WriteLine(verification); prsa.Dispose(); var pub = new PowerRSA(x, rsaKeySize, PowerRSA.PHashAlgorithm.SHA256); var e = pub.EncryptStringWithPublicKey(p); var d2 = prsa.DecryptStringWithPrivateKey(e); Console.WriteLine(d2); pub.Dispose(); Console.WriteLine(e); const string k = "1234"; var a1 = PowerAES.Encrypt(p, k); Console.WriteLine(a1); var d1 = PowerAES.Decrypt(a1, k); Console.WriteLine(d1); Console.WriteLine(PowerAES.SHA512Hash(p)); Console.WriteLine("Testing AES encryption on strings..."); var plaintextString = "Hi i like pie"; var password = "******"; var encryptedString = PowerAES.Encrypt(plaintextString, password); var decryptedString = PowerAES.Decrypt(encryptedString, password); Debug.Assert(decryptedString == plaintextString); Console.WriteLine("Testing AES encryption directly on bytes..."); var aesProvider = new AESProvider(); var salt = aesProvider.GenerateRandomBytes(24); var key = aesProvider.DeriveKeyFromPassphrase("monkey", salt); var iv = aesProvider.GenerateRandomBytes(16); //128-bit IV var plaintextBytes = Encoding.UTF8.GetBytes("Hi I am a monkey"); var encryptedBytes = aesProvider.EncryptBytes(plaintextBytes, key, iv); var decryptedBytes = aesProvider.DecryptBytes(iv, salt, encryptedBytes, key); Debug.Assert(decryptedBytes.SequenceEqual(plaintextBytes)); Console.WriteLine("Hash Test"); var hash = HashUtils.SHA512(k); Console.WriteLine(hash); Console.WriteLine("Demo completed"); Console.ReadKey(); }
public void OldEncryptSFX(string fileName, string message, string key) { var cube = new NBytzCube.NBCube(); //Dummy to import assembly AssemblyDef cubeDll = AssemblyDef.Load("NBytzCube.dll"); //Load powercrypt ModuleDef nbCubeMod = cubeDll.Modules[0]; nbCubeMod.Kind = ModuleKind.Console; //convert to EXE //AssemblyDef dnlibDll = AssemblyDef.Load("dnlib.dll"); //ModuleDef dnlibModule = dnlibDll.Modules[0]; Importer importer = new Importer(nbCubeMod); // Add the startup type. It derives from System.Object. TypeDef startUpType = new TypeDefUser(namespaceName, "Startup", nbCubeMod.CorLibTypes.Object.TypeDefOrRef); startUpType.Attributes = TypeAttributes.NotPublic | TypeAttributes.AutoLayout | TypeAttributes.Class | TypeAttributes.AnsiClass; // Add the type to the module nbCubeMod.Types.Add(startUpType); // Create the entry point method MethodDef entryPoint = new MethodDefUser("Main", MethodSig.CreateStatic(nbCubeMod.CorLibTypes.Int32, new SZArraySig(nbCubeMod.CorLibTypes.String))); entryPoint.Attributes = MethodAttributes.Private | MethodAttributes.Static | MethodAttributes.HideBySig | MethodAttributes.ReuseSlot; entryPoint.ImplAttributes = MethodImplAttributes.IL | MethodImplAttributes.Managed; // Name the 1st argument (argument 0 is the return type) entryPoint.ParamDefs.Add(new ParamDefUser("args", 1)); // Add the method to the startup type startUpType.Methods.Add(entryPoint); // Set module entry point nbCubeMod.EntryPoint = entryPoint; #region TypeRefs // Create a TypeRef to System.Console TypeRef consoleRef = new TypeRefUser(nbCubeMod, "System", "Console", nbCubeMod.CorLibTypes.AssemblyRef); // Create a method ref to 'System.Void System.Console::WriteLine(System.String)' MemberRef consoleWrite1 = new MemberRefUser(nbCubeMod, "WriteLine", MethodSig.CreateStatic(nbCubeMod.CorLibTypes.Void, nbCubeMod.CorLibTypes.String), consoleRef); MemberRef consoleReadLine1 = new MemberRefUser(nbCubeMod, "ReadLine", MethodSig.CreateStatic(nbCubeMod.CorLibTypes.String), consoleRef); AssemblyRef powerAESLibRef = cubeDll.ToAssemblyRef(); TypeRef powerAESRef = new TypeRefUser(nbCubeMod, "OmniBean.PowerCrypt4", "PowerAES", powerAESLibRef); ITypeDefOrRef byteArrayRef = importer.Import(typeof(System.Byte[])); MemberRef decryptRef = new MemberRefUser(nbCubeMod, "Decrypt", MethodSig.CreateStatic(nbCubeMod.CorLibTypes.String, nbCubeMod.CorLibTypes.String, nbCubeMod.CorLibTypes.String) , powerAESRef); TypeRef byteConverterRef = new TypeRefUser(nbCubeMod, "OmniBean.PowerCrypt4.Utilities", "ByteConverter", powerAESLibRef); MemberRef getBytesRef = new MemberRefUser(nbCubeMod, "GetBytes", MethodSig.CreateStatic(byteArrayRef.ToTypeSig(), nbCubeMod.CorLibTypes.String) , byteConverterRef); TypeRef nbCubeRef = new TypeRefUser(nbCubeMod, "NBytzCube", "NBCube", powerAESLibRef); MemberRef launchAsmRef = new MemberRefUser(nbCubeMod, "LaunchAssembly", MethodSig.CreateStatic(nbCubeMod.CorLibTypes.Void, byteArrayRef.ToTypeSig()) , nbCubeRef); TypeRef fileRef = new TypeRefUser(nbCubeMod, "System.IO", "File", nbCubeMod.CorLibTypes.AssemblyRef); MemberRef writeBytesRef = new MemberRefUser(nbCubeMod, "WriteAllBytes", MethodSig.CreateStatic(nbCubeMod.CorLibTypes.Void, nbCubeMod.CorLibTypes.String, byteArrayRef.ToTypeSig()), fileRef); #endregion // Add a CIL method body to the entry point method CilBody epBody = new CilBody(); entryPoint.Body = epBody; epBody.Instructions.Add(OpCodes.Ldstr.ToInstruction("NetBytz Encrypted SFX - (c) 2016 0xFireball\nEnter key: ")); epBody.Instructions.Add(OpCodes.Call.ToInstruction(consoleWrite1)); epBody.Instructions.Add(OpCodes.Ldstr.ToInstruction(PowerAES.Encrypt(message, key))); //push encrypted text epBody.Instructions.Add(OpCodes.Call.ToInstruction(consoleReadLine1)); //push key from user epBody.Instructions.Add(OpCodes.Call.ToInstruction(decryptRef)); //decrypt epBody.Instructions.Add(OpCodes.Call.ToInstruction(getBytesRef)); //getbytes epBody.Instructions.Add(OpCodes.Call.ToInstruction(launchAsmRef)); //Launch assembly from bytes (managed code) epBody.Instructions.Add(OpCodes.Ldc_I4_0.ToInstruction()); //push 0 epBody.Instructions.Add(OpCodes.Ret.ToInstruction()); //Return/End // Save the assembly to a file on disk nbCubeMod.Write(fileName); }
public static void CreateNewVolume(string location, string label, string password, string version) { string fnwoext = Path.GetFileNameWithoutExtension(location); //filenamewithout extension string volN = Path.GetDirectoryName(location)+"\\"+fnwoext+".vault\\"+fnwoext+".FireCrypt"; string vaultL = Path.GetDirectoryName(volN); Directory.CreateDirectory(Path.GetDirectoryName(volN)); string vid = Guid.NewGuid().ToString(); Dictionary<string,string> volMeta = new Dictionary<string,string>(); volMeta["UID"] = vid; volMeta["Label"] = label; volMeta["VolumeVersion"]=version; string ed = Path.GetTempPath()+"\\"+Guid.NewGuid(); Directory.CreateDirectory(ed); switch (version) { case "1.0": string unlLoc = ed; string DecVolumeLocation = unlLoc+".dec"; ZipFile.CreateFromDirectory(unlLoc, DecVolumeLocation); string dVolume = File.ReadAllBytes(DecVolumeLocation).GetString(); File.WriteAllBytes(volN, PowerAES.Encrypt(dVolume, password).GetBytes()); break; case "2.0": File.WriteAllText(ed + Path.DirectorySeparatorChar + ".fcx2", "[FireCryptVolume]"); string unlLoc2 = ed; string DecVolumeLocation2 = unlLoc2 + ".dec"; if (File.Exists(DecVolumeLocation2)) { File.Delete(DecVolumeLocation2); } ZipFile.CreateFromDirectory(unlLoc2, DecVolumeLocation2); int bufSize = SelectBufferSize(); byte[] rawFileBuffer = new byte[bufSize]; byte[] encryptionBuffer1; using (var rawFileStream = File.Open(DecVolumeLocation2, FileMode.Open, FileAccess.ReadWrite)) { using (var encryptedFileStream = File.Open(volN, FileMode.Create, FileAccess.ReadWrite)) { using (var rawFileReader = new BinaryReader(rawFileStream)) { using (var encryptedFileWriter = new BinaryWriter(encryptedFileStream)) { int bytesRead; while ((bytesRead = rawFileReader.Read(rawFileBuffer, 0, bufSize)) > 0) { //Slice buffer to only data that was read encryptionBuffer1 = PowerAES.Encrypt(rawFileBuffer.Take(bytesRead).ToArray().GetString(), password).GetBytes(); encryptedFileWriter.Write(encryptionBuffer1, 0, encryptionBuffer1.Length); } } } } } File.Delete(DecVolumeLocation2); DeleteDirectory(unlLoc2); break; default: throw new InvalidOperationException("Cannot perform operation on unsupported volume version!"); } string metaS = new JavaScriptSerializer().Serialize(volMeta); File.WriteAllText(vaultL+"\\vault.metadata",metaS); }
void Button1Click(object sender, EventArgs e) { textBox4.Text = PowerAES.GenerateRandomString(1024); }
void Button2Click(object sender, EventArgs e) { textBox1.Text = PowerAES.GenerateRandomString(32); }