public static bool _ctor_System_Security_Cryptography_RNGCryptoServiceProvider_System_Security_Cryptography_CspParameters( )
        {
            //Parameters
            System.Security.Cryptography.CspParameters cspParams = null;


            //Exception
            Exception exception_Real        = null;
            Exception exception_Intercepted = null;

            InterceptionMaintenance.disableInterception( );

            try
            {
                returnValue_Real = System.Security.Cryptography.RNGCryptoServiceProvider.ctor(cspParams);
            }

            catch (Exception e)
            {
                exception_Real = e;
            }


            InterceptionMaintenance.enableInterception( );

            try
            {
                returnValue_Intercepted = System.Security.Cryptography.RNGCryptoServiceProvider.ctor(cspParams);
            }

            catch (Exception e)
            {
                exception_Intercepted = e;
            }
        }
        static SignatureTests()
        {
            var csp = new System.Security.Cryptography.CspParameters();

            csp.Flags      = System.Security.Cryptography.CspProviderFlags.CreateEphemeralKey;
            csp.KeyNumber  = (int)System.Security.Cryptography.KeyNumber.Signature;
            _asymmetricKey = new System.Security.Cryptography.RSACryptoServiceProvider(2048, csp);
        }
Exemplo n.º 3
0
 /// <summary>
 /// 解密
 /// </summary>
 public static string RSADecrypt(string ciphertext)
 {
     System.Security.Cryptography.CspParameters param = new System.Security.Cryptography.CspParameters();
     param.KeyContainerName = MY_TOKEN;
     using (System.Security.Cryptography.RSACryptoServiceProvider rsa = new System.Security.Cryptography.RSACryptoServiceProvider(param))
     {
         byte[] encryptdata = Convert.FromBase64String(ciphertext);
         byte[] decryptdata = rsa.Decrypt(encryptdata, false);
         return(System.Text.Encoding.Default.GetString(decryptdata));
     }
 }
Exemplo n.º 4
0
 /// <summary>
 /// 加密
 /// </summary>
 public static string RSAEncryption(string express)
 {
     System.Security.Cryptography.CspParameters param = new System.Security.Cryptography.CspParameters();
     param.KeyContainerName = MY_TOKEN;
     using (System.Security.Cryptography.RSACryptoServiceProvider rsa = new System.Security.Cryptography.RSACryptoServiceProvider(param))
     {
         byte[] plaindata   = System.Text.Encoding.Default.GetBytes(express);
         byte[] encryptdata = rsa.Encrypt(plaindata, false);
         return(Convert.ToBase64String(encryptdata));
     }
 }
Exemplo n.º 5
0
 /// <summary>
 /// RSA加密数据
 /// </summary>
 /// <param name="express">要加密数据</param>
 /// <returns>以加密的字符串</returns>
 public static string RSAEncryption(string express)
 {
     System.Security.Cryptography.CspParameters param = new System.Security.Cryptography.CspParameters();
     param.KeyContainerName = KeyContainerName; //密匙容器的名称,保持加密解密一致才能解密成功
     using (System.Security.Cryptography.RSACryptoServiceProvider rsa = new System.Security.Cryptography.RSACryptoServiceProvider(param))
     {
         byte[] plaindata   = System.Text.Encoding.Default.GetBytes(express); //将要加密的字符串转换为字节数组
         byte[] encryptdata = rsa.Encrypt(plaindata, false);                  //将加密后的字节数据转换为新的加密字节数组
         return(Convert.ToBase64String(encryptdata));                         //将加密后的字节数组转换为字符串
     }
 }
Exemplo n.º 6
0
        public static Tuple <string, string> CreateKeyPair()
        {
            System.Security.Cryptography.CspParameters cspParams = new System.Security.Cryptography.CspParameters {
                ProviderType = 1
            };

            System.Security.Cryptography.RSACryptoServiceProvider rsaProvider = new System.Security.Cryptography.RSACryptoServiceProvider(1024, cspParams);

            string publicKey  = Convert.ToBase64String(rsaProvider.ExportCspBlob(false));
            string privateKey = Convert.ToBase64String(rsaProvider.ExportCspBlob(true));

            return(new Tuple <string, string>(privateKey, publicKey));
        }
Exemplo n.º 7
0
        /*公開鍵&秘密鍵生成*/
        public static string CreateKeys(string containerName)
        {
            //CspParametersオブジェクトの作成
            System.Security.Cryptography.CspParameters cp =
                new System.Security.Cryptography.CspParameters();
            //キーコンテナ名を指定する
            cp.KeyContainerName = containerName;
            //CspParametersを指定してRSACryptoServiceProviderオブジェクトを作成
            System.Security.Cryptography.RSACryptoServiceProvider rsa =
                new System.Security.Cryptography.RSACryptoServiceProvider(cp);

            //公開鍵をXML形式で取得して返す
            return(rsa.ToXmlString(false));
        }
Exemplo n.º 8
0
        public static byte[] Encrypt(string publicKey, string data)
        {
            System.Security.Cryptography.CspParameters cspParams = new System.Security.Cryptography.CspParameters {
                ProviderType = 1
            };
            System.Security.Cryptography.RSACryptoServiceProvider rsaProvider = new System.Security.Cryptography.RSACryptoServiceProvider(cspParams);

            rsaProvider.ImportCspBlob(Convert.FromBase64String(publicKey));

            byte[] plainBytes     = System.Text.Encoding.UTF8.GetBytes(data);
            byte[] encryptedBytes = rsaProvider.Encrypt(plainBytes, false);

            return(encryptedBytes);
        }
Exemplo n.º 9
0
        public static string Decrypt(string privateKey, byte[] encryptedBytes)
        {
            System.Security.Cryptography.CspParameters cspParams = new System.Security.Cryptography.CspParameters {
                ProviderType = 1
            };
            System.Security.Cryptography.RSACryptoServiceProvider rsaProvider = new System.Security.Cryptography.RSACryptoServiceProvider(cspParams);

            rsaProvider.ImportCspBlob(Convert.FromBase64String(privateKey));

            byte[] plainBytes = rsaProvider.Decrypt(encryptedBytes, false);

            string plainText = System.Text.Encoding.UTF8.GetString(plainBytes, 0, plainBytes.Length);

            return(plainText);
        }
Exemplo n.º 10
0
        /*キーコンテナ削除*/
        public static void DeleteKeys(string containerName)
        {
            //CspParametersオブジェクトの作成
            System.Security.Cryptography.CspParameters cp =
                new System.Security.Cryptography.CspParameters();
            //キーコンテナ名を指定する
            cp.KeyContainerName = containerName;
            //CspParametersを指定してRSACryptoServiceProviderオブジェクトを作成
            System.Security.Cryptography.RSACryptoServiceProvider rsa =
                new System.Security.Cryptography.RSACryptoServiceProvider(cp);

            //キーコンテナを削除
            rsa.PersistKeyInCsp = false;
            rsa.Clear();
        }
Exemplo n.º 11
0
        /*複合化*/
        public static string Decrypt(string str, string containerName)
        {
            //CspParametersオブジェクトの作成
            System.Security.Cryptography.CspParameters cp =
                new System.Security.Cryptography.CspParameters();
            //キーコンテナ名を指定する
            cp.KeyContainerName = containerName;
            //CspParametersを指定してRSACryptoServiceProviderオブジェクトを作成
            System.Security.Cryptography.RSACryptoServiceProvider rsa =
                new System.Security.Cryptography.RSACryptoServiceProvider(cp);

            //復号化する
            byte[] data          = System.Convert.FromBase64String(str);
            byte[] decryptedData = rsa.Decrypt(data, false);
            return(System.Text.Encoding.UTF8.GetString(decryptedData));
        }
Exemplo n.º 12
0
        public string Descriptografar(string chavePrivada, string criptografia)
        {
            System.Security.Cryptography.CspParameters config = new System.Security.Cryptography.CspParameters {
                ProviderType = 1
            };
            System.Security.Cryptography.RSACryptoServiceProvider rsa = new System.Security.Cryptography.RSACryptoServiceProvider(config);

            byte[] chavePrivadaBytes = Convert.FromBase64String(chavePrivada);
            rsa.ImportCspBlob(chavePrivadaBytes);

            byte[] criptografiaBytes = Convert.FromBase64String(criptografia);
            byte[] mensagemBytes     = rsa.Decrypt(criptografiaBytes, false);

            string mensagem = Encoding.UTF8.GetString(mensagemBytes);

            return(mensagem);
        }
Exemplo n.º 13
0
        public static void Main(string[] args)
        {
            GostCryptoConfig.ProviderType = ProviderTypes.CryptoPro;
            var keyContainer = new System.Security.Cryptography.CspParameters();

            keyContainer.ProviderType     = GostCryptoConfig.ProviderType;
            keyContainer.KeyNumber        = 1;
            keyContainer.KeyContainerName = "REGISTRY\\\\CONTAINER_NAME";
            var ss = new System.Security.SecureString();

            foreach (char c in "12345678".ToCharArray())
            {
                ss.AppendChar(c);
            }
            keyContainer.KeyPassword = ss;
            GostCryptoConfig.KeyContainerParameters = keyContainer;

            RunWCFSmev3();
            Console.ReadKey();
        }
Exemplo n.º 14
0
        public List <string> GerarChaves()
        {
            System.Security.Cryptography.CspParameters cspParams = new System.Security.Cryptography.CspParameters {
                ProviderType = 1
            };
            System.Security.Cryptography.RSACryptoServiceProvider rsaProvider = new System.Security.Cryptography.RSACryptoServiceProvider(cspParams);

            byte[] privateBytes = rsaProvider.ExportCspBlob(true);
            byte[] publicBytes  = rsaProvider.ExportCspBlob(false);

            string privateKey = Convert.ToBase64String(privateBytes);
            string publicKey  = Convert.ToBase64String(publicBytes);

            List <string> chaves = new List <string>();

            chaves.Add(privateKey);
            chaves.Add(publicKey);

            return(chaves);
        }
Exemplo n.º 15
0
        public static string Decrypt(string stringToDecrypt, string key)
        {
            string result = null;

            if (string.IsNullOrEmpty(stringToDecrypt))
            {
                //throw new ArgumentException("An empty string value cannot be encrypted.");
            }

            if (string.IsNullOrEmpty(key))
            {
                throw new ArgumentException("Cannot decrypt using an empty key. Please supply a decryption key.");
            }

            try
            {
                System.Security.Cryptography.CspParameters cspp = new System.Security.Cryptography.CspParameters();
                cspp.KeyContainerName = key;

                System.Security.Cryptography.RSACryptoServiceProvider rsa = new System.Security.Cryptography.RSACryptoServiceProvider(cspp);
                rsa.PersistKeyInCsp = true;

                string[] decryptArray     = stringToDecrypt.Split(new string[] { "-" }, StringSplitOptions.None);
                byte[]   decryptByteArray = Array.ConvertAll <string, byte>(decryptArray, (s => Convert.ToByte(byte.Parse(s, System.Globalization.NumberStyles.HexNumber))));


                byte[] bytes = rsa.Decrypt(decryptByteArray, true);

                result = System.Text.UTF8Encoding.UTF8.GetString(bytes);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                // no need for further processing
            }

            return(result);
        }
Exemplo n.º 16
0
        /// <summary>
        /// Encryptes a string using the supplied key. Encoding is done using RSA encryption.
        /// </summary>
        /// <param name="stringToEncrypt">String that must be encrypted.</param>
        /// <param name="key">Encryptionkey.</param>
        /// <returns>A string representing a byte array separated by a minus sign.</returns>
        /// <exception cref="ArgumentException">Occurs when stringToEncrypt or key is null or empty.</exception>
        public static string Encrypt(this string stringToEncrypt, string key)
        {
            if (string.IsNullOrEmpty(stringToEncrypt))
            {
                throw new ArgumentException("An empty string value cannot be encrypted.");
            }

            if (string.IsNullOrEmpty(key))
            {
                throw new ArgumentException("Cannot encrypt using an empty key. Please supply an encryption key.");
            }

            System.Security.Cryptography.CspParameters cspp = new System.Security.Cryptography.CspParameters();
            cspp.KeyContainerName = key;

            System.Security.Cryptography.RSACryptoServiceProvider rsa = new System.Security.Cryptography.RSACryptoServiceProvider(cspp);
            rsa.PersistKeyInCsp = true;

            byte[] bytes = rsa.Encrypt(System.Text.UTF8Encoding.UTF8.GetBytes(stringToEncrypt), true);

            return(BitConverter.ToString(bytes));
        }
Exemplo n.º 17
0
 public RSACryptoServiceProvider(System.Security.Cryptography.CspParameters parameters)
 {
 }
Exemplo n.º 18
0
 public CspKeyContainerInfo(System.Security.Cryptography.CspParameters parameters)
 {
 }
Exemplo n.º 19
0
 public PasswordDeriveBytes(string strPassword, byte[] rgbSalt, string strHashName, int iterations, System.Security.Cryptography.CspParameters cspParams)
 {
 }
Exemplo n.º 20
0
 public RNGCryptoServiceProvider(System.Security.Cryptography.CspParameters cspParams)
 {
 }
Exemplo n.º 21
0
 public PasswordDeriveBytes(byte[] password, byte[] salt, System.Security.Cryptography.CspParameters cspParams)
 {
 }
Exemplo n.º 22
0
 public PasswordDeriveBytes(string strPassword, byte[] rgbSalt, System.Security.Cryptography.CspParameters cspParams)
 {
 }
 public KeyContainerPermissionAccessEntry(System.Security.Cryptography.CspParameters parameters, KeyContainerPermissionFlags flags)
 {
     Contract.Requires(parameters != null);
     Contract.Ensures(((System.Security.Cryptography.CspProviderFlags)(Int32.MinValue)) <= parameters.Flags);
     Contract.Ensures(parameters.Flags <= ((System.Security.Cryptography.CspProviderFlags)(Int32.MaxValue)));
 }
Exemplo n.º 24
0
 internal CspKeyContainerInfo(System.Security.Cryptography.CspParameters parameters, bool randomKeyContainer)
 {
 }
 public CapiContext(System.Security.Cryptography.CspParameters csp)
 {
 }
 public CapiHash(System.Security.Cryptography.CspParameters cspParams, int hashAlgorithm)
 {
 }
Exemplo n.º 27
0
 public Gost3410_2012_512CryptoServiceProvider(System.Security.Cryptography.CspParameters parameters)
 {
 }
Exemplo n.º 28
0
        public string encrypt(string publicKey, string plainText)
        {
            System.Security.Cryptography.CspParameters cspParams = null;
            System.Security.Cryptography.RSACryptoServiceProvider rsaProvider = null;
            byte[] plainBytes = null;
            byte[] encryptedBytes = null;

            string result = "";
            try
            {
                cspParams = new System.Security.Cryptography.CspParameters();
                cspParams.ProviderType = 1;
                rsaProvider = new System.Security.Cryptography.RSACryptoServiceProvider(cspParams);

                rsaProvider.FromXmlString(publicKey);

                plainBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
                encryptedBytes = rsaProvider.Encrypt(plainBytes, false);
                result = Convert.ToBase64String(encryptedBytes);
            }
            catch (Exception ex) { }
            return result;
        }
Exemplo n.º 29
0
        public void generatekeys()
        {
            System.Security.Cryptography.CspParameters cspParams = null;
            System.Security.Cryptography.RSACryptoServiceProvider rsaProvider = null;

            string publicKey = "";
            string privateKey = "";

            try
            {
                cspParams = new System.Security.Cryptography.CspParameters();
                cspParams.ProviderType = 1;
                cspParams.Flags = System.Security.Cryptography.CspProviderFlags.UseArchivableKey;
                cspParams.KeyNumber = (int)System.Security.Cryptography.KeyNumber.Exchange;
                rsaProvider = new System.Security.Cryptography.RSACryptoServiceProvider(cspParams);

                publicKey = rsaProvider.ToXmlString(false);
                privateKey = rsaProvider.ToXmlString(true);

                log("Public Key");
                log(publicKey);
                log("");
                log("Private Key");
                log(privateKey);

                tabControlCode.SelectedIndex = 2;
            }
            catch (Exception ex)
            {

            }
        }
Exemplo n.º 30
0
 public KeyContainerPermissionAccessEntry(System.Security.Cryptography.CspParameters parameters, KeyContainerPermissionFlags flags)
 {
 }
Exemplo n.º 31
0
 public DSACryptoServiceProvider(int dwKeySize, System.Security.Cryptography.CspParameters parameters)
 {
 }
 public CmsSigner(System.Security.Cryptography.CspParameters parameters)
 {
 }
Exemplo n.º 33
0
        /// <summary>
        /// Returns all of the key containers.
        /// </summary>
        private static string[] GetContainerNames()
        {
            const int BUFFSIZE = 25600;
            List<string> containernames = new List<string>();
            byte[] pbData = new byte[BUFFSIZE];
            int pcbData = BUFFSIZE;
            int enumflags = (int)PP_ENUMCONTAINERS;  // specify container enumeration functionality
            IntPtr hProv = IntPtr.Zero;
            int dwFlags = 0;

            int gotcsp = NativeMethods.CryptAcquireContextW(ref hProv, null, null, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET);

            if (gotcsp == 0)
            {
                int error = Marshal.GetLastWin32Error();
                return null;
            }
            
            /*  ----------  Get KeyContainer Names ------------- */
            dwFlags = (int)CRYPT_FIRST;  //required initalization
            StringBuilder sb = new StringBuilder(BUFFSIZE);

            int result = 0;

            do
            {
                result = NativeMethods.CryptGetProvParam(hProv, enumflags, sb, ref pcbData, dwFlags);

                if (result == 0)
                {
                    int error = Marshal.GetLastWin32Error();

                    if (ERROR_MORE_DATA != error)
                    {
                        break;
                    }
                }

                containernames.Add(sb.ToString());

                System.Security.Cryptography.CspParameters csparms = new System.Security.Cryptography.CspParameters();
                csparms.KeyContainerName = sb.ToString();
                csparms.KeyNumber = AT_KEYEXCHANGE;
                csparms.Flags = System.Security.Cryptography.CspProviderFlags.UseMachineKeyStore;

                System.Security.Cryptography.RSACryptoServiceProvider key = new System.Security.Cryptography.RSACryptoServiceProvider(csparms);

                dwFlags = CRYPT_NEXT;
            }
            while (true);

            if (hProv != IntPtr.Zero)
            {
                NativeMethods.CryptReleaseContext(hProv, 0);
            }

            return null;
        }