Skip to content

Instantly share code, notes, and snippets.

@derhuerst
Last active October 21, 2019 11:53
Show Gist options
  • Save derhuerst/81a7d6a7298c16c3c68e6d54fd80755f to your computer and use it in GitHub Desktop.
Save derhuerst/81a7d6a7298c16c3c68e6d54fd80755f to your computer and use it in GitHub Desktop.
decrypting the encrypted salt for HAFAS MAC params

run Node.js code:

const decryptMACSalt = require('./decrypt-mac-salt')

const decrypted = decryptMACSalt('rGhXPq+xAlvJd8T8cMnojdD0IoaOY53X7DPAbcXYe5g=')
console.log(decrypted.toString('hex'))

run Java code:

javac MACSaltDecrypter.java
java MACSaltDecrypter rGhXPq+xAlvJd8T8cMnojdD0IoaOY53X7DPAbcXYe5g=
'use strict'
const crypto = require('crypto')
// todo: is this the same for all HAFAS endpoints?
const key = Buffer.from('61483646387a527569426e6d33336655', 'hex')
const iv = Buffer.alloc(16, 0)
const decryptMACSalt = (encryptedSalt) => {
const decipher = crypto.createDecipheriv('aes-128-cbc', key, iv)
return Buffer.concat([
decipher.update(Buffer.from(encryptedSalt, 'base64')),
decipher.final()
])
}
module.exports = decryptMACSalt
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import javax.crypto.spec.IvParameterSpec;
import java.util.Base64;
import java.nio.charset.StandardCharsets;
class MACSaltDecrypter {
public static byte[] decrypt(byte[] hci) {
try {
byte[] key = new byte[]{ 97, 72, 54, 70, 56, 122, 82, 117, 105, 66, 110, 109, 51, 51, 102, 85 };
SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");
Cipher instance = Cipher.getInstance("AES/CBC/PKCS5Padding");
instance.init(Cipher.DECRYPT_MODE, secretKeySpec, new IvParameterSpec(new byte[16]));
return instance.doFinal(Base64.getDecoder().decode(hci));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static void main (String[] args) {
byte[] encrypted = args[0].getBytes(StandardCharsets.UTF_8);
byte[] decrypted = MACSaltDecrypter.decrypt(encrypted);
StringBuilder asHex = new StringBuilder(decrypted.length * 2);
for (byte b: decrypted) asHex.append(String.format("%02x", b));
System.out.println(asHex.toString());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment