RC4 Encryption and decryption in UTF-8 ASP classic

◇◆丶佛笑我妖孽 提交于 2019-12-24 03:28:00

问题


Is there a way to make this work in an ASP page? It was working fine with charset windows-1253 but now I have to convert my site to UTF-8 and the only problem I have is encryption and decryption. I have encrypted members registration passwords and now with UTF-8, after decryption passwords looks like this: 2��8��6

Please I nead some help (sorry for my English). This is RC4 code:

Dim sbox(255)
   Dim rc4Key(255)

   Sub RC4Initialize(strPwd)

      dim tempSwap, a, b      

      intLength = len(strPwd)

      for a = 0 To 255

         rc4Key(a)  = asc(mid(strpwd, (a mod intLength)+1, 1))

         sbox(a) = a                       
      next

      b = 0

      for a = 0 To 255
         b = (b + sbox(a) + rc4Key(a)) Mod 256         
         tempSwap = sbox(a)
         sbox(a) = sbox(b)
         sbox(b) = tempSwap
      Next

   End Sub

   function EnDeCrypt(plaintxt, psw)      

      dim temp, a, i, j, k, cipherby, cipher      

      i = 0
      j = 0

      RC4Initialize psw

      for a = 1 To Len(plaintxt)
         i  = (i + 1) Mod 256
         j  = (j + sbox(i)) Mod 256
         temp   = sbox(i)
         sbox(i)= sbox(j)
         sbox(j)= temp

         k  = sbox((sbox(i) + sbox(j)) Mod 256)

         cipherby   = Asc(Mid(plaintxt, a, 1)) Xor k
         cipher     = cipher & Chr(cipherby)         
      next

      enDeCrypt = cipher            

   end function

   function RC4EnCryptASC(plaintxt, psw)      

      dim temp, a, i, j, k, cipherby, cipher      

      i = 0
      j = 0

      RC4Initialize psw

      for a = 1 To Len(plaintxt)
         i  = (i + 1) Mod 256
         j  = (j + sbox(i)) Mod 256
         temp   = sbox(i)
         sbox(i)= sbox(j)
         sbox(j)= temp

         k      = sbox((sbox(i) + sbox(j)) Mod 256)

         cipherby   = Asc(Mid(plaintxt, a, 1)) Xor k         
         cipher     = cipher & "|"& cipherby         
      next            

      RC4EnCryptASC = cipher            

   end function

   function RC4DeCryptASC(plaintxt, psw)      

      plaintxt = transformToCHR(plaintxt)

      dim temp, a, i, j, k, cipherby, cipher      

      i = 0
      j = 0      

      dim arrayEncrypted            

      RC4Initialize psw         

       for a = 1 To Len(plaintxt)
         i  = (i + 1) Mod 256
         j  = (j + sbox(i)) Mod 256
         temp   = sbox(i)
         sbox(i)= sbox(j)
         sbox(j)= temp

         k  = sbox((sbox(i) + sbox(j)) Mod 256)

         cipherby   = Asc(Mid(plaintxt, a, 1)) Xor k
         cipher     = cipher & Chr(cipherby)         
      next

      RC4DeCryptASC = cipher                       

   end function

function transformToCHR(plaintxt)


      dim returnText, arrayEncrypted
      arrayEncrypted = split(plaintxt, "|")            

      returnText=""            

      for a = 1 to ubound(arrayEncrypted)         
         returnText=returnText&CHR(arrayEncrypted(a))         
      next            

      transformToCHR = returnText
end function

回答1:


i think you have to use AscW function and the equivalent other functions to deal with Unicode characters?




回答2:


Ok finally I made it. I just replaced each asc to ascW and each chr to chrW and it works perfect.



来源:https://stackoverflow.com/questions/21258634/rc4-encryption-and-decryption-in-utf-8-asp-classic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!