/// <summary> /// Decrypts the file, which was provided to this instance. /// </summary> /// <param name="filePath">Path to the file</param> /// <param name="saveDir">Directory, where the decrypted file is saved</param> /// <param name="deleteOriginal">Determines if the file of filePath is deleted</param> public void Decrypt(string filePath, string saveDir = "", bool deleteOriginal = false) { FileStream targetFile = OpenTargetFile(filePath); FileStream tempFile = CreateTempFile(); var writer = new BinaryWriter(tempFile); var reader = new BinaryReader(targetFile); //check file type if (reader.ReadInt32() != BitConverter.ToInt32(ToByte(_secureFileType), 0)) { throw new Exception("Not SECF FileType!"); } string encryptionMethod = ToString(reader.ReadBytes(3)); //read encryption method //secure header | 80bytes byte[] secureHeader = reader.ReadBytes(_secureHeaderSize); _cypher.Decrypt(ref secureHeader, 0); long fileLength = BitConverter.ToInt64(secureHeader, 0); //read length of original file | 8bytes string fileName = FromFixSizedByte(secureHeader, 8); //name of original file | 40bytes string fileExtension = FromFixSizedByte(secureHeader, 48); //file extension of original file | 16bytes string validation = ToString(secureHeader, 64); //read validation | 16bytes if (validation != _decryptionValidation) //check if key is correct { throw new Exception("Incorrect Key!"); } //timer TimeSpan totalTime = new TimeSpan(); var stopWatch = new Stopwatch(); stopWatch.Start(); //decryption of file while (targetFile.Length - targetFile.Position > 1) { byte[] state = reader.ReadBytes((int)Math.Min(targetFile.Length - targetFile.Position, _chunkSize)); //read chunks from file or the entire file if file < 1mb _cypher.Decrypt(ref state, 0); writer.Write(state); //Events and Measurement totalTime = totalTime.Add(stopWatch.Elapsed); OnChunkUpdate(targetFile.Position, targetFile.Length, state.Length, totalTime, stopWatch.Elapsed, ChunkEventArgs.ProcessType.Decryption); stopWatch.Restart(); } tempFile.SetLength(fileLength); //set stream to original length and remove padding stopWatch.Stop(); OnProcessCompleted(targetFile.Length, totalTime, ChunkEventArgs.ProcessType.Decryption); Save(Path.Combine(saveDir, fileName + fileExtension), tempFile); DeleteTempFile(tempFile); CloseTargetFile(targetFile, deleteOriginal); }
private static void EncryptDecryptPlainText(Options ops, ICypher cypher) { string originalText = File.ReadAllText(ops.SourceFilePath); EndOfLine endOfLine; if (!AlphabeticCypher.IsTextPlain(originalText, out endOfLine)) { throw new InvalidOperationException("Text is not considered plain text"); } cypher.Alphabet = AlphabeticCypher.GetPlainTextAlphabet(endOfLine); if (ops.Action == Action.Encrypt) { string plainText = originalText; string cryptoText = cypher.Encrypt(plainText, ops.EncryptionKey); File.WriteAllText(ops.TargetFilePath, cryptoText); } else { string cryptoText = originalText; string plainText = cypher.Decrypt(cryptoText, ops.EncryptionKey); File.WriteAllText(ops.TargetFilePath, plainText); } }
public static void Decrypt( this ICypher self, Stream cypher, IRng key, Stream outPlain) { if (!cypher.CanRead) { throw new InvalidOperationException($"Input stream does not allow read"); } if (!outPlain.CanWrite) { throw new InvalidOperationException($"Output stream does not allow write"); } byte[] buff = new byte[1024]; int read; while ((read = cypher.Read(buff, 0, 1024)) != 0) { byte[] bytesRead = buff.Take(read).ToArray(); byte[] bytesWrite = self.Decrypt(bytesRead, key); outPlain.Write(bytesWrite, 0, bytesWrite.Length); } }
public override byte[] Decrypt(byte[] plain, byte[] key) { byte[] salt, newKey; int skipCount; salt = CryptoUtils.DecodeBytes(BytesToIndices(plain), Alphabet.Count, out skipCount); Shaker.SaltKey(key, salt, out newKey); byte[] res = _cypher.Decrypt(plain.Skip(skipCount).ToArray(), newKey); return(res); }
private static void EncryptDecryptBinary(Options ops, ICypher cypher) { byte[] originalText = File.ReadAllBytes(ops.SourceFilePath); if (ops.Action == Action.Encrypt) { byte[] plainText = originalText; byte[] cryptoText = cypher.Encrypt( plainText, ops.EncryptionKey.ToByteArray()); File.WriteAllBytes(ops.TargetFilePath, cryptoText); } else { byte[] cryptoText = originalText; byte[] plainText = cypher.Decrypt( cryptoText, ops.EncryptionKey.ToByteArray()); File.WriteAllBytes(ops.TargetFilePath, plainText); } }
private bool StreamTester(ICypher cypher, string plain, string key) { int capacity = plain.Length * 2 + 256; //2 bytes per char using (Stream plainStream = GenerateStreamFromString(plain)) using (MemoryStream cryptoStream = new MemoryStream(capacity)) using (MemoryStream backPlainStream = new MemoryStream(capacity)) { byte[] keyArr = key.ToByteArray(); plainStream.Position = 0; cryptoStream.Position = 0; backPlainStream.Position = 0; cypher.Encrypt(plainStream, keyArr, cryptoStream); plainStream.Position = 0; cryptoStream.Position = 0; backPlainStream.Position = 0; cypher.Decrypt(cryptoStream, keyArr, backPlainStream); plainStream.Position = 0; cryptoStream.Position = 0; backPlainStream.Position = 0; int origByte, newByte; do { origByte = plainStream.ReadByte(); newByte = backPlainStream.ReadByte(); if (origByte != newByte) { return(false); } } while (origByte != -1 && newByte != -1); return(true); } }
public async Task <OperationResult <WebsiteOutputModel> > Handle(GetWebsite request, CancellationToken cancellationToken) { Website website = await _repository.GetByIdAsync(request.WebsiteId); if (website is null) { return(OperationResult <WebsiteOutputModel> .Failure(new Dictionary <string, string> { { "WebsiteId", ErrorMessages.WebsiteNotFound } })); } return(OperationResult <WebsiteOutputModel> .Success(website.ToWebsiteOutputModel(_cypher.Decrypt(website.Password)))); }
public static string Decrypt(this ICypher self, string cypher, string key) { byte[] cypherByte = cypher.ToByteArray(); byte[] keyByte = key.ToByteArray(); return(self.Decrypt(cypherByte, keyByte).ToTextString()); }