2023-01-29 14:49:48 +01:00
|
|
|
from typing import Dict, List
|
2021-06-07 07:46:39 +02:00
|
|
|
from typing_extensions import Final
|
2019-12-08 22:43:49 +01:00
|
|
|
|
2023-01-29 14:49:48 +01:00
|
|
|
from Crypto.Cipher import DES3
|
2019-12-08 22:43:49 +01:00
|
|
|
|
2023-07-30 02:38:54 +02:00
|
|
|
|
2019-12-08 22:43:49 +01:00
|
|
|
class CardCipherException(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class CardCipher:
|
|
|
|
"""
|
|
|
|
Algorithm for converting between the Card ID as stored in an
|
|
|
|
eAmusement card and the 16 character card string as shown on
|
|
|
|
the back of a card and in-game. All of this was kindly RE'd by
|
|
|
|
Tau and converted ham-fistedly to Python.
|
|
|
|
"""
|
|
|
|
|
2023-01-29 14:49:48 +01:00
|
|
|
# https://bsnk.me/eamuse/cardid.html
|
|
|
|
DES_KEY: Final[bytes] = bytes(c * 2 for c in b"?I'llB2c.YouXXXeMeHaYpy!")
|
|
|
|
INTERNAL_CIPHER = DES3.new(DES_KEY, DES3.MODE_ECB)
|
2019-12-08 22:43:49 +01:00
|
|
|
|
2021-05-31 20:09:40 +02:00
|
|
|
VALID_CHARS: Final[str] = "0123456789ABCDEFGHJKLMNPRSTUWXYZ"
|
|
|
|
CONV_CHARS: Final[Dict[str, str]] = {
|
2019-12-08 22:43:49 +01:00
|
|
|
"I": "1",
|
|
|
|
"O": "0",
|
|
|
|
}
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def __type_from_cardid(cardid: str) -> int:
|
2022-10-15 20:56:30 +02:00
|
|
|
if cardid[:2].upper() == "E0":
|
2019-12-08 22:43:49 +01:00
|
|
|
return 1
|
2022-10-15 20:56:30 +02:00
|
|
|
if cardid[:2].upper() == "01":
|
2019-12-08 22:43:49 +01:00
|
|
|
return 2
|
2022-10-15 20:56:30 +02:00
|
|
|
raise CardCipherException("Unrecognized card type")
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def encode(cardid: str) -> str:
|
|
|
|
"""
|
|
|
|
Given a card ID as stored on a card (Usually starting with E004), convert
|
|
|
|
it to the card string as shown on the back of the card.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
cardid - 16 digit card ID (hex values stored as string).
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
String representation of the card string.
|
|
|
|
"""
|
|
|
|
if len(cardid) != 16:
|
|
|
|
raise CardCipherException(
|
2022-10-15 20:56:30 +02:00
|
|
|
f"Expected 16-character card ID, got {len(cardid)}",
|
2019-12-08 22:43:49 +01:00
|
|
|
)
|
|
|
|
|
2022-11-26 17:35:32 +01:00
|
|
|
cardbytes = bytes.fromhex(cardid)
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
# Reverse bytes
|
2022-11-26 17:35:32 +01:00
|
|
|
reverse = cardbytes[::-1]
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
# Encipher
|
2023-01-29 14:49:48 +01:00
|
|
|
ciphered = CardCipher.INTERNAL_CIPHER.encrypt(reverse)
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
# Convert 8 x 8 bit bytes into 13 x 5 bit groups (sort of)
|
|
|
|
bits = [0] * 65
|
|
|
|
for i in range(0, 64):
|
2022-11-26 17:35:32 +01:00
|
|
|
bits[i] = (ciphered[i // 8] >> (7 - (i % 8))) & 1
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
groups = [0] * 16
|
|
|
|
for i in range(0, 13):
|
|
|
|
groups[i] = (
|
2022-10-15 20:56:30 +02:00
|
|
|
(bits[i * 5 + 0] << 4)
|
|
|
|
| (bits[i * 5 + 1] << 3)
|
|
|
|
| (bits[i * 5 + 2] << 2)
|
|
|
|
| (bits[i * 5 + 3] << 1)
|
|
|
|
| (bits[i * 5 + 4] << 0)
|
2019-12-08 22:43:49 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
# Smear 13 groups out into 14 groups
|
|
|
|
groups[13] = 1
|
|
|
|
groups[0] ^= CardCipher.__type_from_cardid(cardid)
|
|
|
|
|
|
|
|
for i in range(0, 14):
|
|
|
|
groups[i] ^= groups[i - 1]
|
|
|
|
|
|
|
|
# Scheme field is 1 for old-style, 2 for felica cards
|
|
|
|
groups[14] = CardCipher.__type_from_cardid(cardid)
|
|
|
|
groups[15] = CardCipher.__checksum(groups)
|
|
|
|
|
|
|
|
# Convert to chars and return
|
2022-10-15 20:56:30 +02:00
|
|
|
return "".join([CardCipher.VALID_CHARS[i] for i in groups])
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def decode(cardid: str) -> str:
|
|
|
|
"""
|
|
|
|
Given a card string as shown on the back of the card, return the card ID
|
|
|
|
as stored on the card itself. Does some sanitization to remove dashes,
|
|
|
|
spaces and convert confusing characters (1, L and 0, O) before converting.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
cardid - String representation of the card string.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
16 digit card ID (hex values stored as string).
|
|
|
|
"""
|
|
|
|
# First sanitize the input
|
2022-10-15 20:56:30 +02:00
|
|
|
cardid = cardid.replace(" ", "")
|
|
|
|
cardid = cardid.replace("-", "")
|
2019-12-08 22:43:49 +01:00
|
|
|
cardid = cardid.upper()
|
|
|
|
for c in CardCipher.CONV_CHARS:
|
|
|
|
cardid = cardid.replace(c, CardCipher.CONV_CHARS[c])
|
|
|
|
|
|
|
|
if len(cardid) != 16:
|
|
|
|
raise CardCipherException(
|
2022-10-15 20:56:30 +02:00
|
|
|
f"Expected 16-character card ID, got {len(cardid)}",
|
2019-12-08 22:43:49 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
for c in cardid:
|
|
|
|
if c not in CardCipher.VALID_CHARS:
|
|
|
|
raise CardCipherException(
|
2022-10-15 20:56:30 +02:00
|
|
|
f"Got unexpected character {c} in card ID",
|
2019-12-08 22:43:49 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
# Convert chars to groups
|
|
|
|
groups = [0] * 16
|
|
|
|
|
|
|
|
for i in range(0, 16):
|
|
|
|
for j in range(0, 32):
|
|
|
|
if cardid[i] == CardCipher.VALID_CHARS[j]:
|
|
|
|
groups[i] = j
|
|
|
|
break
|
|
|
|
|
|
|
|
# Verify scheme and checksum
|
2022-10-15 20:56:30 +02:00
|
|
|
if groups[14] != 1 and groups[14] != 2:
|
|
|
|
raise CardCipherException("Unrecognized card type")
|
2019-12-08 22:43:49 +01:00
|
|
|
if groups[15] != CardCipher.__checksum(groups):
|
2022-10-15 20:56:30 +02:00
|
|
|
raise CardCipherException("Bad card number")
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
# Un-smear 14 fields back into 13
|
|
|
|
for i in range(13, 0, -1):
|
|
|
|
groups[i] ^= groups[i - 1]
|
|
|
|
groups[0] ^= groups[14]
|
|
|
|
|
|
|
|
# Explode groups into bits
|
|
|
|
bits = [0] * 64
|
|
|
|
|
|
|
|
for i in range(0, 64):
|
2022-11-26 17:35:32 +01:00
|
|
|
bits[i] = (groups[i // 5] >> (4 - (i % 5))) & 1
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
# Re-pack bits into eight bytes
|
2022-11-26 17:35:32 +01:00
|
|
|
ciphered = bytearray(8)
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
for i in range(0, 64):
|
2022-11-26 17:35:32 +01:00
|
|
|
ciphered[i // 8] |= bits[i] << (7 - (i % 8))
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
# Decipher and reverse
|
2023-01-29 14:49:48 +01:00
|
|
|
deciphered = CardCipher.INTERNAL_CIPHER.decrypt(ciphered)
|
2022-11-26 17:35:32 +01:00
|
|
|
reverse = deciphered[::-1]
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
# Convert to a string, verify we have the same type
|
2022-11-26 17:35:32 +01:00
|
|
|
finalvalue = reverse.hex().upper()
|
2019-12-08 22:43:49 +01:00
|
|
|
if groups[14] != CardCipher.__type_from_cardid(finalvalue):
|
2022-10-15 20:56:30 +02:00
|
|
|
raise CardCipherException("Card type mismatch")
|
2019-12-08 22:43:49 +01:00
|
|
|
return finalvalue
|
|
|
|
|
2022-11-26 17:35:32 +01:00
|
|
|
# extended/modified luhn mod 32 checksum?
|
2019-12-08 22:43:49 +01:00
|
|
|
@staticmethod
|
|
|
|
def __checksum(data: List[int]) -> int:
|
2022-11-26 17:35:32 +01:00
|
|
|
checksum = sum(n * 1 for n in data[0:15:3])
|
|
|
|
checksum += sum(n * 2 for n in data[1:15:3])
|
|
|
|
checksum += sum(n * 3 for n in data[2:15:3])
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
while checksum >= 0x20:
|
2022-11-26 17:35:32 +01:00
|
|
|
checksum = sum(divmod(checksum, 0x20))
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
return checksum
|