public static MinerWallet Open(string path, SecureString password) { byte[] data; byte[] iv = new byte[16]; using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read)) { fs.Read(iv, 0, iv.Length); data = new byte[fs.Length - iv.Length]; fs.Read(data, 0, data.Length); } byte[] masterKey = password.ToArray().Sha256().Sha256(); using (AesManaged aes = new AesManaged()) using (ICryptoTransform decryptor = aes.CreateDecryptor(masterKey, iv)) { return new MinerWallet(decryptor.TransformFinalBlock(data, 0, data.Length)); } }
private void Save(string path, SecureString password) { byte[] masterKey = password.ToArray().Sha256().Sha256(); byte[] iv = new byte[16]; byte[] data; using (RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider()) { rng.GetNonZeroBytes(iv); } using (AesManaged aes = new AesManaged()) using (ICryptoTransform encryptor = aes.CreateEncryptor(masterKey, iv)) { ProtectedMemory.Unprotect(key_exported, MemoryProtectionScope.SameProcess); data = encryptor.TransformFinalBlock(key_exported, 0, key_exported.Length); ProtectedMemory.Protect(key_exported, MemoryProtectionScope.SameProcess); } using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None)) { fs.Write(iv, 0, iv.Length); fs.Write(data, 0, data.Length); } }