基本上该文件包含16个字节的文本,但是当它超过16个字节时,它会抛出错误: hexBinary需要是偶数长度。
错误:
byte[] result = cipher.doFinal(DatatypeConverter.parseHexBinary(content));
我是否需要使用其他密码方法或填充?
public static void main(String[] args) throws Exception {
convertToHex(System.out, new File("E:\\TESTS\\kristersss.txt"));
//write the output into a file
convertToHex(new PrintStream("E:\\TESTS\\kristersssHex.txt"), new File("E:\\TESTS\\kristersss.txt"));
System.out.println( " " );
System.out.println( "128-bit hex key example: ffffffffffffffffffffffffffffffff" );
String content = new Scanner(new File("E:\\TESTS\\kristersssHex.txt")).useDelimiter("\\Z").next();
System.out.println("----------FAILA SATURS----------");
System.out.println(content);
System.out.println("------------------------------");
Scanner scanner = new Scanner( System.in );
System.out.println( "Enter 128-bit hex key: " );
final String keyHex = scanner.nextLine();
final String plaintextHex = "aaaaaaaaaabbbbbbbbbbbccccccccccff";
SecretKey key = new SecretKeySpec(DatatypeConverter
.parseHexBinary(keyHex), "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] result = cipher.doFinal(DatatypeConverter
.parseHexBinary(content));
String a = DatatypeConverter.printHexBinary(result);
System.out.println("----------FAILA SATURA ENKRIPTACIJA----------");
System.out.println(DatatypeConverter.printHexBinary(result));
BufferedWriter output = null;
try {
File file = new File("E:\\TESTS\\kristerssEncrypts.txt");
output = new BufferedWriter(new FileWriter(file));
output.write(a);
} catch ( IOException e ) {
e.printStackTrace();
} finally {
if ( output != null ) {
output.close();
}
}
Cipher cipherd = Cipher.getInstance("AES/ECB/NoPadding");
cipherd.init(Cipher.DECRYPT_MODE, key);
byte[] result2 = cipherd.doFinal(result);
System.out.println("----------FAILA SATURA DEKRIPTACIJA----------");
System.out.println(DatatypeConverter.printHexBinary(result2));
BufferedWriter outputt = null;
String aa = DatatypeConverter.printHexBinary(result2);
try {
File file = new File("E:\\TESTS\\kristerssDekrypts.txt");
outputt = new BufferedWriter(new FileWriter(file));
outputt.write(aa);
} catch ( IOException e ) {
e.printStackTrace();
} finally {
if ( outputt != null ) {
outputt.close();
}
}
将文件字符串转换为十六进制的类。
public static void convertToHex(PrintStream out, File file) throws IOException {
InputStream is = new FileInputStream(file);
int bytesCounter =0;
int value = 0;
StringBuilder sbHex = new StringBuilder();
StringBuilder sbText = new StringBuilder();
StringBuilder sbResult = new StringBuilder();
while ((value = is.read()) != -1) {
//convert to hex value with "X" formatter
sbHex.append(String.format("%02X", value));
//If the chracater is not convertable, just print a dot symbol "."
if (!Character.isISOControl(value)) {
sbText.append((char)value);
}else {
sbText.append(".");
}
//if 16 bytes are read, reset the counter,
//clear the StringBuilder for formatting purpose only.
if(bytesCounter==15){
sbResult.append(sbHex).append("");
sbHex.setLength(0);
sbText.setLength(0);
bytesCounter=0;
}else{
bytesCounter++;
}
}
//if still got content
if(bytesCounter!=0){
//add spaces more formatting purpose only
for(; bytesCounter<16; bytesCounter++){
//1 character 3 spaces
sbHex.append(" ");
}
sbResult.append(sbHex).append("");
}
out.print(sbResult);
is.close();
}
我正在努力实现的目标: