コード例 #1
0
ファイル: AuthKeysRepository.cs プロジェクト: radtek/HKSJ
        /// <summary>
        /// 生成用户的公钥,返回加密UserId的加密串
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="userType"></param>
        /// <returns></returns>
        public string CreatePublicKey(int uid, AuthUserType userType)
        {
            var encryptStr = Execute(db =>
            {
                var authKey = db.AuthKeys.FirstOrDefault(u => u.UserId == uid && u.UserType == (int)userType);
                if (authKey != null)
                {
                    return(RSAHelper.EncryptString(uid.ToString(), authKey.PublicKey));
                }
                var keyPair = RSAHelper.GetRASKey();
                authKey     = new AuthKeys
                {
                    UserId     = uid,
                    PublicKey  = keyPair.PublicKey,
                    PrivateKey = keyPair.PrivateKey,
                    UserType   = (int)userType,
                    CreateTime = DateTime.Now
                };
                db.AuthKeys.Add(authKey);
                db.SaveChanges();
                return(RSAHelper.EncryptString(uid.ToString(), authKey.PublicKey));
            });

            CreateCache <AuthKeys>();
            return(encryptStr);
        }
コード例 #2
0
ファイル: Form1.cs プロジェクト: zengfanlin/RSATest
        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnS_Click(object sender, EventArgs e)
        {
            string sk  = txtSK.Text.Trim();
            string str = txtContext.Text.Trim();

            txtSed.Text = RSAHelper.EncryptString(str, sk);
        }
コード例 #3
0
        private void btnEncrypt_Click(object sender, EventArgs e)
        {
            StartTimer(true);

            if (_rsa == null)
            {
                MessageBox.Show("Lütfen Anahtar boyunu geriniz.!");
                StopTimer(true);

                return;
            }
            if (string.IsNullOrEmpty(txtEncryptInputContent.Text))
            {
                MessageBox.Show("Lütfen şifrelenecek metini giriniz.!");
                StopTimer(true);

                return;
            }
            try
            {
                txtEncryptOutContent.Text = _rsa.EncryptString(txtEncryptInputContent.Text, txtPublicKey.Text);
            }
            catch (Exception ex)
            {
                StopTimer(true);

                txtEncryptInputContent.Text = "";
                txtEncryptOutContent.Text   = "";
                MessageBox.Show("Metin içeriğini şifrelemeye çalışırken bir hata oluştu.Anahtar boyutunun metin boyutuna uygun olup olmadığını kontrol edin!!");
            }

            StopTimer(true);
        }
コード例 #4
0
        private void btnEncrypt_Click(object sender, EventArgs e)
        {
            var _val = txtEncryptString.Text;
            var _key = txtEncryptSecret.Text;

            if (string.IsNullOrEmpty(_val) || string.IsNullOrEmpty(_key))
            {
                MessageBox.Show("需要加密的明文和加密密钥均不能为空,请重试。", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                txtEncryptResult.Text = RSAHelper.EncryptString(_val, _key);
            }
        }
コード例 #5
0
        public void RsaCryptTest()
        {
            (string pubKey, string priKey) = RSAHelper.GenRSAKeyPair();
            Console.WriteLine(pubKey);
            Console.WriteLine(priKey);

            string encyypted = RSAHelper.EncryptString(pubKey, "testsetsetset");

            Console.WriteLine(encyypted);

            var decrypt = RSAHelper.DecryptString(priKey, encyypted);

            Assert.IsTrue(decrypt == "testsetsetset");
        }
コード例 #6
0
 //生成注册信息文件
 public static void getRegInfo(string filePath, string com, string usr)
 {
     try
     {
         string cpu = RSAHelper.EncryptString(getCpu);
         com = RSAHelper.EncryptString(com);
         usr = RSAHelper.EncryptString(usr);
         string regMsg = GetDisk + cpu + GetDisk + com + GetDisk + usr + GetDisk;
         EncryptHelper.FileEncrypt(filePath, regMsg);
         MessageBox.Show("生成注册信息文件成功!\n");
     }
     catch (Exception ex)
     {
         MessageBox.Show("生成注册信息文件失败!\n" + ex.Message);
     }
 }
コード例 #7
0
        private static T InvokeApi <T>(string absoluteUri, object value, HttpMethod method, string token = "")
        {
            var responseResult = new HttpResponseMessage(HttpStatusCode.OK);

            using (var httpClient = new HttpClient())
            {
                httpClient.BaseAddress = new Uri(_baseAddress);
                var appkey = RSAHelper.EncryptString(EncryptValue, PublicKey);
                httpClient.DefaultRequestHeaders.Add("appkey", appkey);
                //将激活的语言写入header
                httpClient.DefaultRequestHeaders.Add("ActiveLanguage", WebConfig.ActiveLanguage);

                if (!string.IsNullOrWhiteSpace(token))
                {
                    httpClient.DefaultRequestHeaders.Add("token", token);//TODO uid type 添加
                }
                if (method == HttpMethod.Post)
                {
                    responseResult = PerformActionSafe(() => (httpClient.PostAsJsonAsync(absoluteUri, value)).Result);
                }
                else if (method == HttpMethod.Get)
                {
                    responseResult = PerformActionSafe(() => (httpClient.GetAsync(absoluteUri)).Result);
                }
            }
            try
            {
                if (!responseResult.IsSuccessStatusCode)
                {
                    return(default(T));
                }
                if (typeof(T).Name == "String")
                {
                    return((T)Convert.ChangeType(responseResult.Content.ReadAsStringAsync().Result, typeof(T)));
                }
                return(responseResult.Content.ReadAsAsync <T>().Result);
            }
            catch
            {
                return(default(T));
            }
        }
コード例 #8
0
        public static string Create(string urlpath, object querystring, object form, string publickey)
        {
            List <string>       paramlist   = new List <string>();
            List <string>       signparam   = new List <string>();
            NameValueCollection QueryString = new NameValueCollection();
            NameValueCollection Form        = new NameValueCollection();

            if (querystring != null)
            {
                foreach (PropertyDescriptor propertyDescriptor in TypeDescriptor.GetProperties(querystring))
                {
                    object obj = propertyDescriptor.GetValue(querystring);
                    paramlist.Add(propertyDescriptor.Name + "=" + HttpUtility.UrlEncode(obj.ToString()));
                    QueryString.Add(propertyDescriptor.Name, obj.ToString());
                }
            }
            if (form != null)
            {
                foreach (PropertyDescriptor propertyDescriptor in TypeDescriptor.GetProperties(form))
                {
                    object obj = propertyDescriptor.GetValue(form);
                    paramlist.Add(propertyDescriptor.Name + "=" + HttpUtility.UrlEncode(obj.ToString()));
                    Form.Add(propertyDescriptor.Name, obj.ToString());
                }
            }
            NameValueCollection parameters = new NameValueCollection()
            {
                Form,
                QueryString
            };

            foreach (var item in parameters.AllKeys.OrderBy(k => k))
            {
                signparam.Add(item + "=" + parameters[item]);
            }

            string digest = Sha1.Compute(string.Join("&", signparam));

            paramlist.Add("sign=" + RSAHelper.EncryptString(digest, publickey));
            return(urlpath + (paramlist.Count > 0 ? "?" + string.Join("&", paramlist) : ""));
        }
コード例 #9
0
 private void btnEncrypt_Click(object sender, EventArgs e)
 {
     if (_rsa == null)
     {
         MessageBox.Show("Please, generate a key pair first!");
         return;
     }
     if (string.IsNullOrEmpty(txtContent.Text))
     {
         MessageBox.Show("Please, inform some content!");
         return;
     }
     try
     {
         txtResult.Text = _rsa.EncryptString(txtContent.Text, txtPublicKey.Text);
     }
     catch (Exception ex)
     {
         txtResult.Text = "";
         MessageBox.Show("There was an error trying to encrypt the text content. Check if the key size is enought to the text size!");
     }
 }
コード例 #10
0
 //生成注册密钥文件
 private void BtnOK_Click(object sender, RoutedEventArgs e)
 {
     try
     {
         //加密处理生成密钥文件*.regkey
         List <string> regList = new List <string>();
         regList.Add(RSAHelper.EncryptString(cpu + disk, privateKey));
         regList.Add(RSAHelper.EncryptString(com, privateKey));
         regList.Add(RSAHelper.EncryptString(usr, privateKey));
         foreach (string confKey in RegConfig.config.Keys)
         {
             string key = RSAHelper.EncryptString(confKey, privateKey);
             string val = RSAHelper.EncryptString(RegConfig.config[confKey], privateKey);
             regList.Add(key + disk + val);
         }
         string regMsg = string.Join(cpu, regList);
         EncryptHelper.FileEncrypt(txtRegKey.Text, regMsg);
         MessageBox.Show("生成注册密钥文件成功!\n");
     }
     catch (Exception ex)
     {
         MessageBox.Show("生成注册密钥文件失败!\n" + ex.Message);
     }
 }