Пример #1
0
 private static extern int CryptoNative_RsaDecrypt(
     SafeEvpPKeyHandle pkey,
     ref byte source,
     int sourceLength,
     RSAEncryptionPaddingMode paddingMode,
     IntPtr digestAlgorithm,
     ref byte destination,
     int destinationLength);
Пример #2
0
        public static byte[] DecryptRSA(this byte[] data, RSAParameters privateKey,
                                        RSAEncryptionPaddingMode padding = RSAEncryptionPaddingMode.OaepSHA256)
        {
            using var rsa = RSA.Create();
            rsa.ImportParameters(privateKey);
            var result = padding switch
            {
                RSAEncryptionPaddingMode.Pkcs1 => rsa.Decrypt(data,
                                                              RSAEncryptionPadding.Pkcs1),
                RSAEncryptionPaddingMode.OaepSHA1 => rsa.Decrypt(data,
                                                                 RSAEncryptionPadding.OaepSHA1),
                RSAEncryptionPaddingMode.OaepSHA256 => rsa.Decrypt(data,
                                                                   RSAEncryptionPadding.OaepSHA256),
                RSAEncryptionPaddingMode.OaepSHA384 => rsa.Decrypt(data,
                                                                   RSAEncryptionPadding.OaepSHA384),
                RSAEncryptionPaddingMode.OaepSHA512 => rsa.Decrypt(data,
                                                                   RSAEncryptionPadding.OaepSHA512),
                _ => throw new ArgumentOutOfRangeException(nameof(padding), padding, null)
            };

            return(result);
        }
Пример #3
0
        internal static int RsaDecrypt(
            SafeEvpPKeyHandle pkey,
            ReadOnlySpan <byte> source,
            RSAEncryptionPaddingMode paddingMode,
            IntPtr digestAlgorithm,
            Span <byte> destination)
        {
            int written = CryptoNative_RsaDecrypt(
                pkey,
                ref MemoryMarshal.GetReference(source),
                source.Length,
                paddingMode,
                digestAlgorithm,
                ref MemoryMarshal.GetReference(destination),
                destination.Length);

            if (written < 0)
            {
                Debug.Assert(written == -1);
                throw CreateOpenSslCryptographicException();
            }

            return(written);
        }
 private RSAEncryptionPadding(RSAEncryptionPaddingMode mode, HashAlgorithmName oaepHashAlgorithm)
 {
     _mode = mode;
     _oaepHashAlgorithm = oaepHashAlgorithm;
 }
Пример #5
0
 public static string DecryptRSAToString(this byte[] data, RSAParameters privateKey,
                                         RSAEncryptionPaddingMode padding = RSAEncryptionPaddingMode.OaepSHA256)
 {
     return(DecryptRSA(data, privateKey, padding).ToText());
 }
Пример #6
0
 public static byte[] EncryptRSA(this string data, RSAParameters publicKey,
                                 RSAEncryptionPaddingMode padding = RSAEncryptionPaddingMode.OaepSHA256)
 {
     return(EncryptRSA(data.ToByteArray(), publicKey, padding));
 }