package fgwspackage;public class Base64 {


/*****************************加密函数---开始***********************************/
//加密对照表
private static final byte[] encodingTable = {
(byte) 'A', (byte) 'B', (byte) 'C', (byte) 'D', (byte) 'E',
(byte) 'F', (byte) 'G', (byte) 'H', (byte) 'I', (byte) 'J',
(byte) 'K', (byte) 'L', (byte) 'M', (byte) 'N', (byte) 'O',
(byte) 'P', (byte) 'Q', (byte) 'R', (byte) 'S', (byte) 'T',
(byte) 'U', (byte) 'V', (byte) 'W', (byte) 'X', (byte) 'Y',
(byte) 'Z', (byte) 'a', (byte) 'b', (byte) 'c', (byte) 'd',
(byte) 'e', (byte) 'f', (byte) 'g', (byte) 'h', (byte) 'i',
(byte) 'j', (byte) 'k', (byte) 'l', (byte) 'm', (byte) 'n',
(byte) 'o', (byte) 'p', (byte) 'q', (byte) 'r', (byte) 's',
(byte) 't', (byte) 'u', (byte) 'v', (byte) 'w', (byte) 'x',
(byte) 'y', (byte) 'z', (byte) '0', (byte) '1', (byte) '2',
(byte) '3', (byte) '4', (byte) '5', (byte) '6', (byte) '7',
(byte) '8', (byte) '9', (byte) '+', (byte) '/'
};    //加密方法起始方法 一
public String internalEncrypt(String password,int pkey){

byte[] tmpBytes=password.getBytes();
    
int tmpLen = tmpBytes.length;
    
char[] resBytes = new char[tmpLen];

String result="";

for(int i=0 ; i<tmpLen ; i++ ){
       
resBytes[i]=(char)((tmpBytes[i])^(pkey>>8));  //key右移8位后异或

pkey = ((((int)(resBytes[i])+(pkey&0xffff))*C1)+C2)&0xffff;  //转化成无符号的整数

result=result+resBytes[i];

}        return result;

}
//加密方法起始方法 二
   public String postProcess(String pwdAndkey){
   
    String enKey="";
   
        while(pwdAndkey!=""){       
String tmpSS="";       
         if(pwdAndkey.length()>3) {
         tmpSS=pwdAndkey.substring(0,3);
pwdAndkey=pwdAndkey.substring(3);
         }else{
         tmpSS=pwdAndkey.substring(0);
pwdAndkey="";
         }
        enKey=enKey+encode(tmpSS);
        }
        
        return enKey;
   }
 
   //加密方法起始方法 三    
   public String encode(String tmpKey){
  String ret="";  
      int I=0;
  int tmpKLen=tmpKey.length(); 
  
  String str="";
  
  for(int m=tmpKLen-1;m>=0;m--){
   String x=Integer.toHexString((int)(tmpKey.toCharArray()[m]));
     
str=str+x;
  }
      I=Integer.parseInt(str,16);
  
  switch(tmpKLen){
   case 1: 
   ret=(char)encodingTable[I%64]+""+(char)encodingTable[(I>>6)%64];
      break;
    case 2:
   ret=(char)encodingTable[I%64]+""+(char)encodingTable[(I>>6)%64]+""+(char)encodingTable[(I>>12)%64];
       break;
    case 3:
   ret=(char)encodingTable[I%64]+""+(char)encodingTable[(I>>6)%64]+""+(char)encodingTable[(I>>12)%64]
       +""+(char)encodingTable[(I>>18)%64];
       break;
    default:
       break;
  }
  
  return ret;
   }
/*****************************加密函数---结束***********************************/
/*****************************解密函数---开始***********************************/
//解密对照表

private static final byte[] decodingTable ={
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 62, 0, 0, 0, 63, 52, 53,
54, 55, 56, 57, 58, 59, 60, 61, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2,
3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
20, 21, 22, 23, 24, 25, 0, 0, 0, 0, 0, 0, 26, 27, 28, 29, 30,
31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45,
46, 47, 48, 49, 50, 51, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0
};
   
   
   public String internalDecrypt(String enCodeKey,int vKey){
    
int tmpLen = enCodeKey.length();
    
char[] resBytes = new char[tmpLen];

String result="";

for(int i=0 ; i<tmpLen ; i++ ){

resBytes[i]=(char)(((int)enCodeKey.toCharArray()[i])^(vKey>>8));  //key右移8位后异或

vKey = ((((int)enCodeKey.toCharArray()[i]+(vKey&0xffff))*C1)+C2)&0xffff;  //转化成无符号的整数
result=result+resBytes[i];

}
  //System.out.println(result);
return result;
   }
   
   public String preProcess(String S){
String enKey="";
   
while(S!=""){       
String tmpSS="";       
if(S.length()>4) {
tmpSS=S.substring(0,4);
S=S.substring(4);
}else{
tmpSS=S.substring(0);
S="";
}
enKey=enKey+deCode(tmpSS);
}   
return enKey;
   }
   
   public String deCode(String S){
    int I=0;
    String res="";
    switch(S.length()){
       case 2:{
          I=decodingTable[S.getBytes()[0]]+(decodingTable[S.getBytes()[1]]<<6);
  res=move(I,S.length());
          break;
       }
       case 3:{
  I=decodingTable[S.getBytes()[0]]+(decodingTable[S.getBytes()[1]]<<6)+(decodingTable[S.getBytes()[2]]<<12);
  res=move(I,S.length());
          break;
       }
       case 4:{
  I=decodingTable[S.getBytes()[0]]+(decodingTable[S.getBytes()[1]]<<6)
    +(decodingTable[S.getBytes()[2]]<<12)+(decodingTable[S.getBytes()[3]]<<18);
  res=move(I,S.length());
          break;
       }
       default:break;
    }
    return res;
   
   }
   
   
   public String move(int I,int strLen){      String toHex=Integer.toHexString(I);    
      int tmpLen=strLen;
  String tmpStr = "";
  String tmpHex = "";     
      for(int k=tmpLen-1;k>0;k--){
       if(toHex.length()%2==0)
  tmpHex=toHex.substring(k*2-2,k*2);
else {
if((k*2-3)<0) tmpHex=toHex.substring(0,k*2-1);
else tmpHex=toHex.substring(k*2-3,k*2-1);

int tmpI=Integer.parseInt(tmpHex,16);
tmpStr=tmpStr+(char)tmpI;     
      }
      
      //System.out.println(tmpStr);
      return tmpStr;
   }
   
   
   //加密接口
   public String enCrypt(String enStr){
      String tmpCode=internalEncrypt(enStr,key);
      return postProcess(tmpCode);
   }
   
   //解密接口
   public String deCrypt(String enCodeKey){
   String tmpS=preProcess(enCodeKey);
   String ret=internalDecrypt(tmpS,key);    
   return ret;
   }  
   
   
   
/*****************************解密函数---结束***********************************/
public static void  main(String args[]){

Base64 secur = new Base64();        String tmpPwd=secur.internalEncrypt("123",key);
String enCode=secur.postProcess(tmpPwd);
    System.out.println("encoded is ======"+enCode);   
    String deCode=secur.deCrypt("1Imt"); 
    System.out.println("encoded is ======"+deCode); }  private static final int key = 1233;

private static final int C1 = 52845;

private static final int C2= 22719;

}我模仿写了个一个函数  但是得出的结果不同 
private static $key=1233;
    private static $C1=52845;
    private static $C2=22719;
public function internalEncrypt($password, $pkey)
{
           
        $tmpBytes= explode('|',$password);

for($i=0;$i<count($tmpBytes);$i++)
{ $tmpBytes[$i]=ord($tmpBytes[$i]);

}

//print_r($tmpBytes);
        $tmpLen=count($tmpBytes);
          //  echo $tmpLen;
$resBytes=array();
            
$result="";
//echo $tmpBytes[0];
for($i=0;$i<$tmpLen;$i++)
{
echo $pkey."......."; 
//echo $tmpBytes[i];
$resBytes[$i]=chr(($tmpBytes[$i])^($pkey>>8));
            echo $pkey."***********"; 
//echo $resBytes[$i];
//echo $pkey;
//echo(($tmpBytes[$i])^($pkey>>8));
//echo $resBytes[$i];
//echo ord($resBytes[$i]);
echo ord($resBytes[$i])."################";

$pkey=(((ord($resBytes[$i])+($pkey & (65535 )))*$C1)+$C2) &(65535 ); 
   echo $pkey."-------------------"; 
$result=$result.$resBytes[$i];
} //echo "funfun";
            echo $result;
}