Created
May 21, 2017 16:40
-
-
Save DrakiaXYZ/e36f97d924b80329eff6b54f52734684 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <openssl/aes.h> | |
void chartohex(unsigned char* input, unsigned char* output) | |
{ | |
char tmp[3]; | |
memset(tmp, 0, 3); | |
for (int i = 0; i < strlen(input); i++) | |
{ | |
memcpy(tmp, &input[i * 2], 2); | |
output[i] = (char)(strtol(tmp, NULL, 16)); | |
} | |
} | |
int encrypt_titlekey(unsigned char* titleId, unsigned char *titlekey, unsigned char *commonKey) | |
{ | |
AES_KEY aeskey_dec; | |
unsigned char iv[16]; | |
int ret = 0; | |
memset(iv, 0, 16); | |
memcpy(iv, titleId, 8); | |
ret = AES_set_encrypt_key(commonKey, 128, &aeskey_dec); | |
if(ret<0) | |
{ | |
printf("Failed to set key: %d\n", ret); | |
return ret; | |
} | |
AES_cbc_encrypt(titlekey, titlekey, 16, &aeskey_dec, iv, AES_ENCRYPT); | |
return 0; | |
} | |
int tikdecrypt_titlekey(unsigned char *titleId, unsigned char *titlekey, unsigned char* commonKey) | |
{ | |
// Convert the passed in values to binary arrays | |
unsigned char titleIdData[0x08]; | |
unsigned char titleKeydata[0x10]; | |
unsigned char commonKeyData[0x10]; | |
chartohex(titleId, titleIdData); | |
chartohex(titlekey, titleKeydata); | |
chartohex(commonKey, commonKeyData); | |
unsigned char encryptedTitlekey[0x10]; | |
memcpy(encryptedTitlekey, titleKeydata, 0x10); | |
int ret = encrypt_titlekey(titleIdData, encryptedTitlekey, commonKeyData); | |
if (ret) | |
{ | |
return ret; | |
} | |
printf("\nDecrypted titlekey: "); | |
for(int i=0; i<16; i++)printf("%02x", titleKeydata[i]); | |
printf("\n"); | |
printf("Encrypted titlekey: "); | |
for(int i=0; i<16; i++)printf("%02x", encryptedTitlekey[i]); | |
printf("\n"); | |
return 0; | |
} | |
int main(int argc, char *argv[]) | |
{ | |
printf("Title key encrypter by Drakia\n"); | |
if(argc != 4) | |
{ | |
printf("Decrypt an encrypted titlekey\n"); | |
printf("%s <titleId> <decTitleKey> <0x3D Normal Key>\n\n", argv[0]); | |
return 0; | |
} | |
if (strlen(argv[1]) != 16) | |
{ | |
printf("Invalid title ID length\n\n"); | |
return 0; | |
} | |
if (strlen(argv[2]) != 32) | |
{ | |
printf("Invalid title key length\n\n"); | |
return 0; | |
} | |
if (strlen(argv[3]) != 32) | |
{ | |
printf("Invalid normal key length\n\n"); | |
return 0; | |
} | |
tikdecrypt_titlekey(argv[1], argv[2], argv[3]); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment