내 프로그램에 이상한 점이 있습니다. 일부 문자열을 암호화 한 다음 암호를 해독하려고합니다. 나는 암호 해독의 기능을 테스트하기 위해 시험 클래스를 만들었다. 상황이 암호화를 위해 잘 작동하지만, 해독하려고 할 때만, 때로는 문자열 중간에 이상한 문자가 있습니다. 여기 내 코드입니다 :Java Cipher 해독에 이상한 문자가 있습니다.
public class Prova {
public static void main(String[] args){
String s = "with the lights out is less dangerous here we are now entertain us";
s = cripta(s);
System.out.println(s);
s = decripta(s);
System.out.println(s);
}
public static String cripta(String s){
System.out.println("lunghezza stringa:"+s.length());
byte[] input = s.getBytes();
byte[] output;
byte[] keyBytes = hexStringToByteArray("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
byte[] ivBytes = hexStringToByteArray("AAAAAAAAAAAAAAAA");
String out = "";
SecretKeySpec key = new SecretKeySpec(keyBytes, "DESede");
IvParameterSpec ivSpec = new IvParameterSpec(ivBytes);
try {
Cipher cp = Cipher.getInstance("DESede/ECB/PKCS5Padding");
cp.init(Cipher.ENCRYPT_MODE, key);
byte[] criptati = new byte[cp.getOutputSize(input.length)];
int enc_len = cp.update(input, 0, input.length, criptati, 0);
enc_len += cp.doFinal(criptati, enc_len);
out = new String(criptati);
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ShortBufferException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (BadPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return out;
}
public static String decripta(String s){
byte[] input = s.getBytes();
byte[] output;
byte[] keyBytes = hexStringToByteArray("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
byte[] ivBytes = hexStringToByteArray("AAAAAAAAAAAAAAAA");
String out = "";
try {
SecretKeySpec key = new SecretKeySpec(keyBytes, "DESede");
IvParameterSpec ivSpec = new IvParameterSpec(ivBytes);
Cipher cp = Cipher.getInstance("DESede/ECB/NoPadding");
cp.init(Cipher.DECRYPT_MODE, key);
byte[] decrypt = new byte[cp.getOutputSize(input.length)];
int dec_len = cp.update(input, 0, input.length, decrypt, 0);
System.out.println(dec_len);
dec_len += cp.doFinal(decrypt, dec_len);
System.out.println(dec_len);
out = new String(decrypt);
} catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ShortBufferException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (BadPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return out;
}
private static byte[] hexStringToByteArray(String s) {
// TODO Auto-generated method stub
int len = s.length();
byte[] data = new byte[len/2];
for (int i = 0; i < len; i += 2) {
data[i/2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
+ Character.digit(s.charAt(i+1), 16));
}
return data;
}
}
내가받을 출력 "이 소등이 덜 dangÝâ ¥ 8, -xÖÆZŽÏö = AEE 지금 우리를 즐겁게"입니다
는'String' 바이너리 데이터의 컨테이너가 아닙니다. – EJP