基本加密示例

无忧客服提供一种加密算法 基本加密模式. 如果你使用 C#, ASP, PHPJava, 你可以将无忧客服与你的网站用户集成,通过基本加密算法.

  1. C# 示例 
    int EncryptLoopCount = 4;

    public string BasicEncrypt(string data, string encryptkey)
    {
      if (string.IsNullOrEmpty(data))
      throw (new ArgumentNullException("data"));
      if (string.IsNullOrEmpty(encryptkey))
      throw (new ArgumentNullException("encryptkey"));
      int[] vals = MakeArray(data, true);
      int[] keys = MakeArray(encryptkey, false);
      for (int t = 0; t < EncryptLoopCount; t++)
      {
      for (int i = 0; i < vals.Length; i++)
      {
      int v = vals[i];
      int im = (v + i) % 5;
      for (int x = 0; x < vals.Length; x++)
      {
      if (x == i)
      continue;
      if (x % 5 != im)
      continue;
      for (int y = 0; y < keys.Length; y++)
      {
      int k = keys[y];
      if (k == 0)
      continue;
      vals[x] += v % k;
      }
      }
      }
      }
      return BasicFormat(vals);
    }

    public string BasicFormat(int[] vals)
    {
      StringBuilder sb = new StringBuilder();
      foreach (int i in vals)
      {
      if (sb.Length > 0)
      sb.Append("-");
      sb.Append(i);
      }
      return sb.ToString();
    }

    public int[] MakeArray(string str, bool random)
    {
      int len = (int)Math.Pow(2, Math.Floor(Math.Log(str.Length, 2)) + 1) + 8;
      if (len < 32) len = 32;
      int[] arr = new int[len];
      if (random)
      {
      Random r = new Random();
      for (int i = 0; i < arr.Length; i++)
      arr[i] = str[r.Next() % str.Length];
      int start = 1 + r.Next() % (len - str.Length - 2);
      for (int i = 0; i < str.Length; i++)
      arr[start + i] = str[i];
      arr[start - 1] = 0;
      arr[start + str.Length] = 0;
      }
      else
      {
      for (int i = 0; i < arr.Length; i++)
      arr[i] = str[i % str.Length];
      }
      return arr;
    }
  2. Asp 示例 
    Public Function MakeArray(str, random)
      Dim arrlen
      arrlen = 2^(Fix(log(2)/log(Len(str))) + 1) + 8
      If arrlen < 32 Then
      arrlen = 32
      End If
      ReDim arr(arrlen-1)
      If random=true Then
      Randomize Timer
      For i = 0 to arrlen-1
      arr(i) = Asc(Mid(str,Int((9999 - 1000 + 1) * Rnd + 1000) mod Len(str) + 1,1))
      Next
      start = 1 + Int((9999 - 1000 + 1) * Rnd + 1000) mod (arrlen - Len(str) - 2)
      For i = 1 to Len(str)
      arr(start + i) = Asc(Mid(str,i,1))
      Next
      arr(start - 1) = 0
      arr(start + Len(str)) = 0
      Else
      For i = 0 to arrlen-1
      arr(i) = Asc(Mid(str,i mod Len(str) + 1,1))
      Next
      End If
      MakeArray=arr
    End Function

    Public Function BasicFormat(vals)
      Dim sb
      sb = ""
      For i=0 to UBound(vals)
      If Len(sb) > 0 Then
      sb = sb & "-"
      End if
      sb = sb & (vals(i)&"")
      Next
      BasicFormat=sb
    End Function

    Public Function BasicEncrypt(data, encryptkey)
      If data=null Or Len(Trim(data))=0 Then
      Err.Raise6
      End If
      If encryptkey=null Or Len(Trim(encryptkey))=0 Then
      Err.Raise6
      End If
      vals = MakeArray(data, true)
      keys = MakeArray(encryptkey, false)
      EncryptLoopCount = 4
      for t = 0 to EncryptLoopCount-1
      for i = 0 to UBound(vals)
      v = vals(i)
      im = (v + i) mod 5
      for x = 0 to UBound(vals)
      if x <> i And x mod 5 = im Then
      for y = 0 to UBound(keys)
      k = keys(y)
      if k <> 0 Then
      vals(x) = vals(x) + v mod k
      End if
      Next
      End If
      Next
      Next
      Next
      BasicEncrypt=BasicFormat(vals)
    End Function
  3. Php 示例 
    function BasicEncrypt($data, $encryptkey)
    {
      $EncryptLoopCount = 4;
      $vals = MakeArray($data, true);
      $keys = MakeArray($encryptkey, false);
      $len = sizeof($vals);
      $len2 = sizeof($keys);
      for ($t = 0; $t < $EncryptLoopCount; $t++)
      {
      for ($i = 0; $i < $len; $i++)
      {
      $v = $vals[$i];
      $im = ($v + $i) % 5;
      for ($x = 0; $x < $len; $x++)
      {
      if ($x == $i)
      continue;
      if ($x % 5 != $im)
      continue;
      for ($y = 0; $y <$len2; $y++)
      {
      $k = $keys[$y];
      if ($k == 0)
      continue;
      $vals[$x] += $v % $k;
      }
      }
      }
      }
      return implode('-', $vals);
    }

    function MakeArray($str, $random)
    {
      $len = pow(2, floor(log(strlen($str), 2)) + 1) + 8;
      if ($len < 32) $len = 32;
      $arr = Array();
      $strarr = str_split($str);
      if ($random==true)
      {
      for ($i = 0; $i < $len; $i++)
      $arr[] = ord($strarr[rand() % strlen($str)]);
      $start = 1 + rand() % ($len - strlen($str) - 2);
      for ($i = 0; $i < strlen($str); $i++)
      $arr[$start + $i] = ord($strarr[$i]);
      $arr[$start - 1] = 0;
      $arr[$start + strlen($str)] = 0;
      }
      else
      {
      for ($i = 0; $i < $len; $i++)
      $arr[] = ord($strarr[$i % strlen($str)]);
      }
      return $arr;
    }
  4. Java 示例 
    int EncryptLoopCount = 4;
    public int[] MakeArray(String str, Boolean random)
    {
      int len = (int)Math.pow(2, Math.floor(Math.log(2)/Math.log(str.length())) + 1) + 8;
      if (len < 32) len = 32;
      int[] arr = new int[len];
      if (random)
      {
      Random r = new Random();
      for (int i = 0; i < arr.length; i++)
      arr[i] = str.charAt(r.nextInt(9999) % str.length());
      int start = 1 + r.nextInt(9999) % (len - str.length() - 2);
      for (int i = 0; i < str.length(); i++)
      arr[start + i] = str.charAt(i);
      arr[start - 1] = 0;
      arr[start + str.length()] = 0;
      }
      else
      {
      for (int i = 0; i < arr.length; i++)
      arr[i] = str.charAt(i % str.length());
      }
      return arr;
    }

    public String BasicFormat(int[] vals)
    {
      StringBuilder sb = new StringBuilder();
      for (int i=0;i   {
      if (sb.length() > 0)
      sb.append("-");
      sb.append(vals[i]);
      }
      return sb.toString();
    }

    public String BasicEncrypt(String data, String encryptkey)
    {
      if (data==null || data.trim() == "")
      throw new IllegalArgumentException("data");
      if (encryptkey == null || encryptkey == "")
      throw new IllegalArgumentException("encryptkey");
      int[] vals = MakeArray(data, true);
      int[] keys = MakeArray(encryptkey, false);
      for (int t = 0; t < EncryptLoopCount; t++)
      {
      for (int i = 0; i < vals.length; i++)
      {
      int v = vals[i];
      int im = (v + i) % 5;
      for (int x = 0; x < vals.length; x++)
      {
      if (x == i)
      continue;
      if (x % 5 != im)
      continue;
      for (int y = 0; y < keys.length; y++)
      {
      int k = keys[y];
      if (k == 0)
      continue;
      vals[x] += v % k;
      }
      }
      }
      }
      return BasicFormat(vals);
    }