delphi加密转换Java代码 [英] delphi encrypt convert Java Code

查看:118
本文介绍了delphi加密转换Java代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将Delphi编写的特定代码转换为JAVA?

How can I convert the specific code written in Delphi to JAVA?

Delphi代码是加密代码。

Delphi code is encrypt code.

function Encrypt(const S: String; Key1, Key2, Key3: WORD): String;
var
  i: Byte;
  FirstResult: String;
begin
  SetLength(FirstResult, Length(S));
  for i:=1 to Length(S) do begin
    FirstResult[i]:=Char(Byte(S[i]) xor (Key1 shr 8));
    Key1          :=(Byte(FirstResult[i])+Key1)*Key2+Key3;
  end;
  Result:=ValueToHex(FirstResult);
end;

function ValueToHex(const S: String): String;
var i: Integer;
begin
  SetLength(Result, Length(S)*2); 
  for i:=0 to Length(S)-1 do begin
    Result[(i*2)+1]:=HexaChar[Integer(S[i+1]) shr 4];
    Result[(i*2)+2]:=HexaChar[Integer(S[i+1]) and $0f];
  end;
end;

所以我试图制作源代码。在这里

so I was try to make source code. it's here

int key1=11;        int key2=22;        int key3=33;

String value = "ABCDE";
for(int i=0; i< value.length(); i++){
    byte[] bValue = value.substring(i).getBytes();
    int[] rValue = {0};
    rValue[0] = bValue[0]^(key1>>8);
    key1 = (bValue[0]+key1)*key2+key3;
    System.out.print(ValueToHex(rValue));
}       

但不同的结果。

key1 = 11,key2 = 22,key3 = 33;

key1 = 11, key2 = 22, key3 = 33;

value : "ABCDE"
delphi encrypt : 4144DB69BF
java encrypt   : 4144DB0901

不匹配

value : "ABC"
delphi encrypt : 4144DB
java encrypt   : 4144DB

一致

为什么不符合长?

推荐答案

有两个不同的错误。

首先,更新 key1 必须使用 rValue 而不是 bValue

Firstly, the updating of key1 must use rValue rather than bValue.

其次,Delphi代码在 key1 的上下文中执行算术c> Word 它是一个2字节的无符号整数。但是Java代码在 int 的上下文中执行相同的计算,这是一个4字节的有符号整数。

And secondly, the Delphi code performs arithmetic on key1 in the context of Word which is a 2 byte unsigned integer. But the Java code performs the same calculations in the context of int which is a 4 byte signed integer.

修复这个我相信你只需要使用4字节签名执行算术,然后将$ code> key1 截断为2字节值。像这样:

To fix this I believe you simply need to perform the arithmetic using 4 byte signed and then truncate key1 to a 2 byte value. Like this:

key1 = ((rValue[0]+key1)*key2+key3) & 0xffff;

我也认为可以大大简化Java代码。我知道Java没有什么关系,所以我相信一个熟练的Java专家可以做得比这更好:

I also think that you can simplify the Java code considerably. I know next to nothing about Java and so I'm sure a skilled Java expert could do very much better than this:

class SO15885898 {

    private static String ValueToHex(int myInt)
    {
        StringBuilder sb = new StringBuilder();
        sb.append(Integer.toHexString(myInt & 0xff));
        if (sb.length() < 2) {
            sb.insert(0, '0'); // pad with leading zero if needed
        }
        return sb.toString();
    }

    public static void main(String[] args)
    {
        int key1=11;        
        int key2=22;        
        int key3=33;

        String value = "ABCDE";
        for(int i=0; i<value.length(); i++){
            byte bValue = value.substring(i).getBytes()[0];
            int rValue = bValue^(key1>>8);
            key1 = ((rValue+key1)*key2+key3) & 0xffff;
            System.out.print(ValueToHex(rValue));
        }
    }
}

输出:


4144db69bf






<在更一般的注意事项上,既然你有两个代码,你应该安排这两个代码在组织中尽可能接近。然后打印尽可能多的诊断程序,以确定首先出现差异的计算步骤。


On a more general note, since you have both codes, you should arrange that both codes are as close to each other in organisation as possible. And then print off as much diagnostics as possible to pinpoint the calculation step where differences first appear.

这篇关于delphi加密转换Java代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆