Exemple #1
0
        private byte[] Decrypt(byte[] data, Interop.libcrypto.OpenSslRsaPadding padding)
        {
            SafeRsaHandle key = _key.Value;

            CheckInvalidKey(key);

            byte[] buf = new byte[Interop.libcrypto.RSA_size(key)];

            int returnValue = Interop.libcrypto.RSA_private_decrypt(
                data.Length,
                data,
                buf,
                key,
                padding);

            CheckReturn(returnValue);

            // If the padding mode is RSA_NO_PADDING then the size of the decrypted block
            // will be RSA_size, so let's just return buf.
            //
            // If any padding was used, then some amount (determined by the padding algorithm)
            // will have been reduced, and only returnValue bytes were part of the decrypted
            // body, so copy the decrypted bytes to an appropriately sized array before
            // returning it.
            if (returnValue == buf.Length)
            {
                return(buf);
            }

            byte[] plainBytes = new byte[returnValue];
            Array.Copy(buf, 0, plainBytes, 0, returnValue);
            return(plainBytes);
        }
Exemple #2
0
        private byte[] Encrypt(byte[] data, Interop.libcrypto.OpenSslRsaPadding padding)
        {
            SafeRsaHandle key = _key.Value;

            CheckInvalidKey(key);

            byte[] buf = new byte[Interop.libcrypto.RSA_size(key)];

            int returnValue = Interop.libcrypto.RSA_public_encrypt(
                data.Length,
                data,
                buf,
                key,
                padding);

            CheckReturn(returnValue);

            return(buf);
        }
Exemple #3
0
        internal byte[] Encrypt(byte[] data, Interop.libcrypto.OpenSslRsaPadding padding)
        {
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }

            SafeRsaHandle key = _key.Value;

            CheckInvalidKey(key);

            byte[] buf = new byte[Interop.libcrypto.RSA_size(key)];

            int returnValue = Interop.libcrypto.RSA_public_encrypt(
                data.Length,
                data,
                buf,
                key,
                padding);

            CheckReturn(returnValue);

            return(buf);
        }