繁体   English   中英

Java包含hexBinary的非法字符

[英]Java Contains illegal character for hexBinary

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

  • ffffffffffffffffffffffffffffffffff

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

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));

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

这样做神奇了,删除了空格。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM