/// <summary>
        /// 解密函数
        /// </summary>
        /// <param name="str">解密的密文</param>
        /// <param name="prssword">解密后的明文</param>
        /// <returns></returns>
        public string Decrypt(string str, string prssword)
        {
            str = (str.Length % 32 != 0 ? str.PadRight(str.Length + (32 - (str.Length % 32)), '\0') : str);
            KEY = prssword;                                      
            KeySteup();                                            
            uint A, B, C, D, T, U, temp; T = U = 0;                
            A = B = C = D = 0;
            temp = 0;
            byte[] input = Enc_default.GetBytes(str);            
            byte[] output = new Byte[input.Length];            for (int k = 0; k < 4; k++)
            {
                A += ((uint)input[2 * k] & 0xFF) << (8 * k);
                B += ((uint)input[2 * k + 8] & 0xFF) << (8 * k);
                C += ((uint)input[2 * k + 16] & 0xFF) << (8 * k);
                D += ((uint)input[2 * k + 24] & 0xFF) << (8 * k);            }            C -= m_nKeyExpandBox[2 * r + 3];
            A -= m_nKeyExpandBox[2 * r + 2];            for (int i = 1; i <= m_nChipherlen; i++)
            {
                temp = D;
                D = C;
                C = B;
                B = A;
                A = temp;
                U = ROTL(D * (2 * D + 1), 5, m_nWord);
                T = ROTL(B * (2 * B + 1), 5, m_nWord);
                C = ROTR(C - m_nKeyExpandBox[2 * (m_nChipherlen - i) + 3], T, m_nWord) ^ U;
                A = ROTR(A - m_nKeyExpandBox[2 * (m_nChipherlen - i) + 2], U, m_nWord) ^ T;
            }
            D -= m_nKeyExpandBox[1];
            B -= m_nKeyExpandBox[0];
            for (int k = 0; k < 4; k++)
            {
                output[2 * k] = (byte)((A >> (8 * k)) & 0xFF);
                output[2 * k + 8] = (byte)((B >> (8 * k)) & 0xFF);
                output[2 * k + 16] = (byte)((C >> (8 * k)) & 0xFF);
                output[2 * k + 24] = (byte)((D >> (8 * k)) & 0xFF);
            }
            char[] outarrchar = new char[Enc_default.GetCharCount(output, 0, output.Length)];
            Enc_default.GetChars(output, 0, output.Length, outarrchar, 0);
            this.m_sCryptedText = new string(outarrchar, 0, outarrchar.Length);
            byte[] Output1 = Enc_default.GetBytes(this.m_sCryptedText);  
            return m_sCryptedText;
        }
    }
}
#endregion实现程序见后!

解决方案 »

  1.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace WindowsApplication1
    {
        public partial class Form1 : Form
        {
            private int round;
            public Form1()
            {
                InitializeComponent();
            }        private void button1_Click(object sender, EventArgs e)
            {
                if (this.textBox1.Text == "") { MessageBox.Show("请输入加密的明文"); this.textBox1.Focus(); return; }
                if (this.textBox3.Text == "") { MessageBox.Show("请输入加密的密钥"); this.textBox3.Focus(); return; }
                RC6 RC = new RC6(round);
                //如果手动输入加密向量RC.IV = -1;请使用RC._IV():函数进行验证。
                
                textBox2.Text = RC.Encrypt(this.textBox1.Text, this.textBox3.Text);
                comboBox1.Enabled = false; comboBox1.ForeColor = System.Drawing.SystemColors.WindowText;
                textBox2.Enabled = false; textBox2.ForeColor = System.Drawing.SystemColors.WindowText;
                textBox3.Enabled = false; textBox3.ForeColor = System.Drawing.SystemColors.WindowText;
            }        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                string str = this.comboBox1.Text;
                switch (str)
                {
                    case "128位加密": round = 16; break;
                    case "192位加密": round = 24; break;
                    case "256位加密": round = 32; break;
                    default: break;
                }
            }        private void Form1_Load(object sender, EventArgs e)
            {
                comboBox1.SelectedIndex = 0;
            }        private void button2_Click(object sender, EventArgs e)
            {
                RC6 RC = new RC6(round);
                this.label6.Text = RC.Decrypt(this.textBox2.Text, this.textBox3.Text);
                comboBox1.Enabled = true; comboBox1.ForeColor = System.Drawing.SystemColors.WindowText;
                textBox2.Enabled = true; textBox2.ForeColor = System.Drawing.SystemColors.WindowText;
                textBox3.Enabled = true; textBox3.ForeColor = System.Drawing.SystemColors.WindowText;
            }
        }
    }本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/ExcelWord/archive/2010/11/29/6041864.aspx