我把用Rijndael加密算法的Key和IV值转换成BitConverter.ToString()然后再转成System.Text.UTF8Encoding的byte类型写到文件中,现在从文件中取出的数值不知
该怎样转换才能把Key和IV转回byte了,请高手帮忙。

解决方案 »

  1.   

    byte byteT=byte.parse(stringtoconvert[index]);
      

  2.   

    Encoding.UTF8.GetBytes(yourStringFromTextFile);
      

  3.   

    把顺序倒过来操作就可以了
    BitConverter.GetBytes 将指定的数据转换为字节数组。
      

  4.   

    Encoding.UTF8.GetBytes(yourStringFromTextFile);
    好象也不行
      

  5.   

    加密代码
    FileStream fs=new FileStream(@"k:\大史记2(分家在十月).wmv",FileMode.Open);
    byte []bytesize=new byte[fs.Length];
    fs.Read(bytesize,0,bytesize.Length);
    fs.Close();
    Rijndael crypt=Rijndael.Create();    //Rijndael算法
    byte []IVValue=crypt.IV;             //导出向量
    byte []KeyValue=crypt.Key;           //导出密匙
    System.Text.UTF8Encoding enc=new System.Text.UTF8Encoding();
    string str1=BitConverter.ToString(IVValue);      //把向量转成BitConverter.ToString
    string str2=BitConverter.ToString(KeyValue);    //把密匙BitConverter.ToString
    byte []size1=enc.GetBytes(str1);                //把密匙和向量转成byte
    byte []size2=enc.GetBytes(str2);
    FileStream fso=new FileStream(@"k:\IV.txt",FileMode.Create,FileAccess.Write);
    fso.Write(size1,0,size1.Length);
    fso.Close();
    fso=new FileStream(@"k:\key.txt",FileMode.Create,FileAccess.Write);
    fso.Write(size2,0,size2.Length);
    fso.Close();
    fs=new FileStream(@"k:\encry.cu",FileMode.Create,FileAccess.Write); CryptoStream cs=new CryptoStream(fs,crypt.CreateEncryptor(),CryptoStreamMode.Write);
    cs.Write(bytesize,0,bytesize.Length);解密代码
    FileStream fs=new FileStream(@"k:\encry.cu",FileMode.Open);
    byte []bytesize=new byte[fs.Length];
    fs.Read(bytesize,0,bytesize.Length);       //读取文件
    fs.Close();
    FileStream fso=new FileStream(@"k:\IV.txt",FileMode.Open);
    byte []size1=new byte[fso.Length];
    fso.Read(size1,0,size1.Length);
    fso.Close();
    fso=new FileStream(@"k:\key.txt",FileMode.Open);
    byte []size2=new byte[fso.Length];
    fso.Read(size2,0,size2.Length);
    fso.Close();
    System.Text.UTF8Encoding enc=new System.Text.UTF8Encoding();
    string str1=enc.GetString(size1);
    byte []IVValue=System.Text.Encoding.UTF8.GetBytes(str1);
      

  6.   

    关键是文件中的密匙部份,因为在写文件前是先将BitConverter.ToString()转成enc.GetByte然后写入文件。