/// <summary> /// Checks if the given public key is valid. /// </summary> public static bool CheckPublicKey(byte[] bytes, Key publicKey) { if (publicKey.Version != LEA.Version.LEA9) { throw new WrongKeyVersionException(publicKey.Version, LEA.Version.LEA9); } return(LEA.CheckPublicKey(bytes, publicKey)); }
/// <summary> /// Generates a private key thanks to a public key. See <see cref="LEA.GetPrivateKey(Key)"/>. /// </summary> /// <param name="publicKey">The public key from which the private key will be generated.</param> public static Key GetPrivateKey(Key publicKey, bool silent = false) { if (publicKey.Version != LEA.Version.LEA9) { throw new WrongKeyVersionException(publicKey.Version, LEA.Version.LEA9); } return(LEA.GetPrivateKey(publicKey, silent)); }
/// <summary> /// Generates a random public key. /// </summary> /// <param name="seed">An optional seed used for the randomizer.</param> /// <param name="reversible"> /// Defines if the generated key has to be reversible. /// If not, you will not be able to decrypt a byte array (acts as a hash). /// </param> public static Key GeneratePublicKey(Version version, bool reversible = true, int seed = 0) { Random random = (seed <= 0) ? new Random() : new Random(seed); byte[,] fragments = new byte[(int)version / 2, (int)version / 2]; byte[] bytes = new byte[(int)version]; // Generate the key depending on the version. switch (version) { case Version.LEA4: bool pass = false; do { do { random.NextBytes(bytes); fragments[0, 0] = bytes[0]; fragments[0, 1] = bytes[1]; fragments[1, 0] = bytes[2]; fragments[1, 1] = bytes[3]; }while (((bytes[0] - bytes[2]) * (bytes[1] - bytes[3]) == 0)); pass = reversible ? LEA.CheckPublicKey(new Key(false, fragments, (int)version), true) : true; } while (!pass); break; case Version.LEA9: do { random.NextBytes(bytes); fragments[0, 0] = bytes[0]; fragments[0, 1] = bytes[1]; fragments[0, 2] = bytes[2]; fragments[1, 0] = bytes[3]; fragments[1, 1] = bytes[4]; fragments[1, 2] = bytes[5]; fragments[2, 0] = bytes[6]; fragments[2, 1] = bytes[7]; fragments[2, 2] = bytes[8]; }while (reversible ? !LEA.CheckPublicKey(new Key(false, fragments, (int)version), true) : false); break; // In case there is not the required version, throws an exception default: throw new UnsupportedVersionException((int)version); } return(new Key(false, fragments, (int)version)); }
/// <summary> /// Generates a random public key. /// </summary> /// <param name="seed">An optional seed used for the randomizer.</param> public static Key GeneratePublicKey(bool reversible = true, int seed = 0) => LEA.GeneratePublicKey(LEA.Version.LEA9, reversible, seed);
/// <summary> /// Checks if the given public key is valid. /// </summary> /// <param name="publicKey">The public key that will be checked.</param> public static bool CheckPublicKey(Key publicKey, bool silent = false) { return(LEA.CheckPublicKey(Encoding.Default.GetBytes("Leijnen Encryption Algorythm"), publicKey, silent)); }