Simplest encryption or parsing of the Road Rash 3 password structure by bones
Yes, the title didn’t deceive you: today we will remember about the good old console game Road Rash 3.

I think many of you recall with pleasure how you spent hours in proud solitude or in a noisy company of friends, winding virtual kilometers in this, without exaggeration, cult game. Also, one evening I decided to remember my youth and go through a couple of levels in my favorite races. And now, armed with an emulator and a gamepad, he began climbing the career ladder. Everything was going fine, but at one fine moment something jumped at the game and she refused to take me to a new level, although all the conditions for this were fulfilled. I was not upset for a long time, because knew that the game has a kind of password system to save the accumulated achievements. It was only necessary to “tweak” the existing password a little by typing in order to manually transfer yourself to a new unfortunate level and continue the competition with a clear conscience.
But it was too simple and, frankly, not interesting. I immediately had a thought: “How does it work?” And I got the idea to know the password generation algorithm itself, in order to be able to create it myself later on the basis of specific needs or just mood.
Bit of theory
So, what is Road Rash 3? This is a race on cool sports bikes on the roads of various countries to the magnificent 8-bit music. The higher the place you take in the race, the more money you get, which then can be spent on buying a new iron horse or on its modernization. Of particular interest is the opportunity to fight with rivals directly during a race at great speed, to rob them of new weapons and with their help even more beat unlucky competitors. Description description, but let's move on to the numbers.
In the bottom line, we have the following game parameters:
- amount of money (from $ 0 to $ 655,350 in increments of $ 10);
- motorcycle (one of 16 possible);
- improvements on it (4 improvements in various combinations);
- current level (one of 5 possible);
- trails at this level (5 trails in various combinations).
Now take a look at the password itself:

Only 8 positions for characters. Each character can take values 0-9 and AV. Total we get 32 possible characters in each position.
The most interesting
So let's get started.
I will not bore you with long stories about how I deduced all these dependencies and algorithms, having spent many hours experimenting, I will get right to the point.
Perhaps many of you have thought: “How can I fit so many parameters into such a small password?” Is very simple. It's all about cunning logic and sophisticated algorithms. Next you will see how this happens.
Money, money and money again

The first 4 positions of the password are responsible for the money.
C440 xxxx
But not everything is so simple and obvious. There are tricks here.
1 and 2 positions are responsible for amounts up to $ 2550 inclusive, 3 and 4 positions - for amounts above $ 2550.
For example, if we have an amount of $ 5500, this is (2 * 2560 $ + 380 $).
So, for 2 * 2560 $ = 5120 $ 3 and 4 positions will be responsible, and for the rest in the form of $ 380 - 1 and 2 positions.
Let's start small: 1 and 2 positions and amounts less than $ 2550
Now we dig deep into, continuing to consider our example. How is this $ 380 stored in 1 and 2 positions? It happens as follows.

Table 1. 1 and 2 password positions - small money.
1 position is responsible for amounts from $ 0 to $ 150 inclusive in increments of $ 10.
Similarly with 2 position. Only now, each step does not add $ 10, but increases by 1 factor for $ 160. Those. how many times do we need to take $ 160: 0, 1, 2, etc.
Imagine "on the fingers" as follows. We have $ 10, add another 10 in it, another 10, and so on, until we get to 150. All this time we have only a symbol wound in 1 position. As we got to 150, add another 10 and jump to the second position, in which there will already be 160. Then, adding 10 each, again we will rotate the symbol in the first position until we reach 150 and again jump to 2 positions, where there will already be 160 + 160 = 320.
Looking at the table, you can see that only even characters are taken (0, 2, 4 ...). For what? More about this later.
Thus, we can cover any amount within $ 2550.
Let's look at the remainder from the example above: 380 $ = 2 * 160 $ + 60 $.
$ 60 is the first position (less than $ 160). The “ C ” symbol corresponds to this amount .
2 * 160 $ - the second position. This sum corresponds to the symbol " 4 ".
That is, the amount of $ 380 can be written as " C4 ".
Now consider 3 and 4 positions and amounts over $ 2550.
Here, in general, everything is similar.

Table 2. 3 and 4 positions of the password - big money.
Each even character of 3 positions gives + $ 2560 and so on up to $ 38400 = $ 15 * 2560.
Each even character of 4 positions has a factor for 40960 $ = 16 * 2560 $, i.e. how many times you need to take $ 40,960: 0, 1, 2, etc. (This is a slightly simplified algorithm for the 4th position, and we will return to it later).
Thus, we cover any amount from $ 2560 to $ 655350.
In our example, we have $ 5120 left. This, as you can see, is less than $ 40,960, so the 4th position remains zero (we need to take zero times $ 40,960). Well, the 3 position symbol corresponding to $ 5120 = 2 * 2560 $ is " 4 " (see Table 2).
As a result, we got the combination “ C440"For the first four positions, describing the amount of $ 5500.
Level

With the level, everything is very simple and transparent. The 5th position of the password and only 5 characters are responsible for it:
xxxx 1xxx
1 - the first level;
2 - second level;
3 - third level;
4 - fourth level;
5 - fifth level;
Improvements (upgrades)
Improvements, as you recall, we have only four.

In the algorithm, they are logically divided into 2 groups:
- performance and suspension (performance and suspension);
- protection and tires (protection and tires).
First about the first pair
Let's go back to the 1 and 2 positions of the password. Remember, I mentioned earlier that symbols for money are taken through one, i.e. only even? This is one of the secrets.
If we want to establish performance, we need to increase the symbol in 1 position by one, and if we want to install a suspension, we need to increase by one symbol in 2 positions.
С4 xx xxxx
Thus, we use the “window” formed due to the omission of odd characters, which allows us to have an improvement and not have it at the same amount.
Let's consider in more detail on an example.
Our first two characters - “ C4 ” correspond to the amount of $ 380 and the lack of upgrades.
Want a brand new engine? Not a problem! Increase the symbol in 1 position by one and get " D4 ". However, the amount of money has not changed! It will change only when you increase by one more, i.e. to " E4 " (see Table 1).
Similarly with the suspension: increase the symbol in 2 positions by one and get " D5 ". At the same time, the amount remained the same: $ 380.
Now about the second pair of upgrades
As I mentioned above, they are registered in 5 positions (level). But this time the algorithm is a bit different.
The fact of the presence of tires increases the symbol in the 5th position by 8. The presence of protection increases the same symbol by 16.
Consider the situation for level 1:
- Level 1 without improvements: xxxx 1 xxx;
- 1 level with tires: xxxx 9 xxx (+8);
- 1 level with protection: xxxx H xxx (+16);
- 1 level with tires and protection: xxxx P xxx (+ 8 + 16);
Thus, it turns out that the 1, 2, and 5 positions of the password are responsible for the improvements.
C4xx 1xxx
Trails
Only at the level of 5 tracks, you can go through them in any sequence.

For this parameter, we have a character in the 7th position of the password.
xxxx xxEx
It is logical to assume that the fact of passing a particular route is marked by some flag and has a value of 0 or 1 for it (passed / not passed).
Of all our 5 tracks, it is easy to compose an ordinary binary number that reflects all possible combinations of traversed and not passed trails.

Table 3. 7 password position - traversed traces
Here, each bit is responsible for one of 5 traces. For example, 01010 shows that tracks 1, 3 and 5 are not passed, and 2 and 4 are passed.
Now, projecting this binary number onto our alphanumeric string of characters, we get the corresponding character for the 7th position of the password. In our case, this is the symbol "A ".
Motorcycle and some more money
Now let's get down to the hardest part - the motorcycle.
In this case, 3 positions (3, 4 and 6) are tied right away according to a rather sophisticated algorithm.
xx40 x1xx
I 'll try to explain it as simple as possible.
The motorcycle itself is not very difficult to choose. There are 16 in total and 6 positions in the password are responsible for them.
xxxx x B xxx
Here, as before in money, symbols jump over one and are grouped in pairs.

Table 4. 6 password position - motorcycle
For example, 0 and 1 - the first motorcycle, 2 and 3 - the second, 4 and 5 - the third, etc. to end. The whole difficulty lies in determining which of the two characters to choose from a particular pair.
In order to understand the sequence of actions, let's go back to the 3 and 4 positions.
3 position can be represented only by even characters (0, 2, 4, 6, etc.). Odd here can not be, in principle, under any circumstances. But the symbols of 4 positions, as well as the sixth (motorcycle) are grouped in pairs: 0 and 1 - $ 0, 2 and 3 - $ 40960, 4 and 5 - 2 * 40960 $, etc. (previously, when explaining 4 positions I talked about the simplification that I made there for simplicity; now you see the whole picture).

Table 5. 3 and 4 password positions - big money (full version)
Thus, we are faced with 2 tasks:
- choose one of two possible characters for 4 positions;
- choose one of two possible characters for 6 positions.
The first thing to deal with a motorcycle
The developers took their souls to fame and made as many as four masks for choosing a motorcycle. You see them below in the image.

Table 6. Mask for the 6th position of the password (motorcycle)
The symbols that should be selected for the corresponding group (motorcycle) are highlighted in purple. For example, if we hit the 3 mask and want to select 10 motorcycle, we must take the “J” symbol for the 6th position of the password.
Now about how we need to choose the mask itself.
You may notice that this same mask rises one cell as it grows with an increase in its number. Those. mask number 2 is none other than mask number 1, shifted up one position. The mask depends on the 3 and 4 positions of the password. You can quite reasonably ask: “How will it depend on 4 positions, if we have not yet decided which of the two characters to substitute there?”, It is very simple: it does not depend on the character itself, but on the group of characters, etc. e. 0 and 1 - the first group, 2 and 3 - the second group, etc. (see Table 5). Symbols of the 3rd position have the same group, only there it consists of one symbol, because there are no odd characters at all: 0 - the first group, 2 - the second, etc. The number of this group can be found in the first column of table 5.
Now the arithmetic itself: you need to calculate the expression
{3} + {4} - 2,
where {3} and {4} are numbers, respectively, of the group of characters for positions 3 and 4 (see Table 5).
If it turns out to be a multiple of 4, then we select the first mask, if the remainder of dividing by 4n (n is a positive integer) is 1, then we select the second mask, if 2 - the third, if 3 - the fourth.
Returning to our example, we had “ 40 ” positions 3 and 4 .
4 - the third group, 0 - the first (see Table 5).
We have {3} + {4} - 2 = 3 + 1 - 2 = 2.
The remainder of dividing by 4n is 2, which means we choose 3 mask for the motorcycle.
Thus, for the 5th motorcycle we have the symbol " 9 " in the 6th position of the password.
Final jerk
We are left with the last step, namely the choice of one of two characters for the 4th position. Here a hitch arose ... My brains were already boiling from trying to explain it simply and clearly. It doesn’t explain so ...
In general, look.

Table 7. Character selection for 4 password positions
Let's look at any of the 4 masks, for example, at the first, namely at its violet part. The symbols are, as it were, grouped in two: violet, violet underneath, shift to the right, again 2 violet under each other, left shift, again 2 violet under each other and so on. So, let's imagine that each first character in this group corresponds to one, and to every second - two. Looking at the first mask from top to bottom: 1, 2, 1, 2, 1, 2 ... From the second it will be: 2, 1, 2, 1, 2, etc. The essence, I hope, is clear. What are these units and deuces for? But to what. In our example, we select a character in the fourth position from 0 and 1 (recall, we have 3 and 4 characters " 40"; 4 will always be itself, but 0 is not yet final: it can be either 0 or 1 (see Table 5)). We also know that we have the third mask (we calculated the expression above). Therefore, we look 3 mask and we find our group in it (0 and 1) - it is the first one from above. As we have just calculated, it corresponds to one, so we take the first character from this group, that is, " 0 ".
Consider another example for cleanliness: we work with the second mask and select the symbol for the 4th position from C and D. We look at the 2nd mask: C and D are the second in the purple vertical block, so we choose the second oh character, ie “ D ”.
In the end
For an appetizer, we left the 8th position.
xxxx xxxM
Everything is pretty obvious here - it's a checksum. Changing each of the first seven positions affects it in some way. Unfortunately, I cannot give the algorithms for its calculation. And he killed too much time on clarifying the mechanism of work, besides there would be even more theory. Yes, and I'm afraid the article has already stretched to indecent dimensions. So just take it for the fact that there is a checksum, which in some way depends on the first seven positions of the password.
Instead of a conclusion
Congratulations, you are a real hero, if you actually read up to this place and understood something else;)
On paper, it turned out to be terribly difficult to explain and incoherent nonsense, but in fact the theory is quite easy if you think a little and try to understand it . I tried as best I could to show everything extremely simply and clearly. I hope at least a bit of it happened. If something remains unclear to you, ask questions in the comments - I will answer.
What is this article for and who needs it at all?
The algorithms presented here will help to understand the basics of encryption in practice, and indeed satisfy the interest of people: "How does it work?" And also, I hope, it will be interesting to read the article to people who are nostalgic for the times of their favorite 16-bit races on Sega.
And finally, a small bonus. Blind password generator in case anyone suddenly wants to play.

Download
Download 2
Download 3
8 characters, as you, I hope, have already read above, no, but quickly sorting by hand a maximum of 32 options for it will not be difficult.
PS In order to play sega on a computer, you need an emulator (I use WGens) and the game rum itself. Everything is on the Internet without any problems.
PPSCongratulations to all Russians and our neighbors on winning the right to host the World Cup in 2018!
Thank you for your attention and see you soon in the open spaces of the Habr!