예제 #1
0
        private void SaveHandler(NhanVien item)
        {
            if (String.IsNullOrWhiteSpace(item.Luong) || String.IsNullOrWhiteSpace(item.MatKhau))
            {
                MessageBox.Show("Vui lòng nhập đầy đủ thông tin cho nhân viên " + item.MaNV);
                return;
            }

            var existItem = DbLib.GetOne <NhanVien>("SP_GETBYMANV_PUBLIC_ENCRYPT_NHANVIEN", new List <SqlParameter> {
                new SqlParameter {
                    ParameterName = "MaNV", Value = item.MaNV
                }
            });

            if (existItem != null)
            {
                //ko duoc cap nhat
            }
            //Insert
            else
            {
                item.PubKey = item.MaNV;
                var keyPairs = rsaCryptoService.GenerateKeys();
                KeyRepository.StoreKeyPairs(item.PubKey, keyPairs, item.MatKhau);
                item.MatKhau = item.MatKhau.GetSHA1Hash();

                item.Luong = rsaCryptoService.Encrypt(keyPairs.publicKey, item.Luong);
                DbLib.ExecuteNonQuery("SP_INS_PUBLIC_ENCRYPT_NHANVIEN", item.ToSqlParameter());
            }
        }
예제 #2
0
        public string GenerateKeys()
        {
            RSACryptography RSA = new RSACryptography();
            string          publicKey, privateKey;

            // Generate RSA key pair
            RSA.GenerateKeys(out publicKey, out privateKey);

            string plainText = "93f99709-ce56-42a9-af7e-1d72c011c2dd";// Guid.NewGuid().ToString();

            // Encrypt
            string encryptedText = RSA.Encrypt(publicKey, plainText);

            // Decrypt
            string decryptedText = RSA.Decrypt(privateKey, encryptedText);

            return(plainText + publicKey + privateKey + encryptedText + decryptedText);
            // return "<b>Token:</b> " + Server.HtmlEncode(plainText) + "<br />" + "<b>Public key:</b> " + Server.HtmlEncode(publicKey) + "<br />" + "<b>Private key:</b> " + Server.HtmlEncode(privateKey) + "<br />" + "<b>Encrypted text:</b> " + Server.HtmlEncode(encryptedText) + "<br />" + "<b>Decrypted text:</b> " + Server.HtmlEncode(decryptedText);
        }
예제 #3
0
        private async Task HandshakeAsync()
        {
            var encryptedServerKey          = rsaCryptography.Encrypt(clientSettings.PublicRsaKey, desCryptography.Key);
            var signature                   = rsaCryptography.Sign(clientSettings.PrivateRsaKey, encryptedServerKey);
            var rawEncryptedServerKeyLength = BitConverter.GetBytes(encryptedServerKey.Length);

            Array.Reverse(rawEncryptedServerKeyLength);

            var requestDataLength = encryptedServerKey.Length + signature.Length + rawEncryptedServerKeyLength.Length;
            var requestData       = new Byte[requestDataLength];

            Array.Copy(rawEncryptedServerKeyLength, 0, requestData, 0, 4);
            Array.Copy(encryptedServerKey, 0, requestData, 4, encryptedServerKey.Length);
            Array.Copy(signature, 0, requestData, 4 + encryptedServerKey.Length, signature.Length);

            var rawResponse = await SendRequestAsync(requestData, ConnectionMode.Assymmetric);

            var response = Convert.ToBase64String(rawResponse);
        }
예제 #4
0
        private void SaveBangDiemHandler(BangDiem item)
        {
            item.MaSV = (sinhVienBindingSource.Current as SinhVien).MaSV;
            var existItem = DbLib.GetOne <BangDiem>(String.Format("select MaSV, MaHP from BangDiem where MaSV = '{0}' AND MaHP = '{1}' ", item.MaSV, item.MaHP));

            if (existItem != null)
            {
                //Cập nhật điểm của sinh viên
                //DbLib.ExecuteNonQuery("SP_BANGDIEM_Update", sqlParams);
            }
            else
            {
                //Lưu điểm mới của sinh viên
                //Lấy public key của nhân viên đang đăng nhập
                var publicKey = KeyRepository.GetPublicKey(curNhanVien.PubKey);
                //Thực hiện mã hóa điểm
                item.DiemThi = rsaCryptoService.Encrypt(publicKey, item.DiemThi);
                var sqlParams = item.ToSqlParameter();
                DbLib.ExecuteNonQuery("SP_INS_ENCRYPT_BANGDIEM", sqlParams);
            }
        }
예제 #5
0
        public static void TestCommutative()
        {
            var rsa        = new RSACryptography();
            var privateKey = rsa.KeyCreator.CreatePrivateKey();
            var publicKey  = rsa.KeyCreator.CreatePublicKey(privateKey);
            var blindKey   = rsa.KeyCreator.CreateBlindKey();
            var msg        = Encoding.UTF7.GetBytes("sdlfjsf;slkfslkfddfshkfksjfhsjhfsjldfslkjfslakddsfklsjdflsdkfjsjgakdglkjjkdfhgdkjfghlskdhgklshdglsjdfhgjshfjsdhfkshfsdgfjshgfasldhalskdjalskfsdhjkfsdhfgsfkjaghsjlkfhasdohdfgskljafskhfgkflsdjfkljnzjzzmnznzzzzzzzzzzzzzz");

            Console.WriteLine(Encoding.UTF7.GetString(msg));
            var encrypted = rsa.Encrypt(publicKey, msg);
            var decrypted = rsa.Decrypt(privateKey, encrypted);

            Console.WriteLine(Encoding.UTF7.GetString(decrypted));

            var B          = Encoding.UTF8.GetBytes("B");
            var blindB     = rsa.BlindData(blindKey, publicKey, B);
            var signB      = rsa.SignData(privateKey, B);
            var signBlindB = rsa.SignData(privateKey, blindB);
            var blindSignB = rsa.BlindData(blindKey, publicKey, signB);

            Console.WriteLine($"Commutative = {signBlindB.SequenceEqual(blindSignB)}");
            Console.WriteLine($"Verify test = {rsa.VerifyData(publicKey, B, signB)}");
        }
예제 #6
0
        public string GenerateKeys(string key1, string key2, string dtcreate)
        {
            RSACryptography RSA = new RSACryptography();
            string          publicKey, privateKey, dtcreates;

            // Generate RSA key pair
            RSA.GenerateKey(out publicKey, out privateKey, out dtcreates);

            string plainText = "93f99709-ce56-42a9-af7e-1d72c011c2dd";// Guid.NewGuid().ToString();

            // Encrypt
            string encryptedText = RSA.Encrypt(publicKey, plainText);

            // Decrypt
            string decryptedText = RSA.Decrypt(privateKey, encryptedText);
            string giaima        = RSA.Decrypt(privateKey, encryptedText);

            File.WriteAllText(Application.StartupPath + "\\PrivateKey.xml", privateKey);
            File.WriteAllText(Application.StartupPath + "\\PublicKey.xml", publicKey);
            File.WriteAllText(Application.StartupPath + "\\DateKey.xml", dtcreates);
            MessageBox.Show("The Key pair created successfully at:\n" + Application.StartupPath);
            return(plainText + publicKey + privateKey + encryptedText + decryptedText);
            // return "<b>Token:</b> " + Server.HtmlEncode(plainText) + "<br />" + "<b>Public key:</b> " + Server.HtmlEncode(publicKey) + "<br />" + "<b>Private key:</b> " + Server.HtmlEncode(privateKey) + "<br />" + "<b>Encrypted text:</b> " + Server.HtmlEncode(encryptedText) + "<br />" + "<b>Decrypted text:</b> " + Server.HtmlEncode(decryptedText);
        }