private static void ConvertToDCStyle(String dataFileName, SecureStringOrArray password) { var oldStyleSet = BinaryDeserializeEncryptedObject <CredentialSet>(dataFileName, password); WcfBinaryCompressedSerializeEncrypted(dataFileName + "new", password, oldStyleSet); //MtomSerializeEncryptedObject<M.UI.Model.NoString.CredentialSet>(newDataFileName, password, newCredentials); //var newSet = MtomDeserializeEncryptedObject<M.UI.Model.NoString.CredentialSet>(newDataFileName, password); }
public void SerializeData <T>(SecureStringOrArray password, T objectValue) { using (var stream = new MemoryStream()) using (var aes = CreateCrypto(password)) using (var cryptoStream = new CryptoStream(stream, aes.EncryptorTransform, CryptoStreamMode.Write)) { ServiceIo.WcfBinaryCompressedSerialize(cryptoStream, objectValue); cryptoStream.FlushFinalBlock(); _secureFileRepository.SaveDataFile(stream.ToArray(), false, -1, null); } }
private Button CreateSaveButton(Control parent) { var b = new Button { Text = "Save" }; b.Click += (sender, args) => { if (FilterCollection.Count > 1) { var result = PasswordInputBox.GetPassword(parent, "Master Password"); if (!string.IsNullOrEmpty(result)) { using (var password = new SecureStringOrArray(Encoding.ASCII.GetBytes(result))) { var folderBasedFilePathProvider = new FolderBasedFilePathProvider("."); var localSecureFileRepository = new LocalSecureFileRepository(folderBasedFilePathProvider, "muipw.iv", DataFileName, "muipw.ver", "muipw.salt"); var aesFormatter = new AesFormatter(localSecureFileRepository); try { //just test that the password is correct by deserializing the current data. var credentialSet = aesFormatter.DeserializeData <CredentialSet>(password); _secretKeyProvider.Initialize(credentialSet.Credentials); _secretKeyProvider.Initialize(FilterCollection); var mSecretsSecretKey = _secretKeyProvider.GetSecret(SecretKeyConstants.MSecretsSecretKeyName); if (mSecretsSecretKey == null) { MessageBox.Show($"Missing {SecretKeyConstants.MSecretsSecretKeyName}!"); return; } var fileBroker2 = SecureFileBroker.Create(folderBasedFilePathProvider, mSecretsSecretKey, "muipw.iv", DataFileName, "muipw.ver", "muipw.salt"); aesFormatter = new AesFormatter(fileBroker2); var set = new CredentialSet(); foreach (var credential in FilterCollection) { set.Credentials.Add(credential); } aesFormatter.SerializeData(password, set); } catch (Exception) { MessageBox.Show("Incorrect password!"); } } } } }; return(b); }
public T DeserializeData <T>(SecureStringOrArray password) { //return AESBasedFileEncryption.WcfBinaryCompressedDeserializeEncrypted<T>(password); using (var stream = new MemoryStream(_secureFileRepository.GetDataFile())) using (var aes = CreateCrypto(password)) using (var cryptoStream = new CryptoStream( stream, aes.DecryptorTransform, CryptoStreamMode.Read)) { var result = ServiceIo.WcfBinaryCompressedDeserialize <T>(cryptoStream); return(result); } }
public static byte[] GenerateKey(SecureStringOrArray password, byte[] salt) { var passwordBytes2 = password.ByteArray; try { //the recommended number of iterations is 1000, lets just chose something random that is near to that. var foo = new Rfc2898DeriveBytes(passwordBytes2, salt, 1076); var finalBytes = foo.GetBytes(32); return(finalBytes); } finally { password.ZeroBytesIfRecreatable(passwordBytes2); } }
public static void BinarySerializeObjectEncrypted(String dataFileName, SecureStringOrArray password, object objectValue) { SetupDataName(dataFileName); String dataFilePath = GetDataFilePath(dataFileName); String tempDataFilePath = SwapExtension(dataFilePath, ".tmp"); using (var stream = File.Open(tempDataFilePath, FileMode.Create)) using (DisposableAes aes = CreateCrypto(password, dataFileName)) using (CryptoStream cryptoStream = new CryptoStream( stream, aes.EncryptorTransform, CryptoStreamMode.Write)) { BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(cryptoStream, objectValue); } SafeFileMove(dataFilePath, tempDataFilePath); }
public static T1 BinaryDeserializeEncryptedObject <T1>(String dataFileName, SecureStringOrArray password) { ConvertToDCStyle(dataFileName, password); String dataFilePath = GetDataFilePath(dataFileName); using (var stream = File.Open(dataFilePath, FileMode.Open)) using (DisposableAes aes = CreateCrypto(password, dataFileName)) using (CryptoStream cryptoStream = new CryptoStream( stream, aes.DecryptorTransform, CryptoStreamMode.Read)) { BinaryFormatter formatter = new BinaryFormatter(); var result = (T1)formatter.Deserialize(cryptoStream); return(result); } }
private DisposableAes CreateCrypto(SecureStringOrArray password) { return(new DisposableAes(AesBasedFileEncryption.GenerateKey(password, _secureFileRepository.GetSalt()), _secureFileRepository.GetIv())); }
public static void WcfBinaryCompressedSerializeEncrypted <T1>(String dataFileName, SecureStringOrArray password, T1 objectValue) { SetupDataName(dataFileName); String dataFilePath = GetDataFilePath(dataFileName); String tempDataFilePath = SwapExtension(dataFilePath, ".tmp"); using (var stream = File.Open(tempDataFilePath, FileMode.Create)) using (DisposableAes aes = CreateCrypto(password, dataFileName)) using (CryptoStream cryptoStream = new CryptoStream( stream, aes.EncryptorTransform, CryptoStreamMode.Write)) { ServiceIo.WcfBinaryCompressedSerialize(cryptoStream, objectValue); } SafeFileMove(dataFilePath, tempDataFilePath); }
private static DisposableAes CreateCrypto(SecureStringOrArray password, String dataFileName) { String ivPath = GetIVPath(dataFileName); return(new DisposableAes(GenerateKey(password, dataFileName), File.ReadAllBytes(ivPath))); }
private static byte[] GenerateKey(SecureStringOrArray password, String saltName) { var filePath = GetSaltFilePath(saltName); return(GenerateKey(password, File.ReadAllBytes(filePath))); }
public static T1 WcfBinaryCompressedDeserializeEncrypted <T1>(String dataFileName, SecureStringOrArray password) { var dataFilePath = GetDataFilePath(dataFileName); using (var stream = File.Open(dataFilePath, FileMode.Open)) using (var aes = CreateCrypto(password, dataFileName)) using (var cryptoStream = new CryptoStream( stream, aes.DecryptorTransform, CryptoStreamMode.Read)) { var result = ServiceIo.WcfBinaryCompressedDeserialize <T1>(cryptoStream); return(result); } }