private byte[] SignHashInternal( byte[] hash , Org.BouncyCastle.Crypto.IDigest digest , bool asOaep ) { byte[] derEncoded = this.DerEncode(hash, digest); Org.BouncyCastle.Crypto.IAsymmetricBlockCipher rsaEngine = null; if (asOaep) // PSS: { rsaEngine = new Org.BouncyCastle.Crypto.Encodings.OaepEncoding( new Org.BouncyCastle.Crypto.Engines.RsaBlindedEngine() ); } else // Pkcs1 { rsaEngine = new Org.BouncyCastle.Crypto.Encodings.Pkcs1Encoding( new Org.BouncyCastle.Crypto.Engines.RsaBlindedEngine() ); } rsaEngine.Init(true, this.m_keyPair.Private); byte[] encoded = rsaEngine.ProcessBlock(derEncoded, 0, derEncoded.Length); return(encoded); } // End Function SignHashInternal
private byte[] DecryptInternal(byte[] bytesToDecrypt, bool asOaep) { if (bytesToDecrypt == null) { throw new System.ArgumentNullException(nameof(bytesToDecrypt)); } if (bytesToDecrypt.Length > this.KeySize / 8) { throw new System.Security.Cryptography.CryptographicException( $"Padding: data too big - key size in bytes: \"{System.Convert.ToString(this.KeySize / 8)}\"."); } Org.BouncyCastle.Crypto.IAsymmetricBlockCipher decryptionEngine = null; if (asOaep) { //Org.BouncyCastle.Crypto.Encodings.OaepEncoding decryptionEngine = decryptionEngine = new Org.BouncyCastle.Crypto.Encodings.OaepEncoding(new Org.BouncyCastle.Crypto.Engines.RsaEngine()); } else { // Org.BouncyCastle.Crypto.Encodings.Pkcs1Encoding decryptionEngine = decryptionEngine = new Org.BouncyCastle.Crypto.Encodings.Pkcs1Encoding(new Org.BouncyCastle.Crypto.Engines.RsaEngine()); } decryptionEngine.Init(false, m_keyParameter); return(decryptionEngine.ProcessBlock(bytesToDecrypt, 0, bytesToDecrypt.Length)); } // End Function DecryptInternal
public byte[] Decrypt(byte[] encryptedData) { encryptedData.ThrowIfNull(nameof(encryptedData)); if (encryptEngine == null) { var encryptEngineTemp = new Org.BouncyCastle.Crypto.Encodings.OaepEncoding(new Org.BouncyCastle.Crypto.Engines.RsaEngine(), new Org.BouncyCastle.Crypto.Digests.Sha256Digest()); Org.BouncyCastle.OpenSsl.PemReader pr = new Org.BouncyCastle.OpenSsl.PemReader( new System.IO.StringReader(privateKey)); Org.BouncyCastle.Crypto.AsymmetricCipherKeyPair keyParametersPrivate = (Org.BouncyCastle.Crypto.AsymmetricCipherKeyPair)pr.ReadObject(); encryptEngineTemp.Init(false, keyParametersPrivate.Private); encryptEngine = encryptEngineTemp; } return(encryptEngine.ProcessBlock(encryptedData, 0, encryptedData.Length)); }
private byte[] EncryptInternal(byte[] bytesToEncrypt, bool asOaep) { if (bytesToEncrypt == null) { throw new System.ArgumentNullException(nameof(bytesToEncrypt)); } Org.BouncyCastle.Crypto.IAsymmetricBlockCipher encryptEngine = null; if (asOaep) { //Org.BouncyCastle.Crypto.Encodings.OaepEncoding encryptEngine = encryptEngine = new Org.BouncyCastle.Crypto.Encodings.OaepEncoding(new Org.BouncyCastle.Crypto.Engines.RsaEngine()); } else { // Org.BouncyCastle.Crypto.Encodings.Pkcs1Encoding encryptEngine = encryptEngine = new Org.BouncyCastle.Crypto.Encodings.Pkcs1Encoding(new Org.BouncyCastle.Crypto.Engines.RsaEngine()); } encryptEngine.Init(true, this.m_keyParameter); return(encryptEngine.ProcessBlock(bytesToEncrypt, 0, bytesToEncrypt.Length)); } // End Function EncryptInternal
private bool VerifyHashInternal( byte[] hash , byte[] signature , Org.BouncyCastle.Crypto.IDigest digest , bool asOaep) { Org.BouncyCastle.Crypto.IAsymmetricBlockCipher rsaEngine = null; if (asOaep) // PSS: { rsaEngine = new Org.BouncyCastle.Crypto.Encodings.OaepEncoding( new Org.BouncyCastle.Crypto.Engines.RsaBlindedEngine() ); } else // Pkcs1 { rsaEngine = new Org.BouncyCastle.Crypto.Encodings.Pkcs1Encoding( new Org.BouncyCastle.Crypto.Engines.RsaBlindedEngine() ); } rsaEngine.Init(false, this.m_keyPair.Public); byte[] a = null; byte[] b = null; try { a = rsaEngine.ProcessBlock(signature, 0, signature.Length); b = this.DerEncode(hash, digest); } catch { return(false); } if (a.Length == b.Length) { return(Org.BouncyCastle.Utilities.Arrays.ConstantTimeAreEqual(a, b)); } if (a.Length != b.Length - 2) { return(false); } int num1 = a.Length - hash.Length - 2; int num2 = b.Length - hash.Length - 2; b[1] -= (byte)2; b[3] -= (byte)2; int num3 = 0; for (int index = 0; index < hash.Length; ++index) { num3 |= (int)a[num1 + index] ^ (int)b[num2 + index]; } for (int index = 0; index < num1; ++index) { num3 |= (int)a[index] ^ (int)b[index]; } return(num3 == 0); } // End Function VerifyHashInternal