Beispiel #1
0
        public static int PaddingSizeForMethod(PaddingAlgorithm paddingMethod)
        {
            switch (paddingMethod)
            {
            case PaddingAlgorithm.None: return(0);

            case PaddingAlgorithm.PKCS1: return(RsaPkcs1PaddingSize);

            case PaddingAlgorithm.PKCS1_OAEP: return(RsaPkcs1OaepPaddingSize);
            }

            throw new Exception();
        }
Beispiel #2
0
        public static int CalculateEncryptedSize(X509Certificate2 cert, int messageSize,
                                                 PaddingAlgorithm paddingAlgorithm)
        {
            var rsa = cert.PublicKey.Key as RSA;

            if (rsa == null)
            {
                throw new Exception("Could not create RSA");
            }

            int pad     = PaddingSizeForMethod(paddingAlgorithm);
            int keySize = CalculatePublicKeyLength(cert) / 8;

            if (keySize < pad)
            {
                throw new Exception();
            }

            int blockSize = keySize - pad;
            int numBlocks = (messageSize + blockSize - 1) / blockSize;

            return(numBlocks * keySize);
        }