Decrypt() public method

public Decrypt ( byte data, RSAEncryptionPadding padding ) : byte[]
data byte
padding RSAEncryptionPadding
return byte[]
Example #1
0
        /// <summary>
        /// Decrypts a string (previously encrypted with the Encrypt method)
        /// </summary>
        /// <param name="base64String">The encrypted string</param>
        /// <param name="privateKey">The private key used to decrypt the string</param>
        /// <returns>A string containing the decrypted value</returns>
        public string Decrypt(string base64String, System.Security.Cryptography.RSAParameters privateKey)
        {
            string result = null;

            try
            {
                _rsa.ImportParameters(privateKey);
                int base64BlockSize = (256 % 3 != 0) ? ((256 / 3) * 4) + 4 : (256 / 3) * 4;
                int iterations      = base64String.Length / base64BlockSize;
                int l         = 0;
                var fullbytes = new byte[0];
                for (int i = 0; i < iterations; i++)
                {
                    byte[] encBytes = Convert.FromBase64String(base64String.Substring(base64BlockSize * i, base64BlockSize));
                    byte[] bytes    = _rsa.Decrypt(encBytes, System.Security.Cryptography.RSAEncryptionPadding.Pkcs1);
                    Array.Resize(ref fullbytes, fullbytes.Length + bytes.Length);
                    foreach (byte t in bytes)
                    {
                        fullbytes[l] = t;
                        l++;
                    }
                }
                result = fullbytes.AsString();// Encoding.UTF32.GetString(fullbytes);
            }
            catch (Exception e)
            {
                Log.e(e);
            }
            return(result);
        }
Example #2
0
 public string Decrypt(string cipherText)
 {
     if (_privateKeyRsaProvider == null)
     {
         throw new Exception("_privateKeyRsaProvider is null");
     }
     return(System.Text.Encoding.UTF8.GetString(_privateKeyRsaProvider.Decrypt(Convert.FromBase64String(cipherText), RSAEncryptionPadding.Pkcs1)));
 }
Example #3
0
        public static LongRSACryptKey GetLongRSACryptKeyFromEncryptedAES(ReadOnlySpan <Byte> encryptedkey, ReadOnlySpan <Byte> encryptediv,
                                                                         ReadOnlySpan <Byte> privatekey, RSAKeyType type = Cryptography.RSA.DefaultRSAKeyType, RSAParameters?parameters = null)
        {
            Rsa rsa = Cryptography.RSA.Create(privatekey, type, parameters);
            ReadOnlySpan <Byte> key = rsa.Decrypt(encryptedkey.ToArray());
            ReadOnlySpan <Byte> iv  = rsa.Decrypt(encryptediv.ToArray());

            return(new LongRSACryptKey(key, iv));
        }
        public string Decrypt(string cryptoText)
        {
            byte[] encryptedData = Convert.FromBase64String(cryptoText);
            byte[] data          = rsa.Decrypt(encryptedData, RSAEncryptionPadding.Pkcs1);
            var    text          = byteConverter.GetString(data);

            return(text);
        }
        public string Decrypt(string cipherText)
        {
            if (_privateKeyRsaProvider == null)
            {
                return(null);
            }

            return(Encoding.UTF8.GetString(_privateKeyRsaProvider.Decrypt(Convert.FromBase64String(cipherText), RSAEncryptionPadding.Pkcs1)));
        }
Example #6
0
        public byte[] Decrypt(byte[] rgb, bool fOAEP)
        {
            if (rgb == null)
                throw new ArgumentNullException(nameof(rgb));

            // size check -- must be at most the modulus size
            if (rgb.Length > (KeySize / 8))
                throw new CryptographicException(SR.Format(SR.Cryptography_Padding_DecDataTooBig, Convert.ToString(KeySize / 8)));

            return _impl.Decrypt(rgb, fOAEP ? RSAEncryptionPadding.OaepSHA1 : RSAEncryptionPadding.Pkcs1);
        }
Example #7
0
        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="rsa"></param>
        /// <param name="encryptedData"></param>
        /// <param name="padding"></param>
        /// <returns></returns>
        private static byte[] Decrypt(System.Security.Cryptography.RSA rsa, byte[] encryptedData, RSAEncryptionPadding padding = null)
        {
            padding ??= RSAEncryptionPadding.Pkcs1;
            byte[] result;
            var    step = rsa.KeySize / 8;

            // 长数据分割
            if (step != encryptedData.Length)
            {
                var pointer  = 0;
                var resBytes = new List <byte>();
                while (pointer < encryptedData.Length)
                {
                    resBytes.AddRange(rsa.Decrypt(encryptedData.Skip(pointer).Take(step).ToArray(), padding));
                    pointer += step;
                }
                result = resBytes.ToArray();
            }
            else
            {
                result = rsa.Decrypt(encryptedData, padding);
            }
            return(result);
        }
        [System.Security.SecuritySafeCritical]  // auto-generated
        public override byte[] DecryptKeyExchange(byte[] rgbData)
        {
            if (_rsaKey == null)
            {
                throw new CryptographicUnexpectedOperationException(Environment.GetResourceString("Cryptography_MissingKey"));
            }

            if (OverridesDecrypt)
            {
                return(_rsaKey.Decrypt(rgbData, RSAEncryptionPadding.OaepSHA1));
            }
            else
            {
                return(Utils.RsaOaepDecrypt(_rsaKey, SHA1.Create(), new PKCS1MaskGenerationMethod(), rgbData));
            }
        }
Example #9
0
        //
        // public methods
        //

        public override byte[] DecryptKeyExchange(byte[] rgbIn)
        {
            if (_rsaKey == null)
            {
                throw new CryptographicUnexpectedOperationException(Environment.GetResourceString("Cryptography_MissingKey"));
            }

            byte[] rgbOut;
            if (OverridesDecrypt)
            {
                rgbOut = _rsaKey.Decrypt(rgbIn, RSAEncryptionPadding.Pkcs1);
            }
            else
            {
                int    i;
                byte[] rgb;
                rgb = _rsaKey.DecryptValue(rgbIn);

                //
                //  Expected format is:
                //      00 || 02 || PS || 00 || D
                //      where PS does not contain any zeros.
                //

                for (i = 2; i < rgb.Length; i++)
                {
                    if (rgb[i] == 0)
                    {
                        break;
                    }
                }

                if (i >= rgb.Length)
                {
                    throw new CryptographicUnexpectedOperationException(Environment.GetResourceString("Cryptography_PKCS1Decoding"));
                }

                i++;            // Skip over the zero

                rgbOut = new byte[rgb.Length - i];
                Buffer.InternalBlockCopy(rgb, i, rgbOut, 0, rgbOut.Length);
            }
            return(rgbOut);
        }
Example #10
0
        public static string DecryptFromJsEncrypt(string encryptString, String key)
        {
            #region Java RSA/ECB/PKCS1Padding加密方式
            System.Security.Cryptography.RSA rsa = System.Security.Cryptography.RSA.Create();
            RSAParameters para = new RSAParameters();
            byte[]        b1   = null;
            byte[]        b2   = null;
            ResolveKey(key, out b1, out b2);
            para.Modulus  = b2;
            para.D        = b1;
            para.Exponent = Convert.FromBase64String(GlobalConfig.exponent);
            para.DP       = Convert.FromBase64String(GlobalConfig.dp);
            para.DQ       = Convert.FromBase64String(GlobalConfig.dq);
            para.InverseQ = Convert.FromBase64String(GlobalConfig.inverseQ);
            para.P        = Convert.FromBase64String(GlobalConfig.pp);
            para.Q        = Convert.FromBase64String(GlobalConfig.q);
            rsa.ImportParameters(para);
            byte[] enBytes = rsa.Decrypt(BinaryHelper.HexStringToBinary(encryptString, 0, encryptString.Length), RSAEncryptionPadding.Pkcs1);
            return(Encoding.UTF8.GetString(enBytes));

            #endregion
        }
Example #11
0
        public static byte[] DecryptFromBase64(System.Security.Cryptography.RSA rsa, string content, RSAEncryptionPadding padding)
        {
            var         data             = Convert.FromBase64String(content);
            int         KEYBIT           = rsa.KeySize;
            int         decryptBlockSize = KEYBIT / 8;
            List <byte> result           = new List <byte>();
            int         total            = data.Length;
            int         index            = 0;

            while (total > 0)
            {
                int read = Math.Min(decryptBlockSize, total);
                total -= read;
                byte[] bs = new byte[read];
                Array.Copy(data, index, bs, 0, read);
                bs = rsa.Decrypt(bs, padding);
                result.AddRange(bs);

                index += read;
            }
            return(result.ToArray());
        }
Example #12
0
        public static string Decrypt(string encryptString, String key)
        {
            #region Java RSA/ECB/NoPadding加密方式
            //byte[] b1;
            //byte[] b2;
            //ResolveKey(key, out b1, out b2);
            //BigInteger biE = new BigInteger(b1);
            //BigInteger biN = new BigInteger(b2);

            //BigInteger biText = new BigInteger(GetBytes(encryptString), GetBytes(encryptString).Length);
            //BigInteger biEnText = biText.modPow(biE, biN);
            //string temp = System.Text.Encoding.UTF8.GetString(biEnText.getBytes());
            //return temp;
            #endregion

            #region Java RSA/ECB/PKCS1Padding加密方式
            //encryptString = WebUtility.UrlDecode(encryptString);
            System.Security.Cryptography.RSA rsa = System.Security.Cryptography.RSA.Create();
            RSAParameters para = new RSAParameters();
            byte[]        b1   = null;
            byte[]        b2   = null;
            ResolveKey(key, out b1, out b2);
            para.Modulus  = b2;
            para.D        = b1;
            para.Exponent = Convert.FromBase64String(GlobalConfig.exponent);
            para.DP       = Convert.FromBase64String(GlobalConfig.dp);
            para.DQ       = Convert.FromBase64String(GlobalConfig.dq);
            para.InverseQ = Convert.FromBase64String(GlobalConfig.inverseQ);
            para.P        = Convert.FromBase64String(GlobalConfig.pp);
            para.Q        = Convert.FromBase64String(GlobalConfig.q);
            rsa.ImportParameters(para);
            byte[] enBytes = rsa.Decrypt(Convert.FromBase64String(encryptString), RSAEncryptionPadding.Pkcs1);
            return(Encoding.UTF8.GetString(enBytes));

            #endregion
        }
Example #13
0
 /// <summary>
 /// 解开Exponent加密的内容
 /// </summary>
 /// <param name="content"></param>
 /// <returns></returns>
 public byte[] Decrypt(byte[] content)
 {
     return(_rsa.Decrypt(content, System.Security.Cryptography.RSAEncryptionPadding.Pkcs1));
 }
Example #14
0
 public static byte[] RSADecrypt(this byte[] data, RSA rsa)
 {
     return(rsa.Decrypt(data, RSAEncryptionPadding.Pkcs1));
 }
Example #15
0
 public static byte[] Decrypt(byte[] data)
 {
     return(cryptor.Decrypt(data, RSAEncryptionPadding.Pkcs1));
 }
Example #16
0
 public static Byte[] Decrypt(Byte[] data, Rsa rsa)
 {
     return(rsa.Decrypt(data));
 }