Ejemplo n.º 1
0
 public static string DesDecrypt(this string value, string password, string iv)
 {
     return
         (Encoding.UTF8.GetString(DesCipher.DesDecrypt(Base64.DecodeToArray(value), Encoding.ASCII.GetBytes(password),
                                                       Encoding.ASCII.GetBytes(iv))));
 }
Ejemplo n.º 2
0
 public static string DesEncrypt(this string value, string password, string iv)
 {
     return
         (Base64.Encode(DesCipher.DesEncrypt(Encoding.UTF8.GetChars(Encoding.UTF8.GetBytes(value)),
                                             Encoding.ASCII.GetBytes(password), Encoding.ASCII.GetBytes(iv))));
 }
Ejemplo n.º 3
0
        private void Encrypt()
        {
            rtb_CipherText.TextChanged -= Rtb_CipherText_TextChanged;
            rtb_CipherText.Text         = String.Empty;
            try
            {
                switch (cipherMode)
                {
                case CipherMode.Base64:
                    rtb_CipherText.Text = rtb_PlainText.Text.Base64Encode();
                    break;

                case CipherMode.Caesar:
                    rtb_CipherText.Text = rtb_PlainText.Text.CaesarEncrypt((int)nud_Key.Value);
                    break;

                case CipherMode.Des:
                    if (chk_ByteArrayValues.Checked)
                    {
                        var encryptedText  = new StringBuilder();
                        var encryptedBytes = DesCipher.DesEncrypt(Encoding.UTF8.GetChars(Encoding.UTF8.GetBytes(rtb_PlainText.Text)), Encoding.ASCII.GetBytes(tb_Password.Text), Encoding.ASCII.GetBytes(tb_IV.Text));
                        foreach (var encryptedByte in encryptedBytes)
                        {
                            encryptedText.AppendFormat("[{0}]", encryptedByte);
                        }
                        rtb_CipherText.Text = encryptedText.ToString();
                    }
                    else
                    {
                        rtb_CipherText.Text = rtb_PlainText.Text.DesEncrypt(tb_Password.Text, tb_IV.Text);
                    }
                    break;

                case CipherMode.Rotate:
                    rtb_CipherText.Text = rtb_PlainText.Text.RotateEncrypt((int)nud_Key.Value);
                    break;

                case CipherMode.TripleDes:
                    if (chk_ByteArrayValues.Checked)
                    {
                        if (chk_KeyAndIVInByteArrayFormat.Checked)
                        {
                        }
                        else
                        {
                            var encryptedText  = new StringBuilder();
                            var encryptedBytes = TripleDesCipher.TripleDesEncrypt(Encoding.UTF8.GetChars(Encoding.UTF8.GetBytes(rtb_PlainText.Text)), Encoding.ASCII.GetBytes(tb_Password.Text), Encoding.ASCII.GetBytes(tb_IV.Text));
                            foreach (var encryptedByte in encryptedBytes)
                            {
                                encryptedText.AppendFormat("[{0}]", encryptedByte);
                            }
                            rtb_CipherText.Text = encryptedText.ToString();
                        }
                    }
                    else
                    {
                        if (chk_KeyAndIVInByteArrayFormat.Checked)
                        {
                        }
                        else
                        {
                            rtb_CipherText.Text = rtb_PlainText.Text.TripleDesEncrypt(tb_Password.Text, tb_IV.Text);
                        }
                    }
                    break;

                case CipherMode.Xor:
                    rtb_CipherText.Text = rtb_PlainText.Text.XorCrypt(tb_Password.Text);
                    break;
                }
            }
            catch (Exception ex)
            {
                lbl_Error.Text = ex.Message;
            }
            finally
            {
                rtb_CipherText.TextChanged += Rtb_CipherText_TextChanged;
            }
        }
Ejemplo n.º 4
0
        public static string DesDecrypt(this string value, string password)
        {
            var passBytes = Encoding.ASCII.GetBytes(password);

            return(Encoding.UTF8.GetString(DesCipher.DesDecrypt(Base64.DecodeToArray(value), passBytes, passBytes)));
        }
Ejemplo n.º 5
0
        private void Decrypt()
        {
            rtb_PlainText.TextChanged -= Rtb_PlainText_TextChanged;
            rtb_PlainText.Text         = String.Empty;
            try
            {
                switch (cipherMode)
                {
                case CipherMode.Base64:
                    rtb_PlainText.Text = rtb_CipherText.Text.Base64Decode();
                    break;

                case CipherMode.Caesar:
                    rtb_PlainText.Text = rtb_CipherText.Text.CaesarDecrypt((int)nud_Key.Value);
                    break;

                case CipherMode.Des:
                    if (chk_ByteArrayValues.Checked)
                    {
                        var byteStrings = rtb_CipherText.Text.Replace("[", "").Split(']');
                        var bytes       = new byte[byteStrings.Length - 1];
                        for (var i = 0; i < bytes.Length; i++)
                        {
                            bytes[i] = Convert.ToByte(byteStrings[i]);
                        }
                        rtb_PlainText.Text = Encoding.UTF8.GetString(DesCipher.DesDecrypt(bytes, Encoding.ASCII.GetBytes(tb_Password.Text), Encoding.ASCII.GetBytes(tb_IV.Text)));
                    }
                    else
                    {
                        rtb_PlainText.Text = rtb_CipherText.Text.DesDecrypt(tb_Password.Text, tb_IV.Text);
                    }
                    break;

                case CipherMode.Rotate:
                    rtb_PlainText.Text = rtb_CipherText.Text.RotateDecrypt((int)nud_Key.Value);
                    break;

                case CipherMode.TripleDes:
                    if (chk_ByteArrayValues.Checked)
                    {
                        var bytes = rtb_CipherText.Text.StringToByteArray();
                        if (chk_KeyAndIVInByteArrayFormat.Checked)
                        {
                            var keyBytes = tb_Password.Text.StringToByteArray();
                            var ivBytes  = tb_IV.Text.StringToByteArray();
                            rtb_PlainText.Text = Encoding.UTF8.GetString(TripleDesCipher.TripleDesDecrypt(bytes, keyBytes, ivBytes));
                        }
                        else
                        {
                            rtb_PlainText.Text = Encoding.UTF8.GetString(TripleDesCipher.TripleDesDecrypt(bytes, Encoding.ASCII.GetBytes(tb_Password.Text), Encoding.ASCII.GetBytes(tb_IV.Text)));
                        }
                    }
                    else
                    {
                        if (chk_KeyAndIVInByteArrayFormat.Checked)
                        {
                            var keyBytes = tb_Password.Text.StringToByteArray();
                            var ivBytes  = tb_IV.Text.StringToByteArray();
                            rtb_PlainText.Text = rtb_CipherText.Text.TripleDesDecrypt(keyBytes, ivBytes);
                        }
                        else
                        {
                            rtb_PlainText.Text = rtb_CipherText.Text.TripleDesDecrypt(tb_Password.Text, tb_IV.Text);
                        }
                    }
                    break;

                case CipherMode.Xor:
                    rtb_PlainText.Text = rtb_CipherText.Text.XorCrypt(tb_Password.Text);
                    break;
                }
            }
            catch (Exception ex)
            {
                rtb_PlainText.Text = ex.Message;
            }
            finally
            {
                rtb_PlainText.TextChanged += Rtb_PlainText_TextChanged;
            }
        }