15
Write a program or function (or set of programs/functions) to encrypt and decrypt data given the following specification:
Encryption
Calculate an XOR hash of the input by XOR-ing every byte with each-other.
XOR every byte of the input by this hash.
Shift the result four bits left.
Pad the left side with the first four bits of the XOR hash.
Pad the right side with the last four bits of the XOR hash.
Example
Given input:
"G0lf"
(0x47306C66
)Calculate XOR hash:
0x47 ^ 0x30 ^ 0x6C ^ 0x66 = 0x7D
XOR every byte by hash:
0x3A4D111B
Expected result (after shift and pad):
"s¤Ñ\x11½"
(0x73A4D111BD
)
Rules
Your program/function can input/output of whatever type makes sense in your golfing language of choice (String, Byte Array, etc) as long as the input/output are the actual bytes. For example, you may not output a hexidecimal string.
Encryption and decryption can be separated into separate programs (score will be the their combined size) or a single one. Single methods can take an argument for whether it should encrypt or decrypt.
Input for encryption can be expected to be at least 1 byte in size.
Input for decryption can be expected to be at least 2 bytes.
Non-printable bytes do not need to be escaped in the output.
1Could an decimal array be used as an output form? – ɐɔıʇǝɥʇuʎs – 2015-04-25T17:56:58.573
@ɐɔıʇɥʇuʎs It would be acceptable to take input and output as an array of integers to represent bytes. – nderscore – 2015-04-25T19:01:10.270
Is there a maximum input length (for example, 14 bytes (56 bits), so that the final result fits in a 64-bit integer)? – Doorknob – 2015-04-25T20:00:04.520
1Just a note: From a cryptography point of view this is not an encryption, as it has no key (or a 0-bit key). – Paŭlo Ebermann – 2015-04-25T21:21:20.203
@Doorknob No restrictions on input size. It can be expected to fit in memory though. – nderscore – 2015-04-25T21:33:41.377
@PaŭloEbermann What would the correct terminology be for this? I don't want to say it's an encoding because the result is definitely obfuscated. Would it qualify as a cipher? – nderscore – 2015-04-25T22:00:00.657
1I'm just waiting for someone to post something about never rolling your own encryption, ignoring the site this is on... – user253751 – 2015-04-25T22:02:13.417
Funny random comment, I pronounce XOR "zorr", so I expect to see "a XOR hash" not "an XOR hash" – Claudiu – 2015-04-26T03:08:24.713
What is the character encoding you use for the encrypted result? – FUZxxl – 2015-04-26T11:02:34.613
Is it allowed to receive input as an 8 × n array where each row of the array contains one byte represented as a vector of eight bits? – FUZxxl – 2015-04-26T11:06:23.093
@FUZxxl no encoding is necessary. A two dimensional array of bits is okay. – nderscore – 2015-04-26T15:31:01.430