Wednesday 23 November 2022

Chapter 25 // Exercise 13 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2019 and a modified version of the std_lib_facilities header found here.

Chapter 25 // Exercise 13

Use TEA (section 25.5.6) to communicate "securely" between two computers. Email is minimally acceptable.

Github: N/A

Using the program I created from here:

I created this "secure" message:

The key is "plumbus".

If you crack it, paste the message in the comments.

(If you're using my code, you'll have to edit the decipher function to read the correct text file).

Tuesday 22 November 2022

Chapter 25 // Exercise 12 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2019 and a modified version of the std_lib_facilities header found here.

Chapter 25 // Exercise 12

Write out the clear text of the example from section 25.5.6.

Github: N/A

Deciphering the text was part of Exercise 1. You can find the code here:

The deciphered text reads:
The Tiny Encryption Algorithm is one of the fastest and most efficient cryptographic algorithms in existence.
It was developed by David Wheeler and Roger Needham at the Computer Laboratory of Cambridge University.
It is a Feistel cipher which uses operations from mixed (orthogonal) algebraic groups - XOR, ADD and SHIFT in this case.
This is a very clever way of providing Shannon's twin properties of diffusion and confusion which are
necessary for a secure block cipher, without the explicit need for P-boxes and S-boxes respectively.
It encrypts 64 data bits at a time using a 128-bit key. It seems highly resistant to differential
cryptanalysis, and achieves complete diffusion (where a one bit difference in the plaintext will
cause approximately 32 bit differences in the ciphertext) after only six rounds. Performance on a
modern desktop computer or workstation is very impressive.

Monday 21 November 2022

Chapter 25 // Exercise 11 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2019 and a modified version of the std_lib_facilities header found here.

Chapter 25 // Exercise 11

Repeat the previous exercise, but keep the bits in a bitset<32>.
This was extremely easy and just involved converting the bitset to unsigned long longs in places. I like that you can subscript a bitset however, it's weird that you can't do other container-like things like set a range of the bitset.

Sunday 20 November 2022

Chapter 25 // Exercise 10 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2019 and a modified version of the std_lib_facilities header found here.

Chapter 25 // Exercise 10

Look at the bitfield example from section 25.5.5. Write an example that initialises a PPN, then reads and prints each field value, then changes each field value (by assigning to the field) and prints the result. Repeat this exercise, but store the PPN information in a 32-bit unsigned integer and use bit-manipulation operators (section 25.5.4) to access the bits in the word.
When I first read this, my immediate thought was "wtf?" Then I read it again, looked at the example and reminded myself it's just a simple bitfield exercise. Then I got to the second half where you have to read and write to an integer and that took me an hour to wrap my head around.

It's safe to say though, I now understand how shifting and masking works. It's actually much simpler that google makes it out to be. I hope my comments on the code show that. I found drawing it out on paper really helped visualise which bits to mask/shift.


Saturday 19 November 2022

Chapter 25 // Exercise 9 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2019 and a modified version of the std_lib_facilities header found here.

Chapter 25 // Exercise 9

Without using any standard headers (such as <limits>) or documentation, compute the number of bits in
an int and determine whether char is signed or unsigned on your implementation.


So to print the number of bits in an int we know that sizeof() will return us the number of bytes in a type so you can just multiply that by 8. I don't think this is cheating as sizeof is a keyword in C++ and doesn't require any standard headers to be used. 

For the second part, I used the example from page 964. It shows that char is default signed on my machine.

Friday 18 November 2022

Chapter 25 // Exercise 8 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2019 and a modified version of the std_lib_facilities header found here.

Chapter 25 // Exercise 8

Write out the numerical values of each character on your keyboard.


I cheated and just printed out all the ascii characters in a for loop as they're all on my keyboard anyway.

Thursday 17 November 2022

Chapter 25 // Exercise 7 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2019 and a modified version of the std_lib_facilities header found here.

Chapter 25 // Exercise 7

Write out the hexadecimal values from 0 to 400; write out the hexadecimal values from -200 to 200.


After the last exercise I honestly thought that this would trigger an infinite loop or something because Bjarne has that type of humour.

Wednesday 16 November 2022

Chapter 25 // Exercise 5, 6 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2019 and a modified version of the std_lib_facilities header found here.

Chapter 25 // Exercise 5

Write an infinite loop. Execute it.


Simple enough.

Chapter 25 // Exercise 6

Write an infinite loop that is hard to recognise as an infinite loop. A loop that isn't really infinite because
it terminates after completely consuming some resource is acceptable.


For this one I immediately though of a for loop going backwards but checking if the index was >= 0 (I'm not proud to say I've done this before...). However, Visual Studio is very smart these days and will give you an intellisense warning that the loop is infinite.

It also does the same for the old if(bool = false). I even googled for one but VS even recognised that as infinite. The best way to do it would be to just allocate memory in a recursive function.

Tuesday 15 November 2022

Chapter 25 // Exercise 4 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2019 and a modified version of the std_lib_facilities header found here.

Chapter 25 // Exercise 4

Add the bitwise logical operators &, |, ^, and ~ to the calculator from chapter 7.


Just when you thought it was safe, chapter 7 returns. I last touched that code 3 years ago now according to GitHub. I remember absolutely hating tokens and token streams because I thought they were some weird special type...it's just a class that gets characters from the cin buffer lol. I really love going back to old exercises as it makes me realise that I have learnt something after all.

I decided to go with the finished code from exercise 9:

And started by cleaning it up and putting it into seperate files. Then I added the bitwise operators  to my enum of symbols.

Now, as per the rules of the calculator, &, | and ^ are all expressions as they need primary's to operate on. However ~ is a prefix and can be used by itself so that makes it a primary. The main problem though is that you can't use (or shouldn't be using) bitwise operators on doubles as they're made up of 3 parts and our Tokens store all values as doubles.

To get round this I just cast everything to an int and then perform the operations but it prints a warning if the double wasn't safely converted to a float using std::modf();

I also made it so NOT requires parenthesis around the expression so you can do things like ~(5+4+sqrt(25)).

Chapter 25 // Exercise 4 - Principles & Practice Using C++