Exemplo n.º 1
0
        public void CreateNew(string fileName, string passphrase)
        {
            _rsa = new RSACryptoServiceProvider(2048);

            if (File.Exists(fileName))
            {
                File.Delete(fileName);
            }

            if (_fileStream != null)
            {
                _fileStream.Dispose();
            }

            _fileStream = File.Open(fileName, FileMode.CreateNew);

            _header = EncryptionHelper.CreateHeader(_rsa, passphrase);

            var memoryStream = new MemoryStream();

            Serializer.Serialize(memoryStream, _header);

            var headerBytes = memoryStream.ToArray();
            var writer      = new BinaryWriter(_fileStream);

            writer.Write(headerBytes.Length);
            writer.Write(headerBytes);
            writer.Flush();

            IsOpen   = true;
            IsLocked = false;
        }
Exemplo n.º 2
0
        public void OpenDiary(string filePath)
        {
            _fileStream = File.Open(filePath, FileMode.Open);

            _header = ReadHeader(_fileStream, out _headerLength);

            _rsa = new RSACryptoServiceProvider();
            _rsa.ImportCspBlob(_header.PublicKey);

            IsLocked = true;
            IsOpen   = true;
        }
Exemplo n.º 3
0
        public static DiaryHeader CreateHeader(RSACryptoServiceProvider rsa, string passphrase)
        {
            var header = new DiaryHeader();

            header.PublicKey = rsa.ExportCspBlob(false);
            var privateBlob = rsa.ExportCspBlob(true);

            var rij = Rijndael.Create();

            var encryptedStream = new MemoryStream();

            using (var cs = new CryptoStream(encryptedStream, rij.CreateEncryptor(GetKey(passphrase), GetIV(passphrase)), CryptoStreamMode.Write))
            {
                cs.Write(privateBlob, 0, privateBlob.Length);
            }

            header.PrivateKeyEncrypted = encryptedStream.ToArray();

            return(header);
        }