public BigInteger RSAVP1(Models.RSAPublicKey ne, BigInteger s) { if (s <= 0 || s >= ne.n - 1) { throw new Exception("signature representative out of range"); } BigInteger m = BigInteger.ModPow(s, ne.e, ne.n); return(m); }
public BigInteger RSAEP(Models.RSAPublicKey ne, BigInteger m) { if (m <= 0 || m >= ne.n - 1) { throw new Exception("message representative out of range"); } BigInteger c = BigInteger.ModPow(m, ne.e, ne.n); return(c); }
//*????????????????????SHA256 = 256 bits(32bytes) public byte[] RSAES_OAEP_ENCRYPT(Models.RSAPublicKey ne, byte[] M, string L = "") { int mLen = (int)Math.Pow(2, 65) - 1; int hLen = 256 / 8; int k = ne.n.ToByteArray().Length; if (L.Length > mLen) { throw new Exception("label too long"); } if (mLen > k - 2 * hLen - 2) { throw new Exception("message too long"); } // EME-OAEP encoding SHA256Managed hashString = new SHA256Managed(); byte[] lHash = hashString.ComputeHash(Encoding.ASCII.GetBytes(L)); // DB = lHash || PS || 0x01 || M. byte[] PS = new byte[k - mLen - 2 * hLen - 2]; byte[] DB = new byte[k - hLen - 1]; var list = new List <byte>(); list.AddRange(lHash); list.AddRange(PS); list.AddRange(new byte[] { 0x01 }); list.AddRange(M); list.ToArray().CopyTo(DB, 0); byte[] seed = new byte[hLen]; random.NextBytes(seed); byte[] dbMask = MGF1(seed, k - hLen - 1); byte[] maskedDB = new byte[k - hLen - 1]; for (int i = 0; i < k - hLen - 1; i++) { maskedDB[i] = (byte)(DB[i] ^ dbMask[i]); } byte[] seedMask = MGF1(maskedDB, hLen); byte[] maskedSeed = new byte[hLen]; for (int i = 0; i < hLen; i++) { maskedSeed[i] = (byte)(seed[i] ^ seedMask[i]); } byte[] EM = new byte[k]; list = new List <byte>(); list.AddRange(new byte[] { 0x00 }); list.AddRange(maskedSeed); list.AddRange(maskedDB); list.ToArray().CopyTo(EM, 0); // RSA encryption BigInteger m = OS2IP(EM); BigInteger c = RSAEP(ne, m); byte[] C = I2OSP(c, k); return(C); }
public byte[] RSASSA_PKCS1_V1_5_VERIFY(Models.RSAPublicKey ne, byte[] M, byte[] S) { throw new NotImplementedException(); }
public string RSASSA_PSS_VERIFY(Models.RSAPublicKey ne, byte[] M, byte[] S) { throw new NotImplementedException(); }
public byte[] RSAES_PKCS1_V1_5_ENCRYPT(Models.RSAPublicKey ne, byte[] M) { throw new NotImplementedException(); }