一种可逆的加密算法

  1. 服务器加密 PHP 描述:

<?php
 
class base64encrypt
{

 public function encrypt($data){ 

 $key="this is the key,please saved in a safe state";

    $key    =   md5($key); 

    $x      =   0; 

    $len    =   strlen($data); 

    $l      =   strlen($key); 

    for ($i = 0; $i < $len; $i++) 

    { 

        if ($x == $l)  

        { 

            $x = 0; 

        } 

        $char .= $key{$x}; 

        $x++; 

    } 

    for ($i = 0; $i < $len; $i++) 

    { 

        $str .= chr(ord($data{$i}) + (ord($char{$i})) % 256); 

    } 

    return base64_encode($str); 

 } 

  

}

?>

1.1 PHP 解密

function decrypt($data, $key) 
{ 
    $key = md5($key); 
    $x = 0; 
    $data = base64_decode($data); 
    $len = strlen($data); 
    $l = strlen($key); 
    for ($i = 0; $i < $len; $i++) 
    { 
        if ($x == $l)  
        { 
            $x = 0; 
        } 
        $char .= substr($key, $x, 1); 
        $x++; 
    } 
    for ($i = 0; $i < $len; $i++) 
    { 
        if (ord(substr($data, $i, 1)) < ord(substr($char, $i, 1))) 
        { 
            $str .= chr((ord(substr($data, $i, 1)) + 256) - ord(substr($char, $i, 1))); 
        } 
        else 
        { 
            $str .= chr(ord(substr($data, $i, 1)) - ord(substr($char, $i, 1))); 
        } 
    } 
    return $str; 
}

2.java 解密

package com.security.decrypt;
import android.util.Base64;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class Decrypt
{
  private final String secretKey = "M8sNOPRKSUXZ4TJIcfg56F9uQ";
  public String deCodeLink(String url)
    throws UnsupportedEncodingException
  {
    String charEnCodeType = "ISO-8859-1";
    String deCodeLinkStr = "";
    String key = stringMD5("M8sNOPRKSUXZ4TJIcfg56F9uQ");
    int x = 0;
    String tempStr = "";
    byte[] key2 = Base64.decode(url.getBytes(charEnCodeType), 0);
    String data = new String(key2, charEnCodeType);
    int length = data.length();
    int keyLength = key.length();
    for (int i = 0; i < length; ++i) {
      if (x == keyLength) {
        x = 0;
      }
      tempStr = tempStr + key.substring(x, x + 1);
      ++x;
    }
    for (int i = 0; i < length; ++i) {
      if (data.charAt(i) < tempStr.charAt(i)) {
        deCodeLinkStr = deCodeLinkStr + (char)(data.charAt(i) + 'Ā' - tempStr.charAt(i));
      }
      else
      {
        deCodeLinkStr = deCodeLinkStr + (char)(data.charAt(i) - tempStr.charAt(i));
      }
    }
    return deCodeLinkStr;
  }
 }

你可能感兴趣的:(一种可逆的加密算法,php描述加密,java描述解密)