Skip to content

Instantly share code, notes, and snippets.

@Slipyx
Last active March 6, 2017 23:56
Show Gist options
  • Save Slipyx/7c631bfef99c52b5a05a to your computer and use it in GitHub Desktop.
Save Slipyx/7c631bfef99c52b5a05a to your computer and use it in GitHub Desktop.
redshirt2 file decryption/encryption
// redshirt2 file decryption/encryption... in squirrel!
// reads and writes file contents using stdin/stdout.
// algorithm adapted from dx9's php implementation.
// http://forums.introversion.co.uk/viewtopic.php?f=29&t=13803&start=60#p485984
//
// usage: sq redshirt2.nut < infile.txt > outfile.txt
// the key to use for both decrypting and encrypting
// can be a byte array of any size
local thekey = [
0x1e, 0x17, 0x21, 0x1b,
0x13, 0x1b, 0x02, 0x16,
0x13, 0x0f, 0x14, 0x15,
0x1d, 0x15, 0x0a, 0x20
];
// plaintext header
// determines the size and content of an encrypted file's "header"
local hed = "REDSHRT2";
local bEncrypt = false;
// if header text appears at beginning of file, it is seen as encrypted.
// otherwise, the header is written first to stdout for encrypting
for ( local i = 0; i < hed.len(); ++i ) {
if ( stdin.readn( 'b' ) != hed[i] ) {
bEncrypt = true;
stdin.seek( 0 );
print( hed );
break;
}
}
//print( hed + " -- " + bEncrypt );
//print( stdin.len() )
local ikey = 0;
// encrypt or decrypt file byte-by-byte and write to stdout
while ( !stdin.eos() ) {
local c = stdin.readn( 'b' );
if ( c >= 33 ) {
ikey = (ikey + 1) % thekey.len();
local shift = thekey[ikey];
local test0 = (shift > 33) ? shift - 33 : 0;
local test1 = (shift > 33) ? 95 : 128 - shift;
if ( bEncrypt ) {
if ( c < 128 && c >= test1 ) c -= 95;
c = (c + shift) % 256;
if ( c < 33 ) c = (c - 128 + test0 + 256) % 256;
} else {
c = (c - shift + 256) % 256;
if ( c < 128 && c >= test1 )
c = (c + 128 - test0 + 256) % 256;
if ( c < 33 ) c += 95;
}
}
print( c.tochar() );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment