本人要用到DES加密技术,下了一个代码来看,没有多大难度,但是它把加密后的密文保存在生成的TXT文本里,而我需要的是把密文保存在数据库里.在这个环节我不知道在怎么改!
======================这是加密过程它把密文保存在TXT里
//初始序列化
FileStream fs = new FileStream("D:\\target.txt", FileMode.OpenOrCreate, FileAccess.Write);
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter br = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
 //显示密文
cbox.Text = System.Text.Encoding.Default.GetString(System.Text.Encoding.Default.GetBytes(cchar));
 //序列化密文到文件
br.Serialize(fs, cchar);
fs.Close();
--------------------------这一步,我我把前面的步骤忽略掉,直接保存System.Text.Encoding.Default.GetString(System.Text.Encoding.Default.GetBytes(cchar));所得的结果
改后,执行时没有出什么问题
=======================这是解密时从TXT里取出密文
 //从文件反序列化或得密文char形式
 FileStream fs = new FileStream("D:\\target.txt", FileMode.OpenOrCreate, FileAccess.Read);
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
char[] cchar = (char[])bf.Deserialize(fs);
----------------------------这一不,我把前面的步骤忽略掉,直接改最后一步
 char[] cchar = [直接从数据库读取数据出来].ToCharArray();
改后,执行时总是出错
 divcchar[i][7] = cchar[i * 8 + 7];索引超出了数组界限。 不知道是什么原因,估计是我改错了吧.请问正确的改法该怎么改?
我就只剩10分了~~~~~~~~~

解决方案 »

  1.   

    public string DesEncrypt(string strText,string strEncrKey)
    {
    byte[] byKey = null;
    byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
    try
    {
    byKey = System.Text.Encoding.UTF8.GetBytes(strEncrKey.Substring(0, strEncrKey.Length));
    DESCryptoServiceProvider des = new DESCryptoServiceProvider();
    byte[] inputByteArray = Encoding.UTF8.GetBytes(strText);
    MemoryStream ms = new MemoryStream();
    CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write);
    cs.Write(inputByteArray, 0, inputByteArray.Length);
    cs.FlushFinalBlock();
    return Convert.ToBase64String(ms.ToArray());
    }
    catch (System.Exception error)
    {
    return "error:" + error.Message + "\r";
    } }
    //解密
    public string DesDecrypt(string strText, string sDecrKey)
    {
    byte[] byKey = null;
    byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
    byte[] inputByteArray = new Byte[strText.Length];
    try
    {
    byKey = System.Text.Encoding.UTF8.GetBytes(sDecrKey.Substring(0, 8));
    DESCryptoServiceProvider des = new DESCryptoServiceProvider();
    inputByteArray = Convert.FromBase64String(strText);
    MemoryStream ms = new MemoryStream();
    CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);
    cs.Write(inputByteArray, 0, inputByteArray.Length);
    cs.FlushFinalBlock();
    System.Text.Encoding encoding = new System.Text.UTF8Encoding();
    return encoding.GetString(ms.ToArray());
    }
    catch (System.Exception error)
    {
    return "error:" + error.Message + "\r";
    }
    }
      

  2.   

    有点难看懂,我把它的代码搬过来,麻烦帮我看看~~~~~
    =============================
    #region Using directives
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Windows.Forms;
    using System.IO;
    #endregionnamespace DES
    {
        public partial class DES : Form
        {
            public DES()
            {
                InitializeComponent();
            }
            Func.Func F = new Func.Func(); // 初始化函数库
            private void DES_Load(object sender, EventArgs e)
            {        }        private void button1_Click(object sender, EventArgs e)
            {
                char[] tempmchar = mbox.Text.ToCharArray(); //原始明文            int mod = tempmchar.Length % 8;             //扩展明文为8的倍数
                char[] mchar = new char[tempmchar.Length + (8 - mod)];
                for (int i = 0; i < tempmchar.Length; i++)
                {
                    mchar[i] = tempmchar[i];
                }
                for (int i = tempmchar.Length; i < mchar.Length; i++)
                {
                    mchar[i] = ' ';
                }            char[] kchar = kbox.Text.ToCharArray();//获得64位密钥
                char[] cchar = new char[mchar.Length];//初始化密文数组            int[][] keys = F.makekey(kchar);//由makekey函数通过64位密钥计算16个48位密钥            int div = mchar.Length / 8;//每8个为一组对明文分组
                if ((mchar.Length % 8) != 0) div += 1;
                char[][] divmchar = new char[div][];//存放分组明文
                for (int i = 0; i < div; i++)
                {
                    divmchar[i] = new char[8];
                    divmchar[i][0] = mchar[i * 8 + 0];
                    divmchar[i][1] = mchar[i * 8 + 1];
                    divmchar[i][2] = mchar[i * 8 + 2];
                    divmchar[i][3] = mchar[i * 8 + 3];
                    divmchar[i][4] = mchar[i * 8 + 4];
                    divmchar[i][5] = mchar[i * 8 + 5];
                    divmchar[i][6] = mchar[i * 8 + 6];
                    divmchar[i][7] = mchar[i * 8 + 7];
                }
                int[][] divcint = new int[div][];//获得分组加密后的分组密文2进制形式
                for (int i = 0; i < div; i++)
                {
                    divcint[i] = new int[64];
                    divcint[i] = F.DES(keys, divmchar[i]);
                }
                int[] cint = new int[div * 64];//合并密文2进制形式
                for (int i = 0; i < div; i++)
                {
                    for (int j = 0; j < 64; j++)
                    {
                        cint[i * 64 + j] = divcint[i][j];
                    }
                }            //初始序列化
                FileStream fs = new FileStream("D:\\target.txt", FileMode.OpenOrCreate, FileAccess.Write);
                System.Runtime.Serialization.Formatters.Binary.BinaryFormatter br = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                //将2进制密文转化为char密文
                for (int i = 0; i < cchar.Length; i++)
                {
                    cchar[i] = (char)(cchar[i] & 0x0000);
                }
                for (int i = 0; i < cchar.Length; i++)
                {
                    if (cint[i * 8 + 0] == 1) cchar[i] = (char)(cchar[i] | 0x80);
                    if (cint[i * 8 + 1] == 1) cchar[i] = (char)(cchar[i] | 0x40);
                    if (cint[i * 8 + 2] == 1) cchar[i] = (char)(cchar[i] | 0x20);
                    if (cint[i * 8 + 3] == 1) cchar[i] = (char)(cchar[i] | 0x10);
                    if (cint[i * 8 + 4] == 1) cchar[i] = (char)(cchar[i] | 0x8);
                    if (cint[i * 8 + 5] == 1) cchar[i] = (char)(cchar[i] | 0x4);
                    if (cint[i * 8 + 6] == 1) cchar[i] = (char)(cchar[i] | 0x2);
                    if (cint[i * 8 + 7] == 1) cchar[i] = (char)(cchar[i] | 0x1);
                }            //显示密文
                cbox.Text = System.Text.Encoding.Default.GetString(System.Text.Encoding.Default.GetBytes(cchar));            //序列化密文到文件
                br.Serialize(fs, cchar);
                fs.Close();
            }        private void button2_Click(object sender, EventArgs e)
            {
                //初始化
                int[] cint = new int[64];
                char[] kchar = kbox.Text.ToCharArray();
                int[] kint = new int[56];            //或得16个密钥
                int[][] keys = F.makekey(kchar);            //从文件反序列化或得密文char形式
                FileStream fs = new FileStream("D:\\target.txt", FileMode.OpenOrCreate, FileAccess.Read);
                System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                char[] cchar = (char[])bf.Deserialize(fs);
                char[] mchar = new char[cchar.Length];            //每8位一组分组密文
                int div = cchar.Length / 8;
                if ((cchar.Length % 8) != 0) div += 1;
                char[][] divcchar = new char[div][];
                for (int i = 0; i < div; i++)
                {
                    divcchar[i] = new char[8];
                    divcchar[i][0] = cchar[i * 8 + 0];
                    divcchar[i][1] = cchar[i * 8 + 1];
                    divcchar[i][2] = cchar[i * 8 + 2];
                    divcchar[i][3] = cchar[i * 8 + 3];
                    divcchar[i][4] = cchar[i * 8 + 4];
                    divcchar[i][5] = cchar[i * 8 + 5];
                    divcchar[i][6] = cchar[i * 8 + 6];
                    divcchar[i][7] = cchar[i * 8 + 7];
                }            //获得分组后明文2进制形式
                int[][] divmint = new int[div][];
                for (int i = 0; i < div; i++)
                {
                    divmint[i] = new int[64];
                    divmint[i] = F.DES_1(keys, divcchar[i]);
                }            //合并明文2进制形式
                int[] mint = new int[div * 64];
                for (int i = 0; i < div; i++)
                {
                    for (int j = 0; j < 64; j++)
                    {
                        mint[i * 64 + j] = divmint[i][j];
                    }
                }
                //由明文2进制形式转化为char形式
                for (int i = 0; i < mchar.Length; i++)
                {
                    mchar[i] = (char)(mchar[i] & 0x0000);
                }
                for (int i = 0; i < mchar.Length; i++)
                {
                    if (mint[i * 8 + 0] == 1) mchar[i] = (char)(mchar[i] | 0x80);
                    if (mint[i * 8 + 1] == 1) mchar[i] = (char)(mchar[i] | 0x40);
                    if (mint[i * 8 + 2] == 1) mchar[i] = (char)(mchar[i] | 0x20);
                    if (mint[i * 8 + 3] == 1) mchar[i] = (char)(mchar[i] | 0x10);
                    if (mint[i * 8 + 4] == 1) mchar[i] = (char)(mchar[i] | 0x8);
                    if (mint[i * 8 + 5] == 1) mchar[i] = (char)(mchar[i] | 0x4);
                    if (mint[i * 8 + 6] == 1) mchar[i] = (char)(mchar[i] | 0x2);
                    if (mint[i * 8 + 7] == 1) mchar[i] = (char)(mchar[i] | 0x1);
                }            //显示明文
                mbox.Text = System.Text.Encoding.Default.GetString(System.Text.Encoding.Default.GetBytes(mchar));
                fs.Close();
            }
        }
    }