现在有2个string,一个长度为5,保存着学校代码信息;另一个长度为3,保存着学生代码。
现在想将他们加密为一个长度为8的代码,并且可以解密。 最简单的方法是:分别加密,然后连接在一起。解密的时候取也很简单,密码的前5位解密成学校,后3位解密成学生即可。
但是这样有很明显的缺陷,比如一个学校的很多学生拿到代码后互相比较,就可以发现他们的密码前5位都一样,只有后3位不一样,那么很容易就可以猜测到这个密码的构成方法。
请问各位提供一种更为隐蔽安全的加密/解密方法,有具体代码更佳。非常感谢。

解决方案 »

  1.   

    加密解密可以使用.net framework提供的DES算法:public string Encrypt(string pToEncrypt, string sKey)
            {
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                //把字符串放到byte数组中  
                //原来使用的UTF8编码,我改成Unicode编码了,不行  
                byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
                //byte[]  inputByteArray=Encoding.Unicode.GetBytes(pToEncrypt);              //建立加密对象的密钥和偏移量  
                //原文使用ASCIIEncoding.ASCII方法的GetBytes方法  
                //使得输入密码必须输入英文文本  
                des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
                des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
                //Write  the  byte  array  into  the  crypto  stream  
                //(It  will  end  up  in  the  memory  stream)  
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
                //Get  the  data  back  from  the  memory  stream,  and  into  a  string  
                StringBuilder ret = new StringBuilder();
                foreach (byte b in ms.ToArray())
                {
                    //Format  as  hex  
                    ret.AppendFormat("{0:X2}", b);
                }
                ret.ToString();
                return ret.ToString();
            }        //解密方法  
            public string Decrypt(string pToDecrypt, string sKey)
            {
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();            //Put  the  input  string  into  the  byte  array  
                byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
                for (int x = 0; x < pToDecrypt.Length / 2; x++)
                {
                    int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
                    inputByteArray[x] = (byte)i;
                }            //建立加密对象的密钥和偏移量,此值重要,不能修改  
                des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
                des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
                //Flush  the  data  through  the  crypto  stream  into  the  memory  stream  
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();            //Get  the  decrypted  data  back  from  the  memory  stream  
                //建立StringBuild对象,CreateDecrypt使用的是流对象,必须把解密后的文本变成流对象  
                StringBuilder ret = new StringBuilder();            return System.Text.Encoding.Default.GetString(ms.ToArray());
            }        string encripted = "";
            private void button6_Click(object sender, EventArgs e)
            {
                encripted = Encrypt("1234567890", "hello123");
            }        private void button7_Click(object sender, EventArgs e)
            {
                MessageBox.Show(Decrypt(encripted, "hello123"));
            }  
      

  2.   

    加密解密可以使用.net framework提供的DES算法:public string Encrypt(string pToEncrypt, string sKey)
            {
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                //把字符串放到byte数组中  
                //原来使用的UTF8编码,我改成Unicode编码了,不行  
                byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
                //byte[]  inputByteArray=Encoding.Unicode.GetBytes(pToEncrypt);              //建立加密对象的密钥和偏移量  
                //原文使用ASCIIEncoding.ASCII方法的GetBytes方法  
                //使得输入密码必须输入英文文本  
                des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
                des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
                //Write  the  byte  array  into  the  crypto  stream  
                //(It  will  end  up  in  the  memory  stream)  
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
                //Get  the  data  back  from  the  memory  stream,  and  into  a  string  
                StringBuilder ret = new StringBuilder();
                foreach (byte b in ms.ToArray())
                {
                    //Format  as  hex  
                    ret.AppendFormat("{0:X2}", b);
                }
                ret.ToString();
                return ret.ToString();
            }        //解密方法  
            public string Decrypt(string pToDecrypt, string sKey)
            {
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();            //Put  the  input  string  into  the  byte  array  
                byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
                for (int x = 0; x < pToDecrypt.Length / 2; x++)
                {
                    int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
                    inputByteArray[x] = (byte)i;
                }            //建立加密对象的密钥和偏移量,此值重要,不能修改  
                des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
                des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
                //Flush  the  data  through  the  crypto  stream  into  the  memory  stream  
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();            //Get  the  decrypted  data  back  from  the  memory  stream  
                //建立StringBuild对象,CreateDecrypt使用的是流对象,必须把解密后的文本变成流对象  
                StringBuilder ret = new StringBuilder();            return System.Text.Encoding.Default.GetString(ms.ToArray());
            }        string encripted = "";
            private void button6_Click(object sender, EventArgs e)
            {
                encripted = Encrypt("1234567890", "hello123");
            }        private void button7_Click(object sender, EventArgs e)
            {
                MessageBox.Show(Decrypt(encripted, "hello123"));
            }  
      

  3.   

     and up
    If DES arithmetic is not enough,you can use AES arithmetic to encrypt them!
      

  4.   

    多谢楼上诸位弟兄的答复。不过上面的几个加密方法和我想要的还不太一样。
    首先,我想要的是5位和3位组合成一个8位的,而md5或者des在长度上不符合规则;其次我希望的是5位和3位这两个码的组合,而不是每个单独加密。
    期待其他解决方法。谢谢