2017-07-27 49 views
-2

我嘗試解碼十六進制編碼結果但不成功 字符串本碼編碼:9041 解碼後的字符串[B @ 1f2be27 請幫我。用java解碼十六進制

public static void main(String[] args) throws Exception { 

    String text = "FEBA4C2EDD47FD32"; 
    String encode = encryptData("9041","747062616e6b6570696e6e6577696e746567726174696f6e",168,true); 
    String result = decryptData("FEBA4C2EDD47FD32", "747062616e6b6570696e6e6577696e746567726174696f6e", 168, false); 
    System.out.println(encode); 
    System.out.println(result); // this is a byte array, you'll just see a reference to an array 

} 

// endcode功能 公共靜態字符串encryptData(字符串明文,String鍵,整數密鑰大小,布爾paddingEnable) 拋出異常{ 字節[] clearData = plainText.getBytes(); return encryptData(clearData,key,keySize,paddingEnable); }

public static String encryptData(byte[] clearData, String key, int keySize, boolean paddingEnable) 
     throws Exception { 
    byte[] keyBytes = getEncryptionKey(key, keySize); 
    SecretKey secretKey = new SecretKeySpec(keyBytes, "DESede"); 
    String algo = "DESede/ECB/pkcs5padding"; 
    Cipher cipher = Cipher.getInstance(algo); 
    cipher.init(Cipher.ENCRYPT_MODE, secretKey); 
    byte[] cipherText = cipher.doFinal(clearData); 
     System.out.println(clearData.toString()); 
    return String.valueOf(Hex.encodeHex(cipherText, false)); 
} 

public static String decryptData(String clearData, String key, int keySize, boolean paddingEnable) 
     throws Exception { 
    byte[] keyBytes = getEncryptionKey(key, keySize); 
    SecretKey secretKey = new SecretKeySpec(keyBytes, "DESede"); 
    String algo = "DESede/ECB/pkcs5padding"; 
    Cipher cipher = Cipher.getInstance(algo); 
    byte[] stringDecode = Hex.decodeHex(clearData.toCharArray()); 
    cipher.init(Cipher.DECRYPT_MODE, secretKey); 
    byte[] cipherText = cipher.doFinal(stringDecode); 
    return cipherText.toString(); 
} 


private static byte[] getEncryptionKey(String keyString, int keySize) 
     throws Exception { 
    int keyLength = keyString.length(); 
    switch (keySize) { 
     case 56: 
      if (keyLength != 16) { 
       throw new InvalidKeyException("Hex Key length should be 16 for a 56 Bit Encryption, found [" + keyLength + "]"); 
      } 
      break; 
     case 112: 
      if (keyLength != 32) { 
       throw new InvalidKeyException("Hex Key length should be 32 for a 112 Bit Encryption, found[" + keyLength + "]"); 
      } 
      break; 
     case 168: 
      if ((keyLength != 32) && (keyLength != 48)) { 
       throw new InvalidKeyException("Hex Key length should be 32 or 48 for a 168 Bit Encryption, found[" + keyLength + "]"); 
      } 
      if (keyLength == 32) { 
       keyString = keyString + keyString.substring(0, 16); 
      } 
      break; 
     default: 
      throw new InvalidKeyException("Invalid Key Size, expected one of [56, 112, 168], found[" + keySize + "]"); 
    } 
    return Hex.decodeHex(keyString.toCharArray()); 
} 

回答

1

截至decryptData()結束時,你有return cipherText.toString();從字節數組本身的Object類調​​用toString()。這不是一個非常有趣的toString()實現 - 它只是基於字節數組對象的內存中的位置返回一些垃圾。您需要將您的字節數組實際轉換回String。你可以嘗試

return new String(cipherText); 

decryptData(),而不是最後一行。

+0

我試過併成功:),非常感謝! –