我不知道你能否幫我理解我的解密方法爲什麼會給出奇怪的字符。具體而言,在這種情況下,我得到類似於java中AES解密的奇怪字符
�����c~�+�J*zC�iV�-��&�_l��*.
這裏的字符是我的代碼:
import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.nio.charset.Charset;
import javax.crypto.*;
import java.security.*;
import java.util.Arrays;
import javax.crypto.spec.*;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Hex;
public class AESCrypto2 {
private Cipher AEScipher;
private KeyGenerator AESgen;
private SecretKeySpec AESkey;
private SecretKeySpec decodeKey;
private String hexDecodeKey;
private String decodeKey64;
private byte[] cipherData;
private String msg;
private String encMsg;
public static void main(String[] args) {
try {
AESCrypto2 a = new AESCrypto2();
a.encrypt("Hello!");
try {
a.decrypt(a.getEncryptedMsg(), a.getDecodeKey());
} catch (DecoderException ex) {
ex.printStackTrace();
}
} catch (NoSuchAlgorithmException ex) {
ex.printStackTrace();
} catch (NoSuchPaddingException ex) {
ex.printStackTrace();
} catch (InvalidKeyException ex) {
ex.printStackTrace();
} catch (UnsupportedEncodingException ex) {
ex.printStackTrace();
} catch (IllegalBlockSizeException ex) {
ex.printStackTrace();
} catch (BadPaddingException ex) {
ex.printStackTrace();
}
}
public AESCrypto2() throws NoSuchAlgorithmException, NoSuchPaddingException,
UnsupportedEncodingException {
AESgen = KeyGenerator.getInstance("AES");
AESgen.init(128);
AESkey = (SecretKeySpec) AESgen.generateKey();
decodeKey = new SecretKeySpec(AESkey.getEncoded(), "AES");
hexDecodeKey = keyToString(decodeKey);
AEScipher = Cipher.getInstance("AES/ECB/NoPadding");
}
public AESCrypto2(String msg) throws NoSuchAlgorithmException,
NoSuchPaddingException, InvalidKeyException,
UnsupportedEncodingException, IllegalBlockSizeException,
BadPaddingException {
this();
encrypt(msg);
}
public String encrypt(String msg) throws NoSuchAlgorithmException,
InvalidKeyException, UnsupportedEncodingException,
IllegalBlockSizeException, BadPaddingException, NoSuchPaddingException {
AEScipher.init(Cipher.ENCRYPT_MODE, AESkey);
cipherData = AEScipher.doFinal(handleString(msg.getBytes("UTF-8")));
this.msg = msg;
encMsg = stringToHex(new String(cipherData));
return encMsg;
}
public String decrypt(String msg, String hexDecodeKey) throws
InvalidKeyException, IllegalBlockSizeException,
BadPaddingException, UnsupportedEncodingException,
NoSuchAlgorithmException, NoSuchPaddingException, DecoderException {
AEScipher.init(Cipher.DECRYPT_MODE, stringToKey(hexDecodeKey));
byte[] decryptedData = AEScipher.doFinal(handleString(hexToString(msg).getBytes("UTF-8")));
encMsg = msg;
msg = new String(decryptedData);
System.out.println(msg);
return msg;
}
public String getEncryptedMsg() {
return encMsg;
}
public String getDecryptedMsg() {
return msg;
}
public String getDecodeKey() {
return hexDecodeKey;
}
public SecretKeySpec getKey() {
return decodeKey;
}
//AEScipher requires that 16 divides the length of b
public static byte[] handleString(byte[] b) throws UnsupportedEncodingException {
byte[] temp = b;
if (temp.length % 16 != 0) {
byte[] byteMsg = Arrays.copyOf(temp, temp.length + 16 - (temp.length % 16));
return byteMsg;
}
return temp;
}
public static String keyToString(SecretKeySpec key) {
String decoded = Hex.encodeHexString(key.getEncoded());
return decoded;
}
public static SecretKeySpec stringToKey(String key) throws DecoderException {
byte[] decodedKey = Hex.decodeHex(key.toCharArray());
return new SecretKeySpec(decodedKey, 0, decodedKey.length, "AES");
}
public static String stringToHex(String msg) throws UnsupportedEncodingException {
return Hex.encodeHexString(msg.getBytes("UTF-8"));
}
public static String hexToString(String msg) throws DecoderException {
return new String(Hex.decodeHex(msg.toCharArray()));
}
}
你是從哪裏複製粘貼的?這在很多層面上都是錯誤的... – ntoskrnl
我沒有複製一些作品,但不是全部。 – Kortlek
-1沒有適當地研究您試圖執行安全相關操作的庫的基本用法。雖然這是一個問答論壇,並且您的問題是有效的,但在您提出問題之前,還需要進行一定程度的調查和故障排除。由於您的問題沒有得到充分研究,並且有一個簡單的錯誤,因此解決方案不太可能對那些偶然發現您的問題並響應其搜索查詢的用戶有用。這個問題幾乎屬於我認爲的「簡單印刷錯誤」標準。 –