public CipherData Load() { CipherData data = new CipherData(); using (FileStream fs = File.OpenRead(_FileName)) { byte x = (byte) fs.ReadByte(); byte[] b = new byte[x]; fs.Read(b, 0, b.Length); data.InitializationVector = b; x = (byte) fs.ReadByte(); b = new byte[x]; fs.Read(b, 0, b.Length); data.Passphrase = System.Text.Encoding.ASCII.GetString(b); x = (byte) fs.ReadByte(); b = new byte[x]; fs.Read(b, 0, b.Length); data.Salt= b; b = new byte[(int)(fs.Length - fs.Position)]; fs.Read(b, 0, b.Length); data.CipherText = b; } return data; }
public void Save(CipherData data) { File.Delete(FileName); using (FileStream fs = File.OpenWrite(_FileName)) { fs.WriteByte((byte)data.InitializationVector.Length); fs.Write(data.InitializationVector, 0, data.InitializationVector.Length); byte[] b = System.Text.Encoding.ASCII.GetBytes(data.Passphrase); fs.WriteByte((byte)b.Length); fs.Write(b, 0, b.Length); fs.WriteByte((byte)data.Salt.Length); fs.Write(data.Salt, 0, data.Salt.Length); fs.Write(data.CipherText, 0, data.CipherText.Length); } }
private void btnSave_Click(object sender, System.EventArgs e) { if ((LastEncrypted != null) && (LastEncrypted.Length != 0)) { CipherData cd = new CipherData(); cd.Salt = this.Salt; cd.Passphrase = txtPassPhrase.Text; cd.InitializationVector = HexStringToByteArray(txtInitVector.Text); cd.CipherText = LastEncrypted; store.Save(cd); AppendStatus(System.String.Format("{0} bytes saved in {1} ({2} crypto bytes).\r\n", cd.GetLength(), store.FileName, cd.CipherText.Length) ); } }