简体   繁体   中英

How can I port this PHP line to Java?

I have a problem with AES encryption and the customer showed me their PHP server code for "decrypting". Curiously enough the exact code has been taken from SO (not surprising). I found this out because the code the customer passed me had the exact same comment! :)

Anyway, it's a piece of PHP code taken from this SO question.

I am trying to do the same with Java but I don't know what this exact line is adding:

$key = 'a16byteslongkey!';

$padded_key = $key . str_repeat(chr(0x00), 16); // Argh!

(note the // Argh! comment was not mine ;)

Is it trying to add chr(0x00) to make a 32 bytes key (because the $key is 16?) if so, how would I do the same in Java?

As Gareth stated this returns the character with ASCII code 0. Using this we can make a function which repeats a string:

public static String strRepeat(String toRepeat, int reps){
    //Sanity checks go here!
    StringBuilder sb = new StringBuilder();
    for(int x = 0; x < reps; x++){
        sb.append(toRepeat);
    }
    return sb.toString();
}

Now the line can be replaced with:

String paddedKey = key + strRepeat('\0', 16); // Argh!

chr(0x00)应该返回带有ASCII码0的字符,我认为它可以用Java中的'\\0'表示。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM