I need to find a key to decrypt something that was encrypted with Xor. I know that the key is only one byte. I was hoping to be able to brute force guess the correct key. Is there a list of possible keys someone could direct me to. Also, would the key be in hex or decimal. Thanks
2
votes
Maybe your question belongs to cryptography stack exchange site?
– Yulia V
If the key is a byte, the possible keys are the possible values of a byte: 0, 1, 2, ..., 254, 255. What's the question?
– user395760
This question appears to be off-topic because it is about cryptography, without involving programming itself.
– Maarten Bodewes
2 Answers
1
votes
0
votes
I provided an answer previously for some 8-channel xor encryption scheme producing a result from an encoded msg and no keys. The function that finds the key along with setup and support code is as follows:
//setup a table of readable characters (characters we will expect to see in the decrypted result).
var readableCharacters=Object.create(null);
var alphanumerics=[[0x30,0x39],[0x41,0x5a],[0x61,0x7a]];
var alphabetical=[[0x41,0x5a],[0x61,0x7a]];
alphanumerics.forEach(function(range){
var indexStart=range[0];
var indexEnd=range[1];
for (var i=indexStart; i<=indexEnd; i++){
var ch=String.fromCharCode(i);
readableCharacters[ch]=1;
}
});
//add some extra characters to the table (optional)
(" ,.;:!\"'").split("").forEach(function(ch){readableCharacters[ch]=1;});
//xor decryption
function dec(nkey,ncrypt){
var ndec=nkey^ncrypt;
return ndec;
}
//find a key candidate by determining which keys correspond to the highest occurance of characters found in the readable character lookup table.
function findKeyCandidate(byteArray){
var keyResults=[];
for (var key=0; key<256; key++){
keyResults[key]=0;
byteArray.forEach(function(ncrypt){
var ndec=dec(key,ncrypt);
var dchar=String.fromCharCode(ndec);
if(readableCharacters[dchar]){
keyResults[key]++;
}
});
}
keyResults=keyResults.map(function(count,index){
return {key:index,count:count};
});
keyResults.sort(function(a,b){
return a.count-b.count;
});
return keyResults.pop().key;
}
Note: For the input you'll need an actual byte array (i.e. an array of numbers 0-255) not a hexadecimal string (the conversion from hex to byte array and back is present in the previous solution linked above). Output is a byte-size key.
This all assumes you'll end up with a message that is readable by humans, if you expect some other message structure/composition, then you'll need to modify the code according to what you expect to find.