Decrypting in Java with Blowfish

后端 未结 6 2043
独厮守ぢ
独厮守ぢ 2021-02-06 18:41

Hullo,

I am encrypting and decrypting in Java with Blowfish.

The encryption works fine, but the decryption fails.

Here is my Java code for decrypting :

6条回答
  •  天命终不由人
    2021-02-06 19:17

    Converting bytes to hex and back is tricky. This should solve your problem. (You need to fix your string representation of encryptedString)

    Output:

    StackOverflow 537461636B4F766572666C6F77 [83, 116, 97, 99, 107, 79, 118, 101, 114, 102, 108, 111, 119]
    J~3¹ÙÂÖ"¢ª„¨u 194A7E33B9060CD9C2D622A2AA84A875 [25, 74, 126, 51, -71, 6, 12, -39, -62, -42, 34, -94, -86, -124, -88, 117]
    StackOverflow 537461636B4F766572666C6F77 [83, 116, 97, 99, 107, 79, 118, 101, 114, 102, 108, 111, 119]
    

    Code:

    import java.util.Arrays;
    import javax.crypto.Cipher;
    import javax.crypto.KeyGenerator;
    import javax.crypto.SecretKey;
    import javax.crypto.spec.SecretKeySpec;
    
    public class Main {
    
      public static void main(String[] args) throws Exception {
    
        KeyGenerator keygenerator = KeyGenerator.getInstance("Blowfish");
        SecretKey secretkey = keygenerator.generateKey();
    
        String plaintextString = "StackOverflow";
        System.out.println(plaintextString + " " + bytesToHex(plaintextString.getBytes()) + " " + Arrays.toString(plaintextString.getBytes()));
    
        SecretKeySpec key = new SecretKeySpec(secretkey.getEncoded(), "Blowfish");
        Cipher cipher = Cipher.getInstance("Blowfish");
    
        cipher.init(Cipher.ENCRYPT_MODE, key);
        byte[] encrypted = cipher.doFinal(plaintextString.getBytes());
        String encryptedString = bytesToHex(encrypted);
        System.out.println(new String(encrypted) + " " + encryptedString + " " + Arrays.toString(encrypted));
    
        cipher.init(Cipher.DECRYPT_MODE, key);
        byte[] decrypted = cipher.doFinal(hexToBytes(encryptedString));
        String decryptedString = bytesToHex(decrypted);
        System.out.println(new String(decrypted) + " " + decryptedString + " " + Arrays.toString(decrypted));
    
      }
    
      public static byte[] hexToBytes(String str) {
        if (str == null) {
          return null;
        } else if (str.length() < 2) {
          return null;
        } else {
          int len = str.length() / 2;
          byte[] buffer = new byte[len];
          for (int i = 0; i < len; i++) {
            buffer[i] = (byte) Integer.parseInt(str.substring(i * 2, i * 2 + 2), 16);
          }
          return buffer;
        }
    
      }
    
      public static String bytesToHex(byte[] data) {
        if (data == null) {
          return null;
        } else {
          int len = data.length;
          String str = "";
          for (int i = 0; i < len; i++) {
            if ((data[i] & 0xFF) < 16)
              str = str + "0" + java.lang.Integer.toHexString(data[i] & 0xFF);
            else
              str = str + java.lang.Integer.toHexString(data[i] & 0xFF);
          }
          return str.toUpperCase();
        }
      }
    }
    

提交回复
热议问题