我正在尝试为 Blowfish 加密算法编写我的源代码。
尝试打印我的加密消息时,我得到一个空白输出。
例如:
Blowfish test = new Blowfish();
String message = test.encrypt("test", "password");
System.out.println("Output = " + message);
Output =
这是我的加密功能的代码:
public String encrypt(String input, String key) //Encrypting a string with a input key
{
init(key);
StringBuffer binaryInput = new StringBuffer(StringToBit(input));
while (binaryInput.length() % 64 != 0) {
binaryInput.insert(0, "0");
}
String[] blocks = new String[binaryInput.length() % 64];
for (int i = 0; i < blocks.length; i++) {
blocks[i] = binaryInput.substring(0, 64);
binaryInput.delete(0, 64);
}
StringBuffer enc = new StringBuffer();
int L;
int R;
for (int i = 0; i < blocks.length; i++) {
L = Integer.parseInt(blocks[i].substring(0, 32), 2);
R = Integer.parseInt(blocks[1].substring(33, 64), 2);
en(L, R);
enc.append(Integer.toHexString(L));
enc.append(Integer.toHexString(R));
//enc.append(" "); //Breaks every 64-Bits
}
reset();
return (enc.toString());
}
这是在加密函数中调用的函数
private void en(int L, int R) //Encrypting L-half bits and R-half bits for key.
{
for (int i=0 ; i<16 ; i += 2)
{
L ^= P[i];
R ^= f(L);
swap(L, R);
}
swap (L, R);
L ^= P[16];
R ^= P[17];
}