出于需要,现在要把下面一段pascal代码转为java,本人对pascal不熟悉,想请教一下大家,谢谢!!代码如下:function Encrypt(const pre_code : string): string;
//加密
var
  strget, str_code : string;
  size,byte1,i,j,k,int_one,int_ten, ifok, int1, int2, int3,int4 : integer;
begin
  ifok:=0;   int1:=0;   int2:=0;
  size:=length(pre_code);
  if (size>6) or (size<4) then
  begin
    MymessageWarn('密码长度只能为四位到六位!');
    ifok := 1;            //ifok如果等于1,则加密过程不执行
  end;  i:=1;
  while i<=size do
  begin         //判断密码的每个字符是否都为字母和数字组成
    byte1 := byte(pre_code[i]);   //将字符转换为ASCII码
    if (byte1<48) or ((byte1>57) and (byte1<65)) or ((byte1>90) and (byte1<97)) or (byte1>122) then
    begin
      MymessageWarn('密码只能由字母和数字组成!');
      ifok := 1;     i:=size+1;   //跳出WHILE语句
    end;
    i := i+1;
  end;  if ifok = 0 then     //如果ifok等于0,则执行加密过程
  begin
    i:=1;
    while i<=size do
    begin
      strget:=copy(pre_code,i,1);       //将字符转换成为两位由数字组成的字符
      if strget='0' then str_code:='27';   if strget='1' then str_code:='88';
      if strget='2' then str_code:='39';   if strget='3' then str_code:='46';
      if strget='4' then str_code:='53';   if strget='5' then str_code:='79';
      if strget='6' then str_code:='87';   if strget='7' then str_code:='63';
      if strget='8' then str_code:='18';   if strget='9' then str_code:='99';
      if (strget='a') or (strget='A') then str_code:='86';
      if (strget='b') or (strget='B') then str_code:='23';
      if (strget='c') or (strget='C') then str_code:='11';
      if (strget='d') or (strget='D') then str_code:='49';
      if (strget='e') or (strget='E') then str_code:='72';
      if (strget='f') or (strget='F') then str_code:='34';
      if (strget='g') or (strget='G') then str_code:='43';
      if (strget='h') or (strget='H') then str_code:='15';             
      if (strget='i') or (strget='I') then str_code:='77';
      if (strget='j') or (strget='J') then str_code:='66';
      if (strget='k') or (strget='K') then str_code:='55';
      if (strget='l') or (strget='L') then str_code:='37';
      if (strget='m') or (strget='M') then str_code:='93';
      if (strget='n') or (strget='N') then str_code:='74';
      if (strget='o') or (strget='O') then str_code:='16';
      if (strget='p') or (strget='P') then str_code:='35';
      if (strget='q') or (strget='Q') then str_code:='58';
      if (strget='r') or (strget='R') then str_code:='71';
      if (strget='s') or (strget='S') then str_code:='92';
      if (strget='t') or (strget='T') then str_code:='83';
      if (strget='u') or (strget='U') then str_code:='67';
      if (strget='v') or (strget='V') then str_code:='38';
      if (strget='w') or (strget='W') then str_code:='64';
      if (strget='x') or (strget='X') then str_code:='56';
      if (strget='y') or (strget='Y') then str_code:='45';
      if (strget='z') or (strget='Z') then str_code:='24';
      int_one:=strtoint(copy(str_code,1,1));    //将密码分解为两个数字
      int_ten:=strtoint(copy(str_code,2,1));
      k:=1;   j:=1;
      while j<=size-i do
      begin
        k:=k*10;    j:=j+1;
      end;
      int1:=int1+int_one*k;
      int2:=int2+int_ten*k;
      i:=i+1;
    end;
    int1:=((int1)xor(190635))*421;                //数字运算加密
    int2:=((int2)xor(518154))*323;
    int1:=(int1)xor(423674161)+36846857;
    int2:=(int2)xor(448719254)+57412843;
    int3:=round((int1-(int1 mod 1000000))/1000000) mod 10;
    int4:=round((int2-(int2 mod 1000000))/1000000) mod 10;
    if (int3=0) then int1:=int1+600;  if (int3=1) then int1:=int1+1000;
    if (int3=2) then int1:=int1+300;  if (int3=4) then int1:=int1-4000;
    if (int3=5) then int1:=int1+200;  if (int3=6) then int1:=int1+3000;
    if (int3=7) then int1:=int1-300;  if (int3=8) then int1:=int1-7000;
    if (int3=9) then int1:=int1-100;  if (int4=0) then int2:=int2+4000;
    if (int4=1) then int2:=int2+400;  if (int4=3) then int2:=int2+5000;
    if (int4=4) then int2:=int2+500;  if (int4=5) then int2:=int2-4000;
    if (int4=6) then int2:=int2-300;  if (int4=8) then int2:=int2-2000;
    if (int4=9) then int2:=int2-900;
  end;
{  if length(inttostr(int2)) = 4 then
    result := inttostr(int1)+'0'+inttostr(int2);
  if length(inttostr(int2)) = 5 then
    result := inttostr(int1)+inttostr(int2);}
  if length(inttostr(int1)) = 4 then
  begin
    if length(inttostr(int2)) = 4 then
      result := '0'+inttostr(int1)+'0'+inttostr(int2);
    if length(inttostr(int2)) = 5 then
      result := '0'+inttostr(int1)+inttostr(int2);
  end
  else
  begin
    if length(inttostr(int2)) = 4 then
      result := inttostr(int1)+'0'+inttostr(int2);
    if length(inttostr(int2)) = 5 then
      result := inttostr(int1)+inttostr(int2);
  end; 
end;

解决方案 »

  1.   

       public String Encrypt(String pre_code) throws UnsupportedEncodingException{
         String strget= null, str_code = null;
         int int1 = 0, int2 = 0, int3 = 0, int4 = 0;
         int i = 0, j = 0, k = 0;
         int int_one = 0, int_ten = 0, ifok = 0;
         int size = 0, byte1 = 0;    
         String result = "";
         size = pre_code.length();
         if (size > 6 || size < 4){
         System.out.println("4 < size < 6");
         ifok = 1;
         }
         i = 1;
         while (i <= size){
         byte1 = pre_code.getBytes("GBK")[i];//replace by ur charset
         if ((byte1 < 48) 
         || ((byte1 > 57) && (byte1 < 65))
          || ((byte1>90) && (byte1<97)) 
          || (byte1>122)){
         System.out.println("must be num or chars");
         ifok = 1; 
         i = size+1; 
         }
         i+=1;
         }     if (ifok == 0){
         i = 0; //diff java <-> delphi
         while (i<=size){
         strget = pre_code.substring(i, i+1);
         if (strget=="0")
         str_code = "27";
         if (strget=="1")
         str_code = "88";
         if (strget=="2")
         str_code = "39";
         if (strget=="3")
         str_code = "46";
         if (strget=="4")
         str_code = "53";
         if (strget=="5")
         str_code = "79";
         if (strget=="6")
         str_code = "87";
         if (strget=="7")
         str_code = "63";
         if (strget=="8")
         str_code = "18";
         if (strget=="9")
             str_code = "99";
         if (strget.toLowerCase().equals("a"))
         str_code = "86";
         if (strget.toLowerCase().equals("b"))
         str_code = "23";
         if (strget.toLowerCase().equals("c"))
         str_code = "11";
         if (strget.toLowerCase().equals("d"))
         str_code = "49";
         //....
         int_one = Integer.parseInt(str_code.substring(0,1));
         int_ten = Integer.parseInt(str_code.substring(1,2));
         k=1; j=1;
              while (j<=size-i) {
              k=k*10; 
         j=j+1;
              }
              int1=int1+int_one*k;
              int2=int2+int_ten*k;
              i=i+1;
         }
         int1=((int1)^(190635))*421;
         int2=((int2)^(518154))*323;
         int1=(int1)^(423674161)+36846857;
         int2=(int2)^(448719254)+57412843;
         int3=Math.round((int1-(int1 % 1000000))/1000000) % 10;
         int4=Math.round((int2-(int2 % 1000000))/1000000) % 10;
         if (int3==0) int1=int1+600; if (int3==1)  int1=int1+1000;
         if (int3==2)  int1=int1+300; if (int3==4)  int1=int1-4000;
         if (int3==5)  int1=int1+200; if (int3==6)  int1=int1+3000;
         if (int3==7)  int1=int1-300; if (int3==8)  int1=int1-7000;
         if (int3==9)  int1=int1-100; if (int4==0)  int2=int2+4000;
         if (int4==1)  int2=int2+400; if (int4==3)  int2=int2+5000;
         if (int4==4)  int2=int2+500; if (int4==5)  int2=int2-4000;
         if (int4==6)  int2=int2-300; if (int4==8)  int2=int2-2000;
         if (int4==9)  int2=int2-900;
         }
        
         if (String.valueOf(int1).length() == 4){
         if (String.valueOf(int2).length() == 4) 
    result = '0'+String.valueOf(int1)+'0'+String.valueOf(int2);
         if (String.valueOf(int2).length() ==5)
    result = '0'+String.valueOf(int1)+String.valueOf(int2);
         }else {
         if (String.valueOf(int2).length() == 4)
         result = String.valueOf(int1)+'0'+String.valueOf(int2);
         if (String.valueOf(int2).length() ==5)
         result = String.valueOf(int1)+String.valueOf(int2);
         }
         return result;
        }