/// <summary> /// The decrypt. /// </summary> /// <param name="encrypted"> /// The encrypted. /// </param> /// <param name="pk"> /// The pk. /// </param> /// <returns> /// The <see cref="string"/>. /// </returns> private string Decrypt(string encrypted, PrivateKey pk) { var keyParam = pk.GetPrivateKeyParam(); var engine = new Pkcs1Encoding(new RsaEngine()); engine.Init(false, keyParam); var blockSize = engine.GetInputBlockSize(); byte[] bytes = Convert.FromBase64String(encrypted); byte[] dec = engine.ProcessBlock(bytes, 0, blockSize); var clear = this.ToUTF8String(dec); return clear; }
/// <summary> /// The hybrid decrypt. /// </summary> /// <param name="crypted"> /// The crypted. /// </param> /// <param name="key"> /// The key. /// </param> /// <returns> /// The <see cref="string"/>. /// </returns> public string HybridDecrypt(Message crypted, PrivateKey key) { string secretKeyData = this.Decrypt(crypted.Key, key); SecretKey secretKey = this.keyMaster.DecodeSecretKey(secretKeyData); string message = this.Decrypt(crypted.Data, secretKey); return message; }
/// <summary> /// generates a key pair. /// </summary> /// <param name="keySize"> /// The key Size in bits /// </param> /// <returns> /// The <see cref="KeyPair"/>. /// </returns> public KeyPair GenerateKeyPair(int keySize) { var g = new RsaKeyPairGenerator(); g.Init(new KeyGenerationParameters(new SecureRandom(), keySize)); var kp = g.GenerateKeyPair(); var puk = new PublicKey(kp.Public); var prk = new PrivateKey(kp.Private); var keyPair = new KeyPair { PublicKey = puk, PrivateKey = prk }; return keyPair; }
/// <summary> /// The public decrypt. /// </summary> /// <param name="crypted"> /// The crypted. /// </param> /// <param name="key"> /// The key. /// </param> /// <returns> /// The <see cref="string"/>. /// </returns> public string PublicDecrypt(Message crypted, PrivateKey key) { return this.Decrypt(crypted.Data, key); }
/// <summary> /// The equals. /// </summary> /// <param name="other"> /// The other. /// </param> /// <returns> /// The <see cref="bool"/>. /// </returns> protected bool Equals(PrivateKey other) { return this.privateKeyParam.Equals(other.privateKeyParam); }
/// <summary> /// The equals. /// </summary> /// <param name="other"> /// The other. /// </param> /// <returns> /// The <see cref="bool"/>. /// </returns> protected bool Equals(PrivateKey other) { return(this.privateKeyParam.Equals(other.privateKeyParam)); }