Exemplo n.º 1
0
        /// <summary>
        /// Generate keys for RSA algorithm: private and public keys
        /// </summary>
        /// <param name="sender">Sender of Click event</param>
        /// <param name="e">Args for Click event</param>
        private void btRSAClick(object sender, EventArgs e)
        {
            // Init vars and take key size
            var keyPublic  = "";
            var keyPrivate = "";
            var keySize    = keySizes[cbKeySize.SelectedIndex];

            // Generate keys based on desired size
            CryptRSA.GenerateKeys(keySize, out keyPublic, out keyPrivate);
            // Show them in text fields (adjust from XML to RAW text)
            txtKey.Text = keyPrivate.Replace("><", ">\r\n<");
            txtIV.Text  = keyPublic.Replace("><", ">\r\n<");
        }
Exemplo n.º 2
0
        public GameConnection(Session session)
        {
            _session    = session;
            Subscribers = new List <IDisposable>();

            Characters = new Dictionary <uint, Character>();
            Houses     = new Dictionary <uint, House>();
            Payment    = new AccountPayment(this);

            //шифрация пакетов
            CryptRsa       = new CryptRSA();
            DecryptCs      = new DecryptCs();
            EncryptSc      = new EncryptSc();
            EncryptSc.MNum = 0;
        }
Exemplo n.º 3
0
        /// <summary>
        /// Actions to do when user clicks the 'Go' button
        /// </summary>
        /// <param name="sender">Sender of Click event</param>
        /// <param name="e">Arguments for Click event</param>
        private void btGoClick(object sender, EventArgs e)
        {
            // No text to crypt/decrypt? finish
            if (txtText.Text == "")
            {
                return;
            }
            // According to selected crypt algorithm...
            switch (cbType.SelectedIndex)
            {
            case 0:     // CryptStream3DES
                // No key and initializer values? finish
                if ((txtKey.Text == "") || (txtIV.Text == ""))
                {
                    return;
                }
                // If we are encrypting, use according texts for encrypt and decrypt
                if (tbSide.Value == 0)
                {
                    txtCrypt.Text   = CryptStream3DES.Encrypt(txtText.Text, txtKey.Text, txtIV.Text);
                    txtDecrypt.Text = CryptStream3DES.Decrypt(txtCrypt.Text, txtKey.Text, txtIV.Text);
                }
                else
                {
                    // If we are decrypting, take the inverse text values
                    txtCrypt.Text   = CryptStream3DES.Decrypt(txtText.Text, txtKey.Text, txtIV.Text);
                    txtDecrypt.Text = CryptStream3DES.Encrypt(txtCrypt.Text, txtKey.Text, txtIV.Text);
                }
                break;

            case 1:     // Crypt3DES
                // No key? finish
                if (txtKey.Text == "")
                {
                    return;
                }
                // If we are encrypting, use according text for encrypt and decrypt
                if (tbSide.Value == 0)
                {
                    txtCrypt.Text   = Crypt3DES.Encrypt(txtText.Text, txtKey.Text);
                    txtDecrypt.Text = Crypt3DES.Decrypt(txtCrypt.Text, txtKey.Text);
                }
                else
                {
                    // If we are decrypting, use the inverse text values
                    txtCrypt.Text   = Crypt3DES.Decrypt(txtText.Text, txtKey.Text);
                    txtDecrypt.Text = Crypt3DES.Encrypt(txtCrypt.Text, txtKey.Text);
                }
                break;

            case 2:     // CryptAES
                // No key and initializer? finish
                if ((txtKey.Text == "") || (txtIV.Text == ""))
                {
                    return;
                }
                // Use right texts to encrypt/decrypt, based on our setup (also use provided key and salt)
                if (tbSide.Value == 0)
                {
                    txtCrypt.Text   = CryptAES.Encrypt(txtText.Text, txtKey.Text, txtIV.Text);
                    txtDecrypt.Text = CryptAES.Decrypt(txtCrypt.Text, txtKey.Text, txtIV.Text);
                }
                else
                {
                    txtCrypt.Text   = CryptAES.Decrypt(txtText.Text, txtKey.Text, txtIV.Text);
                    txtDecrypt.Text = CryptAES.Encrypt(txtCrypt.Text, txtKey.Text, txtIV.Text);
                }
                break;

            case 3:     // UUCode
                // Apply encoding/decoding to right texts according to setup
                if (tbSide.Value == 0)
                {
                    txtCrypt.Text   = UUCoder.Encode(txtText.Text);
                    txtDecrypt.Text = UUCoder.Decode(txtCrypt.Text);
                }
                else
                {
                    txtCrypt.Text   = UUCoder.Decode(txtText.Text);
                    txtDecrypt.Text = UUCoder.Encode(txtCrypt.Text);
                }
                break;

            case 4:     // Mime/Base64
                // Apply encoding/decoding to right texts according to setup
                if (tbSide.Value == 0)
                {
                    txtCrypt.Text   = Base64.EncodeString(txtText.Text);
                    txtDecrypt.Text = Base64.DecodeString(txtCrypt.Text);
                }
                else
                {
                    txtCrypt.Text   = Base64.DecodeString(txtText.Text);
                    txtDecrypt.Text = Base64.EncodeString(txtCrypt.Text);
                }
                break;

            case 5:     // Adler32
                // Calculate hash for given text...
                txtCrypt.Text = string.Format("{0:X}", Adler32.AdlerHash(txtText.Text));
                break;

            case 6:     // CRC16
                // Calculate hash for given text...
                txtCrypt.Text = string.Format("{0:X}", CRC16.Hash(txtText.Text));
                break;

            case 7:     // CRC32
                // Calculate hash for given text...
                txtCrypt.Text = string.Format("{0:X}", CRC32.Hash(txtText.Text));
                break;

            case 8:     // CRC64
                // Calculate hash for given text...
                txtCrypt.Text = string.Format("{0:X}", CRC64.Hash(txtText.Text));
                break;

            case 9:     // Salsa20
                // No key and initializer? finish
                if ((txtKey.Text == "") || (txtIV.Text == ""))
                {
                    return;
                }
                // Use cryptor to encrypt/decrypt data, according to setup
                using (var salsa = new CryptSalsa(txtKey.Text, txtIV.Text))
                {
                    // Encrypt -> Decrypt
                    if (tbSide.Value == 0)
                    {
                        txtCrypt.Text   = salsa.Encrypt(txtText.Text);
                        txtDecrypt.Text = salsa.Decrypt(txtCrypt.Text);
                    }
                    else
                    {
                        // Or Decrypt -> Encrypt
                        txtCrypt.Text   = salsa.Decrypt(txtText.Text);
                        txtDecrypt.Text = salsa.Encrypt(txtCrypt.Text);
                    }
                }
                break;

            case 10:     // ChaCha20
                // No key/initializer? finish
                if ((txtKey.Text == "") || (txtIV.Text == ""))
                {
                    return;
                }
                // Use cryptor to encrypt/decrypt data, according to setup
                using (var chacha = new CryptChaCha20(txtKey.Text, txtIV.Text))
                {
                    // Encrypt -> Decrypt
                    if (tbSide.Value == 0)
                    {
                        txtCrypt.Text   = chacha.Encrypt(txtText.Text);
                        txtDecrypt.Text = chacha.Decrypt(txtCrypt.Text);
                    }
                    else
                    {
                        // Or Decrypt -> Encrypt
                        txtCrypt.Text   = chacha.Decrypt(txtText.Text);
                        txtDecrypt.Text = chacha.Encrypt(txtCrypt.Text);
                    }
                }
                break;

            case 11:     // RSA
                // if no key/initializer value, finish
                if ((txtKey.Text == "") || (txtIV.Text == ""))
                {
                    return;
                }
                using (var rsa = new CryptRSA(txtKey.Text, keySizes[cbKeySize.SelectedIndex]))
                {
                    // Use private key?
                    if (cbPrivateKey.Checked)
                    {
                        // Use cryptor to encrypt/decrypt data, according to setup
                        if (tbSide.Value == 0)
                        {
                            // Encrypt -> Decrypt
                            txtCrypt.Text   = rsa.PrivateEncrypt(txtText.Text);
                            txtDecrypt.Text = rsa.PublicDecrypt(txtCrypt.Text);
                        }
                        else
                        {
                            // Or Decrypt -> Encrypt
                            txtCrypt.Text   = rsa.PublicDecrypt(txtText.Text);
                            txtDecrypt.Text = rsa.PrivateEncrypt(txtCrypt.Text);
                        }
                    }
                    else
                    {
                        // Nope, use public key...
                        // Use cryptor to encrypt/decrypt data, according to setup
                        if (tbSide.Value == 0)
                        {
                            // Encrypt -> Decrypt
                            txtCrypt.Text   = rsa.PublicEncrypt(txtText.Text);
                            txtDecrypt.Text = rsa.PrivateDecrypt(txtCrypt.Text);
                        }
                        else
                        {
                            // Or Decrypt -> Encrypt
                            txtCrypt.Text   = rsa.PrivateDecrypt(txtText.Text);
                            txtDecrypt.Text = rsa.PublicEncrypt(txtCrypt.Text);
                        }
                    }
                }
                break;

            case 12:     // Blowfish
                // No key? finish
                if (txtKey.Text == "")
                {
                    return;
                }
                // Use cryptor to encrypt/decrypt data, according to setup
                using (var blowfish = new CryptBlowfish(txtKey.Text))
                {
                    // Encrypt -> Decrypt
                    if (tbSide.Value == 0)
                    {
                        txtCrypt.Text   = blowfish.Encrypt(txtText.Text);
                        txtDecrypt.Text = blowfish.Decrypt(txtCrypt.Text);
                    }
                    else
                    {
                        // Or Decrypt -> Encrypt
                        txtCrypt.Text   = blowfish.Decrypt(txtText.Text);
                        txtDecrypt.Text = blowfish.Encrypt(txtCrypt.Text);
                    }
                }
                break;
            }
        }