Ejemplo n.º 1
0
        public static void TestProtocol()
        {
            var rsa       = new RSACryptography();
            var validator = new Validator(rsa);

            validator.CreateKeys();

            var agency = new Agency(rsa, validator.PublicKey.GetChangeableCopy(), 10);

            var elector = new Elector(rsa, validator.PublicKey.GetChangeableCopy());

            elector.CreateNewKeys();

            var blinded       = elector.CreateBlindedMessage(0);
            var blindedSigned = elector.CreateBlindedSignedMessage(0);



            if (validator.VerifyBulletin(blindedSigned, blinded, elector.PublicSignKey.GetChangeableCopy()))
            {
                var signedByValidator       = validator.SignBulletin(blinded);
                var signedValidatorUnBlind  = elector.RemoveBlindEncryption(signedByValidator);
                var encryptedBulletin       = elector.GetEncryptedBulletin(0);
                var signedEncryptedBulletin = elector.GetSignedEncryptedBulletin(0);

                agency.AddBulletin(signedValidatorUnBlind, encryptedBulletin, signedEncryptedBulletin, elector.PublicSignKey.GetChangeableCopy(), 1);
            }
        }
Ejemplo n.º 2
0
        public void Process(byte[] buffer, Connection connection)
        {
            var msg = new ByteBuffer(buffer);
            int length;

            length = msg.ReadInt32();
            var key = msg.ReadBytes(length);

            length = msg.ReadInt32();
            var iv = msg.ReadBytes(length);

            var rsa  = connection.RSAKey.GetKey();
            var _key = RSACryptography.RSADecrypt(key, rsa.GetPrivateKey(), false);
            var _iv  = RSACryptography.RSADecrypt(iv, rsa.GetPrivateKey(), false);

            connection.AesKey.SetClientKey(_key);
            connection.AesKey.SetClientIv(_iv);

            // Encria e envia de volta para o cliente.
            _key = connection.AesKey.GetKey();
            _iv  = connection.AesKey.GetIv();

            key = RSACryptography.RSAEncrypt(_key, rsa.GetClientPublicKey(), false);
            iv  = RSACryptography.RSAEncrypt(_iv, rsa.GetClientPublicKey(), false);

            var aes = new SpAESKey(key, iv);

            aes.Send(connection, false);
        }
Ejemplo n.º 3
0
        public string EncryptData(string data, Algorithm.StringTransformationFormat inputStringEncode, Algorithm.Cryptography cryptographyAlgorithm, Algorithm.StringEncodeFormat outputStringEncode)
        {
            string encryptText = string.Empty;

            if (!string.IsNullOrEmpty(data))
            {
                byte[] cipherText = null;
                try
                {
                    BinaryEncodeHelper encodeHelper = new BinaryEncodeHelper(Algorithm.StringTransformationFormat.UTF8);
                    byte[]             dataBinary   = encodeHelper.Decode(data);

                    switch (outputStringEncode)
                    {
                    case Algorithm.StringEncodeFormat.Base32:
                    {
                        encodeHelper.SetEncodeFormat(Algorithm.StringTransformationFormat.Base32);
                        break;
                    }

                    case Algorithm.StringEncodeFormat.Base64:
                    {
                        encodeHelper.SetEncodeFormat(Algorithm.StringTransformationFormat.Base64);
                        break;
                    }
                    }
                    switch (cryptographyAlgorithm)
                    {
                    case Algorithm.Cryptography.AES:
                    {
                        AESCryptography aes = new AESCryptography();
                        cipherText = aes.EncryptData(dataBinary);
                        m_Password = encodeHelper.Encode(aes.Password);
                        m_Salt     = encodeHelper.Encode(aes.Salt);
                        break;
                    }

                    case Algorithm.Cryptography.RSA:
                    {
                        RSACryptography rsa = new RSACryptography();
                        cipherText = rsa.EncryptData(dataBinary);
                        BinaryEncodeHelper encodeLittleHelper = new BinaryEncodeHelper(Algorithm.StringTransformationFormat.UTF8);
                        m_Password = encodeHelper.Encode(encodeLittleHelper.Decode(rsa.PublicKey));
                        break;
                    }
                    }

                    if (cipherText != null)
                    {
                        encryptText = encodeHelper.Encode(cipherText);
                    }
                }
                catch (Exception)
                {
                    throw;
                }
            }
            return(encryptText);
        }
Ejemplo n.º 4
0
        private void Btn_GenerateKeys_Click(object sender, EventArgs e)
        {
            Rsa = new RSACryptography(Enums.RSAKeySize.Key2048);
            var keys = Rsa.GenerateKeys();

            Txt_PublicKey.Text  = keys.PublicKey;
            Txt_PrivateKey.Text = keys.PrivateKey;
        }
Ejemplo n.º 5
0
 public frmBangDiem(NhanVien nv, string privateKey)
 {
     InitializeComponent();
     DbLib                 = new SqlDatabase();
     this.curNhanVien      = nv;
     this.privateKey       = privateKey;
     this.rsaCryptoService = new RSACryptography();
 }
Ejemplo n.º 6
0
        private void button1_Click(object sender, EventArgs e)
        {
            RSACryptography rsa    = new RSACryptography();
            var             rsaKey = rsa.GenerateKeys();

            KeyRepository.StorePublicKey(textBox1.Text, rsaKey.publicKey);
            //KeyRepository.StorePrivateKey(textBox1.Text, rsaKey.privateKey);
        }
Ejemplo n.º 7
0
        public frmNhanVien(NhanVien nv, string password)
        {
            InitializeComponent();
            DbLib = new SqlDatabase();

            this.nhanVienDangNhap = nv;
            this.password         = password;
            this.rsaCryptoService = new RSACryptography();
        }
Ejemplo n.º 8
0
 public void ClearRSAKeyContainer()
 {
     try
     {
         RSACryptography rsa = new RSACryptography();
         rsa.ClearKeyContainer();
     }
     catch (Exception)
     {
         throw;
     }
 }
        public AgencyContext(ServerModel server)
        {
            CryptographyProvider = new RSACryptography();


            Server = server;

            MainFactory = new MainFactory();
            MainFactory.RegisterTypes();

            RegisteredUsers = new RegisteredUsers();
            RegisteredUsers.RegisterUsers();
        }
Ejemplo n.º 10
0
        public Context(Dictionary <string, object> mainConfig)
        {
            if (_instance != null)
            {
                throw new Exception("More than one singletone");
            }

            fastJSON.JSON.Parameters.UseEscapedUnicode = true;
            _instance            = this;
            CryptographyProvider = new RSACryptography();
            NetworkManager       = new NetworkManager(mainConfig);
            NetworkManager.Activate();
            MainFactory = new MainFactory();
            MainFactory.RegisterTypes();
        }
Ejemplo n.º 11
0
        public static void TestVerify()
        {
            var rsa         = new RSACryptography();
            var privateKey1 = rsa.KeyCreator.CreatePrivateKey();
            var publicKey1  = rsa.KeyCreator.CreatePublicKey(privateKey1);

            var privateKey2 = rsa.KeyCreator.CreatePrivateKey();
            var publicKey2  = rsa.KeyCreator.CreatePublicKey(privateKey2);

            var B      = Encoding.UTF8.GetBytes("Bababa");
            var signed = rsa.SignData(privateKey1, B);


            Console.WriteLine(rsa.VerifyData(privateKey1, B, signed));
        }
Ejemplo n.º 12
0
        public void Process(byte[] buffer, Connection connection)
        {
            connection.RSAKey.SetClientPublicKey(buffer);
            // Quando receber a chave. Encrypta e envia.

            var _key = connection.AesKey.GetKey();
            var _iv  = connection.AesKey.GetIv();

            var rsa = connection.RSAKey.GetKey();
            var key = RSACryptography.RSAEncrypt(_key, rsa.GetClientPublicKey(), false);
            var iv  = RSACryptography.RSAEncrypt(_iv, rsa.GetClientPublicKey(), false);

            var aes = new CpAESKey(key, iv);

            aes.Send(connection, false);
        }
Ejemplo n.º 13
0
        /// <summary>
        /// Initializes an instance of the SirenaClient class.
        /// </summary>
        /// <param name="clientSettings">The settings that will be applied during communication.</param>
        public SirenaClient(SirenaClientSettings clientSettings)
        {
            if (clientSettings == null)
            {
                throw new ArgumentNullException("clientSettings can not be null");
            }

            Id = Guid.NewGuid();

            desCryptography = DESCryptography.Instance;
            rsaCryptography = new RSACryptography();

            this.clientSettings = clientSettings;

            client = new TcpClient();
        }
Ejemplo n.º 14
0
        public static void TestBlind()
        {
            var rsa         = new RSACryptography();
            var blindKey    = rsa.KeyCreator.CreateBlindKey();
            var privateKey  = rsa.KeyCreator.CreatePrivateKey();
            var privateKey1 = rsa.KeyCreator.CreatePrivateKey();
            var publicKey   = rsa.KeyCreator.CreatePublicKey(privateKey);
            var publicKey1  = rsa.KeyCreator.CreatePublicKey(privateKey1);


            var B             = Encoding.UTF8.GetBytes("B");
            var blinded       = rsa.BlindData(blindKey, publicKey, B);
            var blindSigned   = rsa.SignData(privateKey, blinded);
            var unblindSigned = rsa.UnBlindData(blindKey, publicKey, blindSigned);

            Console.WriteLine($"Verified? = {rsa.VerifyData(publicKey, B, unblindSigned)}");
        }
Ejemplo n.º 15
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);
        }
Ejemplo n.º 16
0
        public void Process(byte[] buffer, Connection connection)
        {
            var msg = new ByteBuffer(buffer);
            int length;

            length = msg.ReadInt32();
            var key = msg.ReadBytes(length);

            length = msg.ReadInt32();
            var iv = msg.ReadBytes(length);

            var rsa  = connection.RSAKey;
            var keys = rsa.GetKey();
            var _key = RSACryptography.RSADecrypt(key, keys.GetPrivateKey(), false);
            var _iv  = RSACryptography.RSADecrypt(iv, keys.GetPrivateKey(), false);

            connection.AesKey.SetClientKey(_key);
            connection.AesKey.SetClientIv(_iv);

            Global.Socket.HandShake = true;
        }
Ejemplo n.º 17
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)}");
        }
Ejemplo n.º 18
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);
        }
Ejemplo n.º 19
0
 public frmNhanVien()
 {
     InitializeComponent();
     DbLib = new SqlDatabase();
     this.rsaCryptoService = new RSACryptography();
 }
Ejemplo n.º 20
0
        public string DecryptData(string encryptText, Algorithm.StringEncodeFormat inputStringEncode, Algorithm.Cryptography cryptographyAlgorithm, Algorithm.StringTransformationFormat outputStringEncode)
        {
            string data = string.Empty;

            if (!string.IsNullOrEmpty(encryptText))
            {
                byte[] cipherText = null;
                byte[] password   = null;
                byte[] salt       = null;
                try
                {
                    BinaryEncodeHelper encodeHelper = new BinaryEncodeHelper(Algorithm.StringTransformationFormat.Base64);
                    switch (inputStringEncode)
                    {
                    case Algorithm.StringEncodeFormat.Base32:
                    {
                        encodeHelper.SetEncodeFormat(Algorithm.StringTransformationFormat.Base32);
                        break;
                    }

                    case Algorithm.StringEncodeFormat.Base64:
                    {
                        encodeHelper.SetEncodeFormat(Algorithm.StringTransformationFormat.Base64);
                        break;
                    }
                    }
                    cipherText = encodeHelper.Decode(encryptText);
                    if (!string.IsNullOrEmpty(m_Password))
                    {
                        password = encodeHelper.Decode(m_Password);
                    }
                    if (!string.IsNullOrEmpty(m_Salt))
                    {
                        salt = encodeHelper.Decode(m_Salt);
                    }

                    byte[] text = null;
                    switch (cryptographyAlgorithm)
                    {
                    case Algorithm.Cryptography.AES:
                    {
                        if (cipherText != null && password != null && salt != null)
                        {
                            AESCryptography aes = new AESCryptography(password, salt);
                            text = aes.DecryptData(cipherText);
                        }
                        break;
                    }

                    case Algorithm.Cryptography.RSA:
                    {
                        RSACryptography rsa = new RSACryptography();
                        text = rsa.DecryptData(cipherText);
                        break;
                    }
                    }
                    if (text != null)
                    {
                        encodeHelper.SetEncodeFormat(outputStringEncode);
                        data = encodeHelper.Encode(text);
                    }
                }
                catch (Exception)
                {
                    throw;
                }
            }
            return(data);
        }
Ejemplo n.º 21
0
 private void Frm_RSA_Load(object sender, EventArgs e)
 {
     Rsa = new RSACryptography(Enums.RSAKeySize.Key2048);
 }