왜 내 암호 해독 방법이 이상한 문자를 제공하는지 이해하는 데 도움이 될지 궁금합니다. 특히,이 경우에, 나는자바의 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은 보안 관련 작업을 수행하려고 시도하는 라이브러리의 기본 사용법을 적절하게 조사하지 않기 때문에 -1입니다. 이것은 Q & A 포럼이며 귀하의 질문은 유효하지만 질문을하기 전에 어느 수준의 연구 및 문제 해결이 예상됩니다. 질문이 적절하게 조사되지 않았고 간단한 오류가 있었기 때문에 검색 쿼리에 응답하여 질문을 찾은 사람들에게이 솔루션이 유용 할 것 같지 않습니다. 이 질문은 제 의견으로는 "간단한 타이포그래피 오류"기준에 거의 위배됩니다. –