Skip to content

Instantly share code, notes, and snippets.

@Slipyx
Created September 23, 2018 11:44
Show Gist options
  • Save Slipyx/f28fc27d5d2bc3c7aec0c8152d66dc28 to your computer and use it in GitHub Desktop.
Save Slipyx/f28fc27d5d2bc3c7aec0c8152d66dc28 to your computer and use it in GitHub Desktop.
Python script for redshirt2 file encryption & decryption
#!/usr/bin/python3 -tt
# redshirt2 file encryption & decryption
# reads and writes file contents from/to stdin & stdout
# algorithm adapted from dx9's php implementation.
# http://forums.introversion.co.uk/viewtopic.php?f=29&t=13803&start=60#p485984
#
# usage: ./redshirt2.py < infile > outfile
import sys
import io
# enable binary IO
stdin = sys.stdin.buffer
stdout = sys.stdout.buffer
# the key used for encrypting and decrypting
# can be a byte array of any size
KEY = bytes.fromhex( '1f0709010b0205050311280c23161b02' )
# the size and content of an encrypted file's 'header'
HED = b'redshirt2'
# if we are encrypting or not
encrypting = False
# determine if file is encrypted by checking header content
for hb in HED:
inb = ord( stdin.read( 1 ) )
if inb != hb:
encrypting = True
# ensure input is seekable
try: stdin.seek( 0 )
except io.UnsupportedOperation as e:
sys.stderr.write( str(e)+'\n' ); sys.exit()
stdout.write( HED )
break
ikey = 0
if encrypting: sys.stderr.write( 'Encrypting...\n' )
else: sys.stderr.write( 'Decrypting...\n' )
for ch in stdin.read():
c = ch
# keep whitespace
if c >= 33:
ikey = (ikey + 1) % len( KEY )
shift = KEY[ikey]
test0 = (shift - 33) if (shift > 33) else 0
test1 = 95 if (shift > 33) else (128 - shift)
if encrypting:
if test1 <= c < 128: c -= 95
c = (c + shift) % 256
if c < 33: c = (c - 128 + test0 + 256) % 256
else:
c = (c - shift + 256) % 256
if test1 <= c < 128:
c = (c + 128 - test0 + 256) % 256
if c < 33: c += 95
stdout.write( c.to_bytes( 1, sys.byteorder ) )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment