Exporter

File

Decode png with Java

password is null when Encrypt was off; otherwise the png holds the openssl aes256 -pbkdf2 format: Salted__ + salt, PBKDF2-SHA256, 10000 iterations · JDK only
static byte[] decodePng(Path pngFile, String password) throws Exception {
    BufferedImage img = ImageIO.read(pngFile.toFile());
    ByteArrayOutputStream pixels = new ByteArrayOutputStream();
    for (int y = 0; y < img.getHeight(); y++) {
        for (int x = 0; x < img.getWidth(); x++) {
            int rgb = img.getRGB(x, y);
            pixels.write(rgb >> 16);
            pixels.write(rgb >> 8);
            pixels.write(rgb);
        }
    }
    byte[] bytes = pixels.toByteArray();
    byte[] payload = Arrays.copyOfRange(bytes, 4, 4 + ByteBuffer.wrap(bytes).getInt());
    if (password == null) return payload;
    // Java 8 before 8u161 caps AES at 128 bits unless this is set (8u151+) or the JCE policy files are installed
    Security.setProperty("crypto.policy", "unlimited");
    byte[] salt = Arrays.copyOfRange(payload, 8, 16);
    SecretKeyFactory kdf = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
    byte[] keyIv = kdf.generateSecret(new PBEKeySpec(password.toCharArray(), salt, 10000, 384)).getEncoded();
    Cipher aes = Cipher.getInstance("AES/CBC/PKCS5Padding");
    aes.init(Cipher.DECRYPT_MODE, new SecretKeySpec(keyIv, 0, 32, "AES"), new IvParameterSpec(keyIv, 32, 16));
    return aes.doFinal(payload, 16, payload.length - 16);
}

Files.write(Paths.get("/path/to/sample.txt"), decodePng(Paths.get("/path/to/sample-plain.png"), null));
Files.write(Paths.get("/path/to/sample.txt"), decodePng(Paths.get("/path/to/sample-enc.png"), "…"));

Decode raw tx

Raw transaction