wmbusmeters/utils/XMLExtract.java

42 wiersze
1.4 KiB
Java
Czysty Zwykły widok Historia

2018-12-28 15:07:24 +00:00
//
// A utility to extract the contents of an
// XML containing an XML-Encrypted CipherValue.
//
// Compile with javac XMLExtract.java (requires jdk8 or above).
//
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Base64;
import java.util.stream.Collectors;
public class XMLExtract
{
static Cipher getCipherDecrypt(String password) throws Exception {
2018-12-29 22:50:17 +00:00
byte[] key = Arrays.copyOf(password.getBytes("UTF-8"), 16);
2018-12-28 15:07:24 +00:00
SecretKeySpec sks = new SecretKeySpec(key, "AES");
IvParameterSpec ips = new IvParameterSpec(key);
Cipher c = Cipher.getInstance("AES/CBC/NoPadding");
2018-12-28 15:07:24 +00:00
c.init(Cipher.DECRYPT_MODE, sks, ips);
return c;
}
public static void main(String args[]) throws Exception
{
if (args.length < 2) {
System.out.println("java -cp . XMLExtract [password] [encrypted_xml_file] ");
return;
}
Cipher c = getCipherDecrypt(args[0]);
String xml = Files.lines(Paths.get(args[1])).collect(Collectors.joining(""));
String b64 = xml.substring(xml.indexOf("<CipherValue>")+13, xml.indexOf("</CipherValue>"));
byte[] raw = Base64.getDecoder().decode(b64);
2018-12-28 15:07:24 +00:00
String plain = new String(c.doFinal(raw));
System.out.println(plain);
}
}