我照着现有的java程序写了一个加密算法,有点问题
这是java程序
        public String encryption(String sSource)
        throws Exception
        {
        int n;
        int code; //byte
        String des;
        String strKey;
        int num1,num2;
        char charcode;
              String EncodeStr; //返回值        try {
                if (sSource.length()==0) {
                        EncodeStr = "";
                        return EncodeStr;
                }
                strKey = ENCODE_KEY1 + ENCODE_KEY2 + ENCODE_KEY3 + ENCODE_KEY4 + ENCODE_KEY5 + ENCODE_KEY6;
                int onenum=1;
                char onestr=(char) onenum;
                while (sSource.length() < 8 )
                        sSource = sSource + onestr;
                des = "";
                for (n=1 ; n <= sSource.length();n++)
                {
                    while (true)
                            {
                            double myrandom=Math.random();                            
                        code =(int)  Math.round(myrandom * 100);
                        num1 = sSource.charAt(n-1);
                        while ( (code > 0) && ( ( (code ^ num1 )< 0) || ((code ^ num1) > 90)) )
                            code = code - 1;
                        charcode=(char) code;
                        if ( (code > 35) && (code < 122) && (charcode != '|') && (charcode != '\'') && (charcode != ',') && (strKey.charAt(code ^ num1) != '|') && (strKey.charAt(code ^ num1) != '\'')  && (strKey.charAt(code ^ num1) != ',') )
                                 break;
                            }
                    charcode=(char) code;
                    //System.out.println("c="+charcode);
                    //确保生成的字符是可见字符并且在SQL语句中有效
                    num2 = sSource.charAt(n-1);
                    des = des + charcode + strKey.charAt(code ^ num2);
                }
                EncodeStr = des;
        } catch (Exception ex) {
                            EncodeStr="Exception/Message:" + ex.getMessage();        }
        return EncodeStr;
        }
这是我写得delphi程序
function encryption(sSource:string):string;//加密
var strKey,des,ENCODE_KEY1,ENCODE_KEY2,ENCODE_KEY3,
    ENCODE_KEY4,ENCODE_KEY5,ENCODE_KEY6,EncodeStr:string;
    n,code,num1,num2,temp:integer;
    xorstr,onestr,charcode:char;
begin
  ENCODE_KEY1:='zxcvbnm,./asdfg';
  ENCODE_KEY2:='hjkl;'+''''+'qwertyuiop';
  ENCODE_KEY3:='[]\1234567890-';
  ENCODE_KEY4:='=` ZXCVBNM<>?:LKJ';
  ENCODE_KEY5:='HGFDSAQWERTYUI';
  ENCODE_KEY6:='OP{}|+_)(*&^%$#@!~';
  strKey:=ENCODE_KEY1+ENCODE_KEY2+ENCODE_KEY3+ENCODE_KEY4
          +ENCODE_KEY5+ENCODE_KEY6;
  temp:=1;
  code:=0;
 if Length(sSource)=0 then
    begin
      EncodeStr:='';
    end
    else
     begin
      onestr:='';
      while Length(sSource)<8 do sSource:=sSource+onestr;
      des:='';
      for n:=1 to Length(sSource) do
        begin
          while code=0 do code:=Random(100);
          temp:=temp+1;
          num1:=ord(copy(sSource,n,1)[1]);
          while (code>0)and (((code xor num1)<0) or ((code xor num1)>90)) do
              code:=code-1;
              charcode:=chr(code);
          if (code>35) and (code<122) and (charcode<>'|')
              and (charcode<>'\') and (charcode<>',')
              and (copy(strKey,code xor num1,1)<>'|')
              and (copy(strKey,code xor num1,1)<>'\')
              and (copy(strKey,code xor num1,1)<>',')
              then
              break;
           charcode:=chr(code);
           num2:=ord(copy(sSource,n,1)[1]);
           des:=des+charcode+copy(sSource,code xor num2,1);
           showmessage(des);
        end;
     end;
     EncodeStr := des;          
end;
我的程序每次都返回一个空值,不知道哪有问题

解决方案 »

  1.   

    function Encryption(sSource: string): string;//加密
    const
      ENCode_KEY1 = 'zxcvbnm,./asdfg';
      ENCode_KEY2 = 'hjkl;' + '''' + 'qwertyuiop';
      ENCode_KEY3 = '[]\1234567890-';
      ENCode_KEY4 = '=` ZXCVBNM<>?:LKJ';
      ENCode_KEY5 = 'HGFDSAQWERTYUI';
      ENCode_KEY6 = 'OP{}|+_)(*&^%$#@!~';
      strKey = ENCode_KEY1 + ENCode_KEY2 + ENCode_KEY3 + ENCode_KEY4
              + ENCode_KEY5 + ENCode_KEY6;
      OneStr = #1;
    var 
      Des: string;
      n, Code, Num1, Num2: Integer;
      CharCode: Char;
    begin
      Randomize;
      Temp := 1;
      if Length(sSource) = 0 then
      begin
        Result := '';
        Exit;
      end else begin
        while Length(sSource) < 8 do 
          sSource := sSource + OneStr;
        Des := '';
        for n := 1 to Length(sSource) do
        begin
          while True do 
          begin
            Code := Random(100);
            Num1 := Ord(Copy(sSource, n, 1)[1]);
            while (Code > 0) and (((Code xor Num1) < 0) or ((Code xor Num1) > 90)) do
              Code := Code - 1;
              CharCode := Chr(Code);
              if (Code > 35) and (Code < 122) and (CharCode <> '|')
                  and (CharCode <> '\') and (CharCode <> ',')
                  and (Copy(strKey, Code xor Num1, 1) <> '|')
                  and (Copy(strKey, Code xor Num1, 1) <> '\')
                  and (Copy(strKey, Code xor Num1, 1) <> ',')
                  then
                break;
          end;
          CharCode := Chr(Code);
          Num2 := Ord(Copy(sSource,n,1)[1]);
          Des := Des + CharCode + Copy(sSource, Code xor Num2, 1);
          ShowMessage(des);
        end;
        Result := Des;
      end;
    end;
      

  2.   

    procedure FileXor(qFileName : String);   //加解密函数
    var
        fd : array[1..1024] of byte;
        hf : integer;
        br : Integer;
        i : Integer;
    begin
        hf:=fileopen(qFileName,fmOpenReadWrite);
       if hf <> -1 then
        begin
             FileSeek(hf,0,0);
             br :=FileRead(hf, fd, 1024);
             for i := 1 to br do
             begin
                fd[i] := (fd[i]) xor (i mod $ff);
             end;
             FileSeek(hf,0,0);
             filewrite(hf,fd, br);
        end;
        FileClose(hf);
    end;
      

  3.   

    function decryption(varCode:string):string;
    var des:string;
        midNum,n,num1,num2,num3,i:integer;
        xorstr,onestr,temp1:char;
        DecodeStr:string;
    begin
      if length(varCode)=0 then decryption:=''
        else
          begin
            if length(varCode) mod 2 =1 then
              varCode := varCode + '?';
            des:='';
            midNum:= round((length(varCode) div 2)-1);
            for n:=0 to midNum do
              begin
                num1:=ord(copy(varCode,n*2+1,1)[1]);
                if strcomp(PChar(strKey),PChar(copy(varCode,n*2+1+1,1)))<0 then
                    num2:=-1
                   else
                    num2:=pos(copy(varCode,n*2+2,1)[1],strKey)-1;
                num3:=num1 xor num2; //num1,num2按位异或
                xorstr:=chr(num3);
                des := des + xorstr;
              end;
              onestr:='';
              n:=pos(onestr,des);
           if n>0 then
             DecodeStr:=leftstr(des,n-1)
             else
             DecodeStr:=des;
          end;
          decryption:=DecodeStr;
    end;
    我的解密函数已经写好了
    但加密函数有问题
      

  4.   

    我现在要使用一个java开发好的系统,其中的数据库
    它的解密我已经写好了,就是加密老有问题,高手
    请指点一下
      

  5.   

    unit md5_test_main;// -----------------------------------------------------------------------------------------------
    INTERFACE
    // -----------------------------------------------------------------------------------------------uses
     Windows, SysUtils, Classes, Forms, Dialogs, Controls, StdCtrls, ComCtrls;type
     TMain = class(TForm)
       Pages: TPageControl;
       TabString: TTabSheet;
       TabFile: TTabSheet;
       TabTestSuite: TTabSheet;
       TabTimeTrial: TTabSheet;
       SLabel: TLabel;
       SEdit: TEdit;
       SDigest: TButton;
       FLabel: TLabel;
       FEdit: TEdit;
       FSelect: TButton;
       FDigest: TButton;
       TSExecute: TButton;
       TTLabel1: TLabel;
       TTLength: TEdit;
       TTLabel2: TLabel;
       TTCount: TEdit;
       TTStart: TButton;
       Display: TMemo;
       ButtonClear: TButton;
       ButtonClose: TButton;
       OpenDlg: TOpenDialog;
       procedure SDigestClick(Sender: TObject);
       procedure FSelectClick(Sender: TObject);
       procedure FDigestClick(Sender: TObject);
       procedure TSExecuteClick(Sender: TObject);
       procedure TTKeyPress(Sender: TObject; var Key: Char);
       procedure TTStartClick(Sender: TObject);
       procedure ButtonClearClick(Sender: TObject);
       procedure ButtonCloseClick(Sender: TObject);
     end;var
     Main: TMain;// -----------------------------------------------------------------------------------------------
    IMPLEMENTATION
    // -----------------------------------------------------------------------------------------------{$R *.DFM}uses
     md5;function LogEntry(Cmd, Msg: string; Dig: MD5Digest): string;
    begin
     Result := Format('%s(''%s'') =' + #13#10 + '   %s', [Cmd, Msg, MD5Print(Dig)]);
    end;procedure TMain.SDigestClick(Sender: TObject);
    begin
     with Display.Lines do begin
       if Count > 0 then Add('');
       Add(LogEntry('MD5String', SEdit.Text, MD5String(SEdit.Text)));
     end;
    end;procedure TMain.FSelectClick(Sender: TObject);
    begin
     with OpenDlg do if Execute then FEdit.Text := FileName;
    end;procedure TMain.FDigestClick(Sender: TObject);
    begin
     Screen.Cursor := crHourGlass;
     with Display.Lines do begin
       if Count > 0 then Add('');
       Add(LogEntry('MD5File', FEdit.Text, MD5File(FEdit.Text)));
     end;
     Screen.Cursor := crDefault;
    end;procedure TMain.TSExecuteClick(Sender: TObject);
    var
     I: byte;
    const
     Suite: array[1..7] of string = (
       '', 'a', 'abc', 'message digest', 'abcdefghijklmnopqrstuvwxyz',
       'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',
       '12345678901234567890123456789012345678901234567890123456789012345678901234567890'
     );
    begin
     with Display.Lines do begin
       if Count > 0 then Add('');
       Add('Executing RFC 1321 test suite ...');
       for I := 1 to 7 do Add(LogEntry('   MD5', Suite[I], MD5String(Suite[I])));
     end;
    end;procedure TMain.TTKeyPress(Sender: TObject; var Key: Char);
    begin
     if not (Key in [#8, '0'..'9']) then Key := #0;
    end;procedure TMain.TTStartClick(Sender: TObject);
    var
     BLength, BCount: longint;
     Block: pointer;
     B: pByte;
     I: longint;
     Ticks: longword;
     Context: MD5Context;
     Digest: MD5Digest;
     Speed: string;
    begin
     Screen.Cursor := crHourGlass;
     BLength := strToIntDef(copy(TTLength.Text, 1, 5), 0);
     BCount := strToIntDef(copy(TTCount.Text, 1, 5), 0);
     if BLength < 0 then BLength := 0;
     if BCount < 0 then BCount := 0;
     with Display.Lines do begin
       if Count > 0 then Add('');
       Add(Format('Digesting %0.0n %0.0n-byte blocks ...', [BCount + 0.1, BLength + 0.1]));
       GetMem(Block, BLength + 1);
       try
         B := Block;
         I := 0;
         while I < BLength do begin
           B^ := I and $ff;
           inc(B);
           inc(I);
         end;
         Ticks := GetTickCount;
         MD5Init(Context);
         I := 0;
         while I < BCount do begin
           MD5Update(Context, Block, BLength);
           inc(I);
         end;
         MD5Final(Context, Digest);
         Ticks := GetTickCount - Ticks;
         if (BCount * BLength = 0) or (Ticks = 0) then Speed := 'n/a'
         else Speed := Format('%.0n bytes/second', [(BCount * BLength) / (Ticks / 1000)]);
         Add(Format('   Digest = %s', [MD5Print(Digest)]));
         Add(Format('   Time   = %.3n seconds', [Ticks / 1000]));
         Add(Format('   Speed  = %s', [Speed]));
       finally
         FreeMem(Block);
       end;
     end;
     Screen.Cursor := crDefault;
    end;procedure TMain.ButtonClearClick(Sender: TObject);
    begin
     Display.Lines.Clear;
    end;procedure TMain.ButtonCloseClick(Sender: TObject);
    begin
     Close;
    end;end.
      

  6.   

    unit md5;// -----------------------------------------------------------------------------------------------
    INTERFACE
    // -----------------------------------------------------------------------------------------------uses
     Windows;type
     MD5Count = array[0..1] of DWORD;
     MD5State = array[0..3] of DWORD;
     MD5Block = array[0..15] of DWORD;
     MD5CBits = array[0..7] of byte;
     MD5Digest = array[0..15] of byte;
     MD5Buffer = array[0..63] of byte;
     MD5Context = record
       State: MD5State;
       Count: MD5Count;
       Buffer: MD5Buffer;
     end;procedure MD5Init(var Context: MD5Context);
    procedure MD5Update(var Context: MD5Context; Input: pChar; Length: longword);
    procedure MD5Final(var Context: MD5Context; var Digest: MD5Digest);function MD5String(M: string): MD5Digest;
    function MD5File(N: string): MD5Digest;
    function MD5Print(D: MD5Digest): string;function MD5Match(D1, D2: MD5Digest): boolean;// -----------------------------------------------------------------------------------------------
    IMPLEMENTATION
    // -----------------------------------------------------------------------------------------------var
     PADDING: MD5Buffer = (
       $80, $00, $00, $00, $00, $00, $00, $00,
       $00, $00, $00, $00, $00, $00, $00, $00,
       $00, $00, $00, $00, $00, $00, $00, $00,
       $00, $00, $00, $00, $00, $00, $00, $00,
       $00, $00, $00, $00, $00, $00, $00, $00,
       $00, $00, $00, $00, $00, $00, $00, $00,
       $00, $00, $00, $00, $00, $00, $00, $00,
       $00, $00, $00, $00, $00, $00, $00, $00
     );function F(x, y, z: DWORD): DWORD;
    begin
     Result := (x and y) or ((not x) and z);
    end;function G(x, y, z: DWORD): DWORD;
    begin
     Result := (x and z) or (y and (not z));
    end;function H(x, y, z: DWORD): DWORD;
    begin
     Result := x xor y xor z;
    end;function I(x, y, z: DWORD): DWORD;
    begin
     Result := y xor (x or (not z));
    end;procedure rot(var x: DWORD; n: BYTE);
    begin
     x := (x shl n) or (x shr (32 - n));
    end;procedure FF(var a: DWORD; b, c, d, x: DWORD; s: BYTE; ac: DWORD);
    begin
     inc(a, F(b, c, d) + x + ac);
     rot(a, s);
     inc(a, b);
    end;procedure GG(var a: DWORD; b, c, d, x: DWORD; s: BYTE; ac: DWORD);
    begin
     inc(a, G(b, c, d) + x + ac);
     rot(a, s);
     inc(a, b);
    end;procedure HH(var a: DWORD; b, c, d, x: DWORD; s: BYTE; ac: DWORD);
    begin
     inc(a, H(b, c, d) + x + ac);
     rot(a, s);
     inc(a, b);
    end;procedure II(var a: DWORD; b, c, d, x: DWORD; s: BYTE; ac: DWORD);
    begin
     inc(a, I(b, c, d) + x + ac);
     rot(a, s);
     inc(a, b);
    end;// -----------------------------------------------------------------------------------------------// Encode Count bytes at Source into (Count / 4) DWORDs at Target
    procedure Encode(Source, Target: pointer; Count: longword);
    var
     S: PByte;
     T: PDWORD;
     I: longword;
    begin
     S := Source;
     T := Target;
     for I := 1 to Count div 4 do begin
       T^ := S^;
       inc(S);
       T^ := T^ or (S^ shl 8);
       inc(S);
       T^ := T^ or (S^ shl 16);
       inc(S);
       T^ := T^ or (S^ shl 24);
       inc(S);
       inc(T);
     end;
    end;// Decode Count DWORDs at Source into (Count * 4) Bytes at Target
    procedure Decode(Source, Target: pointer; Count: longword);
    var
     S: PDWORD;
     T: PByte;
     I: longword;
    begin
     S := Source;
     T := Target;
     for I := 1 to Count do begin
       T^ := S^ and $ff;
       inc(T);
       T^ := (S^ shr 8) and $ff;
       inc(T);
       T^ := (S^ shr 16) and $ff;
       inc(T);
       T^ := (S^ shr 24) and $ff;
       inc(T);
       inc(S);
     end;
    end;// Transform State according to first 64 bytes at Buffer
    procedure Transform(Buffer: pointer; var State: MD5State);
    var
     a, b, c, d: DWORD;
     Block: MD5Block;
    begin
     Encode(Buffer, @Block, 64);
     a := State[0];
     b := State[1];
     c := State[2];
     d := State[3];
     FF (a, b, c, d, Block[ 0],  7, $d76aa478);
     FF (d, a, b, c, Block[ 1], 12, $e8c7b756);
     FF (c, d, a, b, Block[ 2], 17, $242070db);
     FF (b, c, d, a, Block[ 3], 22, $c1bdceee);
     FF (a, b, c, d, Block[ 4],  7, $f57c0faf);
     FF (d, a, b, c, Block[ 5], 12, $4787c62a);
     FF (c, d, a, b, Block[ 6], 17, $a8304613);
     FF (b, c, d, a, Block[ 7], 22, $fd469501);
     FF (a, b, c, d, Block[ 8],  7, $698098d8);
     FF (d, a, b, c, Block[ 9], 12, $8b44f7af);
     FF (c, d, a, b, Block[10], 17, $ffff5bb1);
     FF (b, c, d, a, Block[11], 22, $895cd7be);
     FF (a, b, c, d, Block[12],  7, $6b901122);
     FF (d, a, b, c, Block[13], 12, $fd987193);
     FF (c, d, a, b, Block[14], 17, $a679438e);
     FF (b, c, d, a, Block[15], 22, $49b40821);
     GG (a, b, c, d, Block[ 1],  5, $f61e2562);
     GG (d, a, b, c, Block[ 6],  9, $c040b340);
     GG (c, d, a, b, Block[11], 14, $265e5a51);
     GG (b, c, d, a, Block[ 0], 20, $e9b6c7aa);
     GG (a, b, c, d, Block[ 5],  5, $d62f105d);
     GG (d, a, b, c, Block[10],  9,  $2441453);
     GG (c, d, a, b, Block[15], 14, $d8a1e681);
     GG (b, c, d, a, Block[ 4], 20, $e7d3fbc8);
     GG (a, b, c, d, Block[ 9],  5, $21e1cde6);
     GG (d, a, b, c, Block[14],  9, $c33707d6);
     GG (c, d, a, b, Block[ 3], 14, $f4d50d87);
     GG (b, c, d, a, Block[ 8], 20, $455a14ed);
     GG (a, b, c, d, Block[13],  5, $a9e3e905);
     GG (d, a, b, c, Block[ 2],  9, $fcefa3f8);
     GG (c, d, a, b, Block[ 7], 14, $676f02d9);
     GG (b, c, d, a, Block[12], 20, $8d2a4c8a);
     HH (a, b, c, d, Block[ 5],  4, $fffa3942);
     HH (d, a, b, c, Block[ 8], 11, $8771f681);
     HH (c, d, a, b, Block[11], 16, $6d9d6122);
     HH (b, c, d, a, Block[14], 23, $fde5380c);
     HH (a, b, c, d, Block[ 1],  4, $a4beea44);
     HH (d, a, b, c, Block[ 4], 11, $4bdecfa9);
     HH (c, d, a, b, Block[ 7], 16, $f6bb4b60);
     HH (b, c, d, a, Block[10], 23, $bebfbc70);
     HH (a, b, c, d, Block[13],  4, $289b7ec6);
     HH (d, a, b, c, Block[ 0], 11, $eaa127fa);
     HH (c, d, a, b, Block[ 3], 16, $d4ef3085);
     HH (b, c, d, a, Block[ 6], 23,  $4881d05);
     HH (a, b, c, d, Block[ 9],  4, $d9d4d039);
     HH (d, a, b, c, Block[12], 11, $e6db99e5);
     HH (c, d, a, b, Block[15], 16, $1fa27cf8);
     HH (b, c, d, a, Block[ 2], 23, $c4ac5665);
     II (a, b, c, d, Block[ 0],  6, $f4292244);
     II (d, a, b, c, Block[ 7], 10, $432aff97);
     II (c, d, a, b, Block[14], 15, $ab9423a7);
     II (b, c, d, a, Block[ 5], 21, $fc93a039);
     II (a, b, c, d, Block[12],  6, $655b59c3);
     II (d, a, b, c, Block[ 3], 10, $8f0ccc92);
     II (c, d, a, b, Block[10], 15, $ffeff47d);
     II (b, c, d, a, Block[ 1], 21, $85845dd1);
     II (a, b, c, d, Block[ 8],  6, $6fa87e4f);
     II (d, a, b, c, Block[15], 10, $fe2ce6e0);
     II (c, d, a, b, Block[ 6], 15, $a3014314);
     II (b, c, d, a, Block[13], 21, $4e0811a1);
     II (a, b, c, d, Block[ 4],  6, $f7537e82);
     II (d, a, b, c, Block[11], 10, $bd3af235);
     II (c, d, a, b, Block[ 2], 15, $2ad7d2bb);
     II (b, c, d, a, Block[ 9], 21, $eb86d391);
     inc(State[0], a);
     inc(State[1], b);
     inc(State[2], c);
     inc(State[3], d);
    end;