import javax.crypto.spec.SecretKeySpec;
import static javax.xml.bind.DatatypeConverter.*;
public class BlowFishTest {
private String encrypt(String skey, String unencryptedData) {
byte[] IV = hexStringToByteArray("0001020304050607");
byte[] data = hexStringToByteArray(unencryptedData);
try {
SecretKeySpec key = new SecretKeySpec(skey.getBytes("UTF-8"), "Blowfish");
Cipher cipher = Cipher.getInstance("Blowfish/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, key,
new javax.crypto.spec.IvParameterSpec(IV));
byte[] encrypted = cipher.doFinal(data);
return bytesToHex(encrypted);
} catch (Exception e) {
return e.toString();
}
}
private String decrypt(String skey, String encryptedData) {
byte[] IV = hexStringToByteArray("0001020304050607");
byte[] data = hexStringToByteArray(encryptedData);
try {
SecretKeySpec key = new SecretKeySpec(skey.getBytes("UTF-8"), "Blowfish");
Cipher cipher = Cipher.getInstance("Blowfish/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, key, new javax.crypto.spec.IvParameterSpec(IV));
byte[] decrypted = cipher.doFinal(data);
return bytesToHex(decrypted);
} catch (Exception e) {
return e.toString();
}
}
public byte[] hexStringToByteArray(String encryptedData) {
return parseHexBinary(encryptedData);
}
public String bytesToHex(byte[] decrypted) {
return printHexBinary(decrypted);
}
public static void main(String[] args) {
String myKey = "mySecret";
String message = "myTestingString";
BlowFishTest blowFishTest = new BlowFishTest();
String myHexString = blowFishTest.bytesToHex(message.getBytes());
String encryptedHex = blowFishTest.encrypt(myKey, myHexString);
String unencryptedHex = blowFishTest.decrypt(myKey, encryptedHex);
System.out.println(new String(blowFishTest.hexStringToByteArray(unencryptedHex)).equals(message));
}
}
true
К сожалению, я не могу воспроизвести проблему с предоставленными вами кодами. Вам может понадобиться проверить ввод, который является unencryptedData и методами hexStringToByteArray и bytesToHex.