public override bool Equals(object obj) { RSAPublicKey p = obj as RSAPublicKey; if (p == null) { return(false); } return(BigInt.Compare(mod, p.mod) == 0 && BigInt.Compare(e, p.e) == 0); }
public static int Encrypt(RSAPublicKey pk, byte[] buf, byte[] outBuf, int outOff) { return(Encrypt(pk, buf, 0, buf.Length, outBuf, outOff)); }
public static bool Verify(RSAPublicKey pk, byte[] header, byte[] headerAlt, byte[] hash, int hashOff, int hashLen, byte[] sig, int sigOff, int sigLen) { /* * Signature must be an integer less than the modulus, * but encoded over exactly the same size as the modulus. */ byte[] n = pk.Modulus; int modLen = n.Length; if (sigLen != modLen) { return(false); } byte[] x = new byte[modLen]; Array.Copy(sig, sigOff, x, 0, modLen); if (BigInt.Compare(x, n) >= 0) { return(false); } /* * Do the RSA exponentation, then verify and remove the * "Type 1" padding (00 01 FF...FF 00 with at least * eight bytes of value FF). */ x = BigInt.ModPow(x, pk.Exponent, n); if (x.Length < 11 || x[0] != 0x00 || x[1] != 0x01) { return(false); } int k = 2; while (k < x.Length && x[k] == 0xFF) { k++; } if (k < 10 || k == x.Length || x[k] != 0x00) { return(false); } k++; /* * Check that the remaining byte end with the provided * hash value. */ int len = modLen - k; if (len < hashLen) { return(false); } for (int i = 0; i < hashLen; i++) { if (x[modLen - hashLen + i] != hash[hashOff + i]) { return(false); } } len -= hashLen; /* * Header is at offset 'k', and length 'len'. Compare * with the provided header(s). */ if (Eq(header, 0, header.Length, x, k, len)) { return(true); } if (headerAlt != null) { if (Eq(headerAlt, 0, headerAlt.Length, x, k, len)) { return(true); } } return(false); }
/* * Encrypt a message with a public key. This applied PKCS#1 v1.5 * "type 2" padding. If the message length exceeds the maximum * that can be processed with that public key, an exception is * thrown. * * There are four methods, depending on the kind of source * operand, and how the destination is to be obtained. */ public static byte[] Encrypt(RSAPublicKey pk, byte[] buf) { return(Encrypt(pk, buf, 0, buf.Length)); }
/* * Get the maximum length (in bytes) of an RSA-encrypted value * with the specified public key. */ public static int GetMaxEncryptedLength(RSAPublicKey pk) { return(pk.Modulus.Length); }