Exemple #1
0
        public byte[] RSAES_OAEP_DECRYPT(Models.RSAPrivateKey K, byte[] C, string L)
        {
            BigInteger n    = K.p * K.q;
            int        mLen = (int)Math.Pow(2, 65) - 1;
            int        hLen = 256 / 8;
            int        k    = n.ToByteArray().Length;

            if (L.Length > mLen)
            {
                throw new Exception("decryption error");
            }
            if (C.Length != k)
            {
                throw new Exception("decryption error");
            }
            if (k < 2 * hLen + 2)
            {
                throw new Exception("decryption error");
            }

            BigInteger c = OS2IP(C);
            BigInteger m = RSADP(K, c);

            byte[] EM = I2OSP(m, k);
            // EME-OAEP decoding
            SHA256Managed hashString = new SHA256Managed();

            byte[] lHash = hashString.ComputeHash(Encoding.ASCII.GetBytes(L));

            byte[] Y          = new byte[] { EM[0] };
            byte[] maskedSeed = new byte[hLen];
            byte[] maskedDB   = new byte[k - hLen - 1];
            Buffer.BlockCopy(EM, 1, maskedSeed, 0, hLen);
            Buffer.BlockCopy(EM, hLen + 1, maskedDB, 0, k - hLen - 1);

            byte[] seedMask = MGF1(maskedDB, hLen);
            byte[] seed     = new byte[hLen];
            for (int i = 0; i < hLen; i++)
            {
                seed[i] = (byte)(maskedSeed[i] ^ seedMask[i]);
            }

            byte[] dbMask = MGF1(seed, k - hLen - 1);
            byte[] DB     = new byte[k - hLen - 1];
            for (int i = 0; i < k - hLen - 1; i++)
            {
                DB[i] = (byte)(maskedDB[i] ^ dbMask[i]);
            }
            //.......................................Page 26


            throw new NotImplementedException();
        }
Exemple #2
0
        public BigInteger RSASP1(Models.RSAPrivateKey K, BigInteger m)
        {
            if (m <= 0 || m >= (K.p * K.q - 1))
            {
                throw new Exception("message representative out of range");
            }
            BigInteger s_1 = BigInteger.ModPow(m, K.dP, K.p);
            BigInteger s_2 = BigInteger.ModPow(m, K.dQ, K.q);
            BigInteger h   = (s_1 - s_2) * K.qInv % K.p;
            BigInteger s   = s_2 + K.q * h;

            return(s);
        }
Exemple #3
0
        public BigInteger RSADP(Models.RSAPrivateKey K, BigInteger c)
        {
            if (c <= 0 || c >= (K.p * K.q - 1))
            {
                throw new Exception("ciphertext representative out of range");
            }
            BigInteger m_1 = BigInteger.ModPow(c, K.dP, K.p);
            BigInteger m_2 = BigInteger.ModPow(c, K.dQ, K.q);
            BigInteger h   = (m_1 - m_2) * K.qInv % K.p;
            BigInteger m   = m_2 + K.q * h;

            return(m);
        }
Exemple #4
0
 public byte[] RSASSA_PKCS1_V1_5_SIGN(Models.RSAPrivateKey K, byte[] M)
 {
     throw new NotImplementedException();
 }
Exemple #5
0
 public byte[] RSAES_PKCS1_V1_5_DECRYPT(Models.RSAPrivateKey K, byte[] M)
 {
     throw new NotImplementedException();
 }