项目需要,原来该程序(asp)的加密和解密方法是:base64()Function Encode(str)'不处理中文
   Dim i,j,k
   k=Len(str)
   For i=1 To k
j=Mid(str,i,1)
If ASC(j)<0 Or ASC(j)>255 Then
   Encode=str
   Exit Function 
End If 
   Next    Dim oBase
   Set oBase = new base64
   Call oBase.initCodecs()
   Encode=oBase.base64Encode(str)
   Set oBase=Nothing 
End FunctionFunction Decode(str)'不处理中文
   Dim i,j,k
   k=Len(str)
   For i=1 To k
j=Mid(str,i,1)
If ASC(j)<0 Or ASC(j)>255 Then
   Decode=str
   Exit Function 
End If 
   Next    Dim oBase
   Set oBase = new base64
   Call oBase.initCodecs()
   Decode=oBase.base64Decode(str)
   Set oBase=Nothing 
End Function
上网找资料,想写一个.net的base64的加密解密算法,使得.net和asp的方法获得的结果一致.net方法
    public string EncodeBase64(string s)
    {
        string strResult = "";
        if ((s != null) && (s != ""))
        {
          strResult = Convert.ToBase64String(System.Text.Encoding.Default.GetBytes(s.Trim()));
        }
        return strResult;
    }    public string DecodeBase64(string s)
    {
        string strResult = "";        if ((s != null) && (s != ""))
        {
            
            byte[] b = Convert.FromBase64String(s);
            strResult = System.Text.Encoding.Default.GetString(b);
        }
        return strResult;
    } 
}
相同的一个字符串用 asp的加密和.net的加密结果不一样....
 不知道为什么..郁闷.望达人相助..

解决方案 »

  1.   

    base64(asp中的类)
    Class base64
         Private BASE_64_MAP_INIT 
     Private Base64EncMap(63)
     Private Base64DecMap(127)
         '初始化函数
         PUBLIC SUB initCodecs()
          
              ' 初始化变量
      BASE_64_MAP_INIT = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-"
              dim max, idx
                 max = len(BASE_64_MAP_INIT)
              for idx = 0 to max - 1
                   Base64EncMap(idx) = mid(BASE_64_MAP_INIT, idx + 1, 1)
              next
              for idx = 0 to max - 1
                   Base64DecMap(ASC(Base64EncMap(idx))) = idx
              next
         END SUB
         'Base64加密函数
         PUBLIC FUNCTION base64Encode(plain)
              if len(plain) = 0 then
                   base64Encode = ""
                   exit function
              end if
              dim ret, ndx, by3, first, second, third
              by3 = (len(plain) \ 3) * 3
              ndx = 1
              do while ndx <= by3
                   first  = asc(mid(plain, ndx+0, 1))
                   second = asc(mid(plain, ndx+1, 1))
                   third  = asc(mid(plain, ndx+2, 1))
                   ret = ret & Base64EncMap(  (first \ 4) AND 63 )
                   ret = ret & Base64EncMap( ((first * 16) AND 48) + ((second \ 16) AND 15 ) )
                   ret = ret & Base64EncMap( ((second * 4) AND 60) + ((third \ 64) AND 3 ) )
                   ret = ret & Base64EncMap( third AND 63)
                   ndx = ndx + 3
              loop
              if by3 < len(plain) then
                   first  = asc(mid(plain, ndx+0, 1))
                   ret = ret & Base64EncMap(  (first \ 4) AND 63 )
                   if (len(plain) MOD 3 ) = 2 then
                        second = asc(mid(plain, ndx+1, 1))
                        ret = ret & Base64EncMap( ((first * 16) AND 48) + ((second \ 16) AND 15 ) )
                        ret = ret & Base64EncMap( ((second * 4) AND 60) )
                   else
                        ret = ret & Base64EncMap( (first * 16) AND 48)
                        ret = ret '& "="
                   end if
                   ret = ret '& "="
              end if
              base64Encode = ret
         END FUNCTION
         'Base64解密函数。转载请保留www.it055.com版权
         PUBLIC FUNCTION base64Decode(scrambled)
              if len(scrambled) = 0 then
                   base64Decode = ""
                   exit function
              end if
              dim realLen
              realLen = len(scrambled)
              do while mid(scrambled, realLen, 1) = "="
                   realLen = realLen - 1
              loop
              dim ret, ndx, by4, first, second, third, fourth
              ret = ""
              by4 = (realLen \ 4) * 4
              ndx = 1
              do while ndx <= by4
                   first  = Base64DecMap(asc(mid(scrambled, ndx+0, 1)))
                   second = Base64DecMap(asc(mid(scrambled, ndx+1, 1)))
                   third  = Base64DecMap(asc(mid(scrambled, ndx+2, 1)))
                   fourth = Base64DecMap(asc(mid(scrambled, ndx+3, 1)))
                   ret = ret & chr( ((first * 4) AND 255) +   ((second \ 16) AND 3))
                   ret = ret & chr( ((second * 16) AND 255) + ((third \ 4) AND 15))
                   ret = ret & chr( ((third * 64) AND 255) +  (fourth AND 63))
                   ndx = ndx + 4
              loop
              if ndx < realLen then
                   first  = Base64DecMap(asc(mid(scrambled, ndx+0, 1)))
                   second = Base64DecMap(asc(mid(scrambled, ndx+1, 1)))
                   ret = ret & chr( ((first * 4) AND 255) +   ((second \ 16) AND 3))
                   if realLen MOD 4 = 3 then
                        third = Base64DecMap(asc(mid(scrambled,ndx+2,1)))
                        ret = ret & chr( ((second * 16) AND 255) + ((third \ 4) AND 15))
                   end if
              end if
              base64Decode = ret
         END Function
    End Class