Skip to content

Instantly share code, notes, and snippets.

@sweis
Last active May 12, 2023 16:55
Show Gist options
  • Save sweis/2d5d0a54c46f86396bda43c11733ebe4 to your computer and use it in GitHub Desktop.
Save sweis/2d5d0a54c46f86396bda43c11733ebe4 to your computer and use it in GitHub Desktop.
A decompiled snippet from the Twitter 9.88 APK release -- Not original source
for (ExtractPublicKeysData publicKeysData : extractPublicKeysDataList) {
Object userId = linkedHashMap.get(publicKeysData.registrationToken);
KeyPairGeneratorSpi.EC ec = new KeyPairGeneratorSpi.EC();
ec.initialize(SessionKeyEncrypter.secp256r1Spec());
KeyPair ephemeralKeyPair = ec.generateKeyPair();
PrivateKey ephemeralPrivateKey = ephemeralKeyPair.getPrivate();
PublicKey recipientPublicKey = publicKeysData.identityKey.publicKey;
byte[] sharedSecret =
SessionKeyEncrypter.generateSecret(recipientPublicKey, ephemeralPrivateKey);
byte[] ephemeralPublicKeyBytes = ephemeralKeyPair.getPublic().getEncoded();
// This looks like an artifact of decompilation, because it is converting from a byte array
// to a list, back to a byte array.
byte[] ivMaybe = rk4.listToByteArray(us0.getNBytesAsList(ephemeralPublicKeyBytes, 65));
// This also might be an artifact of decompilation. It looks like it Base64 encodes the key
// then converst the string to bytes, rather than just using the bytes.
String b64ConversationKey = pk0.b64Encode(conversationKey.secretKey.getEncoded());
byte[] conversationKeyBytes = b64ConversationKey.getBytes(Charsets.utf8);
byte[] ciphertext =
SessionKeyEncrypter.deriveKeyAndEncrypt(1, sharedSecret, ivMaybe, conversationKeyBytes);
byte[] ivAndCiphertext = Arrays.copyOf(ivMaybe, ivMaybe.length + ciphertext.length);
System.arraycopy(ciphertext, 0, ivAndCiphertext, ivMaybe.length, ciphertext.length);
String encryptedConversationKey = pk0.b64Encode(ivAndCiphertext);
arrayList2.add(new Identity((UserIdentifier) userId, publicKeysData.registrationToken, encryptedConversationKey));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment