Java包含hexBinary的非法字符

时间:2017-11-19 03:48:34

标签: java aes

我面临的问题是,当我从文件(字符串)中读取时,例如:

  • ffffffffffffffffffffffffffffffff

它显示错误“包含hexBinary的非法字符:fffffffffffffffffffffffffffffffffffff”但是当我将该语句作为基本 String = fffffffffffffffffffffffffffffffff; 时一切正常。

public static void main(String[] args) throws Exception {
System.out.println( "128-bit hex key example: ffffffffffffffffffffffffffffffff" );

try(BufferedReader br = new BufferedReader(new FileReader("E:\\TESTS\\tests.txt"))) {
    StringBuilder sb = new StringBuilder();
    String line = br.readLine();

    while (line != null) {
        sb.append(line);
        sb.append(System.lineSeparator());
        line = br.readLine();
    }
    String everything = sb.toString();


Scanner scanner = new Scanner( System.in );
 System.out.println( "Enter 128-bit hex key: " );
 final String keyHex = scanner.nextLine();

 final String plaintextHex = "aaaaaaaaaabbbbbbbbbbccccccccccff";

SecretKey key = new SecretKeySpec(DatatypeConverter
    .parseHexBinary(keyHex), "AES");
System.out.println(everything);
Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, key);

byte[] result = cipher.doFinal(DatatypeConverter
    .parseHexBinary(everything));

System.out.println(DatatypeConverter.printHexBinary(result));

Cipher cipherd = Cipher.getInstance("AES/ECB/NoPadding");
cipherd.init(Cipher.DECRYPT_MODE, key);

byte[] result2 = cipherd.doFinal(result);
System.out.println(DatatypeConverter.printHexBinary(result2));

}}

1 个答案:

答案 0 :(得分:0)

String content = new Scanner(new File("E:\\TESTS\\tests.txt")).useDelimiter("\\Z").next();
    System.out.println(content);

这就是魔术,移除了空间。