用Java复制PHP代码

时间:2016-05-07 15:40:26

标签: java php

我正在尝试将PHP中的以下代码翻译为Java。

# Create and setup a "Find Next" button
find_next_btn = QtGui.QPushButton("      Find &Next")
# setupButton is a small custom method to streamline setting up many buttons. See below.
setupButton(find_next_btn, 150, "Icons/arrow_right_cr.png", 30, 20, "RTL")
find_next_btn.setToolTip("Search DOWN the tree")
find_next_btn.clicked.connect(find_next)
# find_next is the method executed when the button is pressed

# Create an action for the additional shortcuts. Alt+N is already set
# by "&" in "Find &Next"
find_next_ret_act = QtGui.QAction(self, triggered=find_next_btn.animateClick)
find_next_ret_act.setShortcut(QtGui.QKeySequence("Return"))

find_next_enter_act = QtGui.QAction(self, triggered=find_next_btn.animateClick)
find_next_enter_act.setShortcut(QtGui.QKeySequence("Enter"))

# Now add (connect) these actions to the push button
find_next_btn.addActions([find_next_ret_act, find_next_enter_act])


# A method to streamline setting up multiple buttons
def setupButton(button, btn_w, image=None, icon_w=None, icon_h=None, layout_dir=None):
    button.setFixedWidth(btn_w)
    if image != None:            
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap(image))
        button.setIcon(icon)
    if icon_w != None:
        button.setIconSize(QtCore.QSize(icon_w, icon_h))
    if layout_dir == "RTL":
        find_next_btn.setLayoutDirection(QtCore.Qt.RightToLeft)

到目前为止,这就是我在Java中所拥有的。

private function _aes256_cbc_encrypt($key, $data, $iv) {
    if (32 !== strlen($key))
        $key = hash('SHA256', $key, true);
    if (16 !== strlen($iv))
        $iv = hash('MD5', $iv, true);
    $padding = 16 - (strlen($data) % 16);
    $data .= str_repeat(chr($padding), $padding);
    return mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $data, MCRYPT_MODE_CBC, $iv);
}

sha256和md5方法是:

 private String aes256_cbc_encrypt(String key, String data, String iv) throws Exception {
    if (32 != key.length())
        key = sha256(key);
    if (16 != iv.length())
        iv = md5(iv);

    char padding = (char) (16-(data.length() % 16));
    char[] paddingArray = new char[padding];
    Arrays.fill(paddingArray, padding);
    data = data + new String(paddingArray);
    String mcryptData = data;
    MCrypt mcrypt = new MCrypt();
    String encrypted = mcrypt.bytesToHex(mcrypt.encrypt(mcryptData));
    return encrypted;
}

我使用的mcrypt类(我在https://github.com/serpro/Android-PHP-Encrypt-Decrypt/blob/master/Java/src/com/serpro/library/String/MCrypt.java找到的)是

public static String md5(String input) throws NoSuchAlgorithmException{
    String result = input;
    if(input != null) {
        MessageDigest md = MessageDigest.getInstance("MD5"); 
        md.update(input.getBytes());
        BigInteger hash = new BigInteger(1, md.digest());
        result = hash.toString(16);
        while(result.length() < 32) {
            result = "0" + result;
        }
    }
    return result;
}

public static String sha256(String input) throws NoSuchAlgorithmException{
    String result = input;
    if(input != null) {
        MessageDigest md = MessageDigest.getInstance("MD5"); 
        md.update(input.getBytes());
        BigInteger hash = new BigInteger(1, md.digest());
        result = hash.toString(16);
        while(result.length() < 32) {
            result = "0" + result;
        }
    }
    return result;
}

但是这两个代码给了我不同的结果。任何人都可以解释为什么会这样。

0 个答案:

没有答案