/// <summary> /// Encrypts a file with the given private key. /// </summary> /// <param name="privateKey">Private key used to encrypt the file.</param> /// <returns>A boolean value indicating that the output file has been written.</returns> public static bool Decrypt(FileInfo file, FileInfo output, Key key) { if (!key.IsPrivate) { throw new InvalidKeyException(key); } if (key.Version != LEA.Version.LEA9) { throw new WrongKeyVersionException(key.Version, LEA.Version.LEA9); } if (!file.Exists) { throw new FileNotFoundException($"The file '{file.FullName}' has not been found.", file.Name); } try { File.WriteAllBytes(output.FullName, LEA9.Obfuscate(File.ReadAllBytes(file.FullName), key)); return(File.Exists(output.FullName)); } catch (Exception innerException) { throw new EncryptionException(key, file, innerException); } }
/// <summary> /// Checks if the given public key is valid. /// </summary> /// <param name="bytes">The array of bytes that will be crypted/decrypted as a checker.</param> /// <param name="publicKey">The public key that will be checked.</param> public static bool CheckPublicKey(byte[] bytes, Key publicKey, bool silent = false) { if (publicKey.IsPrivate) { return(false); } switch (publicKey.Version) { case Version.LEA4: return(bytes.IsEqual( LEA4.Obfuscate( LEA4.Obfuscate(bytes, publicKey), LEA4.GetPrivateKey(publicKey, silent) ) )); case Version.LEA9: return(bytes.IsEqual( LEA9.Obfuscate( LEA9.Obfuscate(bytes, publicKey), LEA9.GetPrivateKey(publicKey, silent) ) )); default: throw new UnsupportedVersionException((int)publicKey.Version); } }