Exemplo n.º 1
0
        public byte[] OaepEncrypt(byte[] data, TpmAlgId hashAlg, byte[] encodingParms)
        {
            int encLen = NumBits / 8;

            byte[]     zeroTermEncoding = GetLabel(encodingParms);
            byte[]     encoded          = CryptoEncoders.OaepEncode(data, zeroTermEncoding, hashAlg, encLen);
            BigInteger message          = FromBigEndian(encoded);
            BigInteger cipher           = BigInteger.ModPow(message, E, N);

            byte[] encMessageBigEnd = ToBigEndian(cipher, KeySize);
            if (encMessageBigEnd.Length < encLen)
            {
                encMessageBigEnd = Globs.AddZeroToBeginning(encMessageBigEnd, encLen - encMessageBigEnd.Length);
            }
            return(encMessageBigEnd);
        }
Exemplo n.º 2
0
        public byte[] OaepDecrypt(byte[] cipherText, TpmAlgId hashAlg, byte[] encodingParms)
        {
            byte[]     zeroTermEncoding = GetLabel(encodingParms);
            BigInteger cipher           = FromBigEndian(cipherText);
            BigInteger plain            = BigInteger.ModPow(cipher, D, N);

            byte[] encMessage = ToBigEndian(plain, KeySize - 1);
            byte[] message;

            // Hack - be robust to leading zeros
            while (true)
            {
                bool decodeOk = CryptoEncoders.OaepDecode(encMessage, zeroTermEncoding, hashAlg, out message);
                if (decodeOk)
                {
                    break;
                }
                encMessage = Globs.AddZeroToBeginning(encMessage);
            }
            return(message);
        }