日期:2014-05-18  浏览次数:20446 次

请教:SQL Server加密解密word文档时候的问题
word文档作为二进制流的形式存到存储过程里面
SqlParameter   nameParam   =   firstCommand.Parameters.Add( "@word ",System.Data.SqlDbType.VarBinary);
nameParam.Value   =   wordData;//wordData为byte[]

在加密的存储过程中

CREATE   PROCEDURE   Encrypt
@word     varbinary(max)  
AS
BEGIN
 
EXECUTE   AS   LOGIN   =   'Mickey '
OPEN   SYMMETRIC   KEY   User1SymmetricKeyCert
DECRYPTION   BY   CERTIFICATE   cert_mickey

INSERT   INTO   PTDG   (word)   values(EncryptByKey(Key_GUID( 'User1SymmetricKeyCert '),@word))

CLOSE   SYMMETRIC   KEY   User1SymmetricKeyCert  
RETURN
END
这样进行加密
然后解密的存储过程如下
CREATE   PROCEDURE   DECRYPT
AS
BEGIN
EXECUTE   AS   LOGIN   =   'Mickey '
OPEN   SYMMETRIC   KEY   User1SymmetricKeyCert     DECRYPTION   BY   CERTIFICATE   cert_mick
SELECT   CONVERT(varbinary,   DecryptByKey(word))   AS   newWord   FROM   PTDG   WHERE   scope=2

CLOSE   ALL   SYMMETRIC   KEYS
REVERT
END
GO
然后我调用解密存储过程生成新的word文档
        SqlDataAdapter   da=new   SqlDataAdapter();
        da.SelectCommand=new   SqlCommand( "DECRYPT ",conn);                      
        da.SelectCommand.CommandType=CommandType.StoredProcedure;
 
        DataSet   ds=new   DataSet();
        da.Fill(ds);
 
        DataTable   table   =ds.Tables[0];
 
        foreach(DataRow   dr   in   table.Rows)
        {
          byte[]   outbyte   =   (byte[])dr[ "word "];      
 
          fs   =   new   FileStream(filePath,   FileMode.OpenOrCreate,   FileAccess.Write);
          bw   =   new   BinaryWriter(fs);
         
          bw.Write(outbyte);
          bw.Flush();
 

          bw.Close();
          fs.Close();
        }


但是生成的word文档不对,打开时候要选择编码,而且里面是空的
不知道那里出了错误,大家帮忙来看看,谢谢了。


------解决方案--------------------
.............不会,学习
------解决方案--------------------
难度挺大,不会,学习中,帮你顶上
------解决方案--------------------
EncryptByKey
 
1.使用密钥加密的数据的变量:nvarchar、char、wchar、varchar 或 nchar 

2.返回: varbinary,最大大小为 8000 个字节