private void LoadMetaDataFromFile(string filePath) { try { int metaSize; byte[] metaSizeBytes = new byte[4]; FileStream metaSizeStream = new FileStream(filePath, FileMode.Open); metaSizeStream.Read(metaSizeBytes, 0, 4); metaSizeStream.Close(); metaSize = BitConverter.ToInt32(metaSizeBytes, 0); byte[] faesMetaData = new byte[metaSize]; FileStream faesMeta = new FileStream(filePath, FileMode.Open); faesMeta.Read(faesMetaData, 0, metaSize); faesMeta.Close(); try { if (metaSize < 7) { LoadLegacyMetaDataFromFile(filePath); } else { ushort faesIdentifierSize; byte[] faesIdentifierSizeBytes = new byte[2]; Array.Copy(faesMetaData, 4, faesIdentifierSizeBytes, 0, 2); faesIdentifierSize = BitConverter.ToUInt16(faesIdentifierSizeBytes, 0); byte[] faesIdentifierBytes = new byte[faesIdentifierSize]; Array.Copy(faesMetaData, 6, faesIdentifierBytes, 0, Convert.ToInt32(faesIdentifierSize)); if (CryptUtils.ConvertBytesToString(faesIdentifierBytes) != CryptUtils.GetCryptIdentifier()) { LoadLegacyMetaDataFromFile(filePath); } else { InitWithBytes(faesMetaData); } } } catch (IOException) { Logging.Log("File is already open in another program!", Severity.ERROR); } catch (Exception) { LoadLegacyMetaDataFromFile(filePath); } } catch (Exception) { LoadLegacyMetaDataFromFile(filePath); } }
/// <summary> /// Converts variables into easy-to-manage method calls to create a byte array for metadata /// </summary> /// <param name="checksumHashType">Type of checksum used to generate the file hash</param> /// <param name="originalFileHash">File hash of the original file (checksum generated using previous hash type)</param> /// <param name="passwordHint">A password hint for the password used to encrypt the file</param> /// <param name="compressionModeUsed">Compression mode used to compress the file</param> /// <param name="originalFileName">The name of the original file</param> public DynamicMetadata(Checksums.ChecksumType checksumHashType, byte[] originalFileHash, string passwordHint, string compressionModeUsed, string originalFileName) { _faesIdentifier = CryptUtils.ConvertStringToBytes(CryptUtils.GetCryptIdentifier()); _hashType = CryptUtils.ConvertChecksumTypeToBytes(checksumHashType); _originalFileHash = originalFileHash; _encryptionTimestamp = BitConverter.GetBytes((long)(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds); _passwordHint = CryptUtils.ConvertStringToBytes(passwordHint.TrimEnd('\n', '\r')); _encryptionVersion = CryptUtils.ConvertStringToBytes(FileAES_Utilities.GetVersion()); _compressionMode = CryptUtils.ConvertStringToBytes(compressionModeUsed); _originalFileName = CryptUtils.ConvertStringToBytes(originalFileName); _totalMetadataSize = (4 + 2 + _faesIdentifier.Length + 2 + _hashType.Length + 2 + _originalFileHash.Length + 2 + _encryptionTimestamp.Length + 2 + _passwordHint.Length + 2 + _encryptionVersion.Length + 2 + _compressionMode.Length + 2 + _originalFileName.Length); }
/// <summary> /// Checks the metadata to see if the file can be decrypted /// </summary> /// <param name="filePath">Path to file (Compatibility)</param> /// <returns>If the file can be decrypted</returns> public bool IsDecryptable(string filePath) { if (_usingCompatibilityMode) { return(new LegacyCrypt().IsDecryptable(filePath)); } else if (_dynamicMetadata.GetFaesIdentifier() == CryptUtils.GetCryptIdentifier()) { return(true); } else { return(false); } }