要写解码或者编码的算法才行sql 中没有直接转换 base64 编码的

解决方案 »

  1.   

    网上可以找
    xp_encode_base64.dll和
    xp_decode_base64.dll两个动态库,里面就有编码和解码的扩展存储过程
      

  2.   

    CREATE   FUNCTION   base64_decode  
      (  
          @encoded_text   varchar(8000)  
      )  
      RETURNS    
                          varchar(6000)  
      AS   BEGIN  
      --local   variables  
      DECLARE  
          @output                       varchar(8000),  
          @block_start             int,  
          @encoded_length       int,  
          @decoded_length       int,  
          @mapr                           binary(122)  
      --IF   @encoded_text   COLLATE   LATIN1_GENERAL_BIN  
      --   LIKE   '%[^ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=]%'  
      --           COLLATE   LATIN1_GENERAL_BIN  
      --     RETURN   NULL  
      --IF   LEN(@encoded_text)   &   3   >   0  
      --     RETURN   NULL  
      SET   @output       =   ''  
      --   The   nth   byte   of   @mapr   contains   the   base64   value  
      --   of   the   character   with   an   ASCII   value   of   n.  
      --   EG,   65th   byte   =   0x00   =   0   =   value   of   'A'  
      SET   @mapr   =  
          0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF   --   1-33  
      +   0xFFFFFFFFFFFFFFFFFFFF3EFFFFFF3F3435363738393A3B3C3DFFFFFF00FFFFFF   --   33-64  
      +   0x000102030405060708090A0B0C0D0E0F10111213141516171819FFFFFFFFFFFF   --   65-96  
      +   0x1A1B1C1D1E1F202122232425262728292A2B2C2D2E2F30313233   --   97-122  
      --get   the   number   of   blocks   to   be   decoded  
      SET   @encoded_length   =   LEN(@encoded_text)  
      SET   @decoded_length   =   @encoded_length   /   4   *   3  
      --for   each   block  
      SET   @block_start   =   1  
      WHILE   @block_start   <   @encoded_length   BEGIN  
          --decode   the   block   and   add   to   output  
          --BINARY   values   between   1   and   4   bytes   can   be   implicitly   cast   to   INT  
          SET   @output   =   @output   +     CAST(CAST(CAST(  
            substring(   @mapr,   ascii(   substring(   @encoded_text,   @block_start         ,   1)   ),   1)   *   262144  
        +   substring(   @mapr,   ascii(   substring(   @encoded_text,   @block_start   +   1,   1)   ),   1)   *   4096  
        +   substring(   @mapr,   ascii(   substring(   @encoded_text,   @block_start   +   2,   1)   ),   1)   *   64  
        +   substring(   @mapr,   ascii(   substring(   @encoded_text,   @block_start   +   3,   1)   ),   1)    
            AS   INTEGER)   AS   BINARY(3))   AS   VARCHAR(3))  
          SET   @block_start   =   @block_start   +   4  
      END  
      IF   RIGHT(@encoded_text,   2)   =   '=='  
        SET   @decoded_length   =   @decoded_length   -   2  
      ELSE   IF   RIGHT(@encoded_text,   1)   =   '='  
        SET   @decoded_length   =   @decoded_length   -   1  
      --IF   SUBSTRING(@output,   @decoded_length,   1)   =   CHAR(0)  
      --   SET   @decoded_length   =   @decoded_length   -   1  
      --return   the   decoded   string  
      RETURN   LEFT(@output,   @decoded_length)  
      END  
      GO    select dbo.base64_decode('amluamF6eg==')
      

  3.   

    六楼的不行啊 我以前试过 这个编码与解码是按照自己的算法写的 与C#的不同 CGQH/DWx43U= 与Rbv//9rJ2VIX3RQhVDtptQ== 解释出来都是乱码 这两个是C#的64编码
      

  4.   

    base64就是base64,不存在什么c#不c#的。你应该理解他,base64编码的是byte类型的数组,数组根据相关的编码规则才能对应到字符串,比如utf-8或者gb2312。
    你先要搞清楚你在c#中把字符串转为byte数组的编码用的是什么。
      

  5.   

    感谢 大家指教 特别是 jinjazz 谢谢 问题已解决! 结贴 O(∩_∩)O
      

  6.   

    我在7楼 出现的问题 是因为 之前的明文被DES加密过一次 然后又进行了base64编码 所以使用了数据库函数解密后还是出现了乱码情况 - -、源码如下: //将字符串进行可逆加密
           public static string Encrypt(string value)
           {           if (value != "")
               {
                   DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
                   MemoryStream stream = new MemoryStream();
                   CryptoStream stream2=new CryptoStream(stream,provider.CreateEncryptor(KEY_64,IV_64),CryptoStreamMode.Write);
                   StreamWriter writer=new StreamWriter(stream2);
                   writer.Write(value);
                   writer.Flush();
                   stream2.FlushFinalBlock();
                   stream.Flush();
                   return Convert.ToBase64String(stream.GetBuffer(),0,(int)stream.Length);
               }
               return "";
           }