Skip to content

Instantly share code, notes, and snippets.

@akintos
Last active July 3, 2023 15:40
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akintos/04e2494c62184d2d4384078b0511673b to your computer and use it in GitHub Desktop.
Save akintos/04e2494c62184d2d4384078b0511673b to your computer and use it in GitHub Desktop.
'Yu-Gi-Oh! Master Duel' TextAsset decryption
using System;
using System.IO;
using System.IO.Compression;
static void Main(string[] args)
{
byte[] data = File.ReadAllBytes("CARD_Name");
Xor(data);
using (MemoryStream ms = new MemoryStream(data))
using (DeflateStream ds = new DeflateStream(ms, CompressionMode.Decompress))
using (FileStream fs = File.Create("CARD_Name.dec"))
{
ms.Position = 2;
ds.CopyTo(fs);
}
}
static void Xor(byte[] data)
{
const byte m_iCryptoKey = 0xB3;
for (int i = 0; i < data.Length; i++)
{
byte v = (byte)((i + m_iCryptoKey + 0x23D) * m_iCryptoKey);
v ^= (byte)(i % 7);
data[i] ^= v;
}
}
import zlib
m_iCryptoKey = 0xB3
with open("CARD_Name", "rb") as f:
data = bytearray(f.read())
for i in range(len(data)):
v = i + m_iCryptoKey + 0x23D
v *= m_iCryptoKey
v ^= i % 7
data[i] ^= v & 0xFF
with open("CARD_Name.dec", "wb") as f:
f.write(zlib.decompress(data))
@RndUser0
Copy link

RndUser0 commented Mar 23, 2022

Thanks a lot for this! Could you please also code an encryption tool?
Edit: Didn't know it was so easy to code the encryption script:
Edit2: Updated the CryptoKey because it changed with the last Master Duel update and added feature to get input file from parameter.

import sys
import zlib

file_name = sys.argv[1]
m_iCryptoKey = 0xDA

with open(file_name, "rb") as f:
	data = bytearray(f.read())

for i in range(len(data)):
	v = i + m_iCryptoKey + 0x23D
	v *= m_iCryptoKey
	v ^= i % 7
	data[i] ^= v & 0xFF

with open(file_name + ".dec", "wb") as f:
	f.write(zlib.decompress(data))

@TulioACG
Copy link

TulioACG commented Aug 8, 2022

Last masterduel update has changed the crypto key to: "0x131"

@RndUser0
Copy link

RndUser0 commented Aug 10, 2022

Last masterduel update has changed the crypto key to: "0x131"

Thank you for the info!

@Ravenite-0
Copy link

Do you happen to know the newest crypto key for the November update? Looks like it changed this time as well.

@RndUser0
Copy link

RndUser0 commented Nov 16, 2022

@Ravenite-0
The new crypto key is 0x1A2. Found it with this:

import os
import zlib

m_iCryptoKey = 0x131 ## old crypto key

def Decrypt(file_name):
    with open(f'{file_name}', "rb") as f:
        data = bytearray(f.read())

    for i in range(len(data)):
        v = i + m_iCryptoKey + 0x23D
        v *= m_iCryptoKey
        v ^= i % 7
        data[i] ^= v & 0xFF

    with open(f'{file_name}' + ".dec", "wb") as f:
        f.write(zlib.decompress(data))

while True:

	try:

		Decrypt('CARD_Indx')
		if os.stat('CARD_Indx.dec').st_size > 0:
			with open('!CryptoKey.txt', 'w') as f:
				f.write(hex(m_iCryptoKey))
			print("Crypto key found and written to ""!CryptoKey.txt"" ")
			
		break

	except zlib.error:

		print("zlib error, CryptoKey =", hex(m_iCryptoKey))
		m_iCryptoKey = m_iCryptoKey + 1		

print("Press <ENTER> to continue")
input()

@RndUser0
Copy link

New crypto key for Master Duel v1.4.1: 0x23F

@walk520
Copy link

walk520 commented Feb 10, 2023

New crypto key for Master Duel v1.4.1: 0x23F
Seems to be incorrect.
image

@RndUser0
Copy link

That's odd, the crypto key 0x23F works for me for the files CARD_Desc, CARD_Indx and CARD_Name. Which file are you trying to decrypt?

@walk520
Copy link

walk520 commented Feb 10, 2023

That's odd, the crypto key 0x23F works for me for the files CARD_Desc, CARD_Indx and CARD_Name. Which file are you trying to decrypt?

I've deleted all the files and tried again, and it's working now. Thank you

@drdan2022010
Copy link

hello everyone, Can anyone tell me what is CARD_Genre file, i desc but when i use hex to check i can't read the file. Thanks for your reading
p/s also can anyone tell me how can find the rarity card file? Thanks for your reading

@RndUser0
Copy link

RndUser0 commented May 8, 2023

The crypto key has changed again with the latest update: 0x25F

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment