Cypher Text Boolean expression

Cypher Text Boolean expression

Today’s lab will have two new functions:
sub_encode: Given plain text and a key, it will return cypher text.
sub_decode: Given cypher text and a key, it will return plain text.
Both functions take a string as the first parameter and a matrix (key) as the second parameter.
Both functions return a string.
Write a function with two parameters and a return value.
Convert char to double and back to char.
Write a for loop.
Use a matrix in a Boolean expression to produce a Boolean matrix.
Use indexing to alter the order of elements in a matrix.
After the rotating cypher was demonstrated to be easily broken with pen and paper, a standard substitution cypher was used. In this cypher, every character is swapped with another
character. You’ve likely seen these:
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
H E D G O Z Y X W V U T S R Q P N M L K J I F C B A

Given the code “XOTTQ”, you find the letter on the bottom row and replace it with the letter on the top row to get “HELLO”. To encode a phrase, you find the letter on the top row and replace it with the letter on the bottom row.
The character values in your string will be from 1 to 127 (the ASCII code values). You will have a key matrix that contains the values 1 through 127, but in random order. It is a mapping because the indexes will be 1 through 127 in order.

key = [4 18 104 82 46 32 119 1 98 72 154 13 …and so on…]
As stated, this is a map because key(1) = 4. So, an ASCII value of 1 will be replaced with an ASCII value of 4. Similarly, key(2) is 18. So, an ASCII value of 2 will be replaced with an ASCII value of 18.
Another way to imagine this is:
index 1 2 3 4 5 6 7 8 9 10 11 12 …
value 4 18 104 82 46 32 119 1 98 72 154 13 …
Given a character that has the value of the index, replace it with the value of the matrix value. This lab will be described in three steps. The first is used to ensure that you have Monday’s lab
functioning. The second is used to ensure that you have the substitution working. The third is used to complete the lab with a rotating substitution. Make absolutely certain that your functions work properly in each step before continuing.

Step 1: Copy the code from Monday.

You should have sub_encode and sub_decode, not cc_encode and cc_decode, when you are done.
Save your two m files (which must be the same names as your functions) and test it.
>>clear
>> code = sub_encode(‘Hello’,5)
code = Mjqqt
>>sub_decode(code,5)
ans = Hello

  • The plain and cypher variables are swapped in the two functions.

cc_encode.m:
function cypher = cc_encode(plain, key)
cypher = plain + key;
cypher(cypher>127) = cypher(cypher>127) – 127;
cypher = char(cypher);
endfunction
cc_decode.m:
function plain = cc_decode(cypher, key)
plain = cypher – key;
plain(plain<1) = plain(plain<1) + 127;
plain = char(plain);
endfunction

The comments should be identical to Monday with one minor alteration. On Monday, the key was asingle value, such as 5 or 42. Now, the key is a matrix of values from 1 to 127 in a random order.

Step 2: Alter the existing functions.

The old encode function used two steps to change the ASCII values. First, it added 127 to the plainstring using element-wise addition. In other words, it added the value of key to every value in the plainstring. After that, it subtracted 127 from every value in the cypher string that was greater than 127. It has 127 indexes each mapped to a unique randomvalue from 1 to 127. For each ASCII value in the plain string, we want to look up the index in the keymatrix and get the value of the matrix for that key.
You will begin with an empty cypher matrix (we convert it from a cypher matrix to acypher string using the char function at the end of the function). For each ASCII value in the plain
string, use that ASCII value as the index for the key matrix. Doing so will give you a value from 1 to127. Place that value in the cypher matrix. You should realize that you will using a for loop. for i=1; i<size(plain); i++
You set a variable equal to a matrix and each time through theloop, the variable has the value of the next element of the matrix. That char
function call will turn the matrix of doubles into a string. It is all the odd numbers from 1 to 127
followed by all the even numbers. This allows you to do a test using the same key and ensure you getthe same result.
>> clear
>> key = [1:2:127 2:2:127];
>> code = sub_encode(‘Hello’,key)
code = JXX^
In case you want to know why you started with 5 characters and got only 4 out of it, not all ASCIIvalues are printable characters. For example, ASCII 7 is a bell. If you print it, it is supposed to make thecomputer make some sort of beep noise. In this particular case, ASCII 16 is a data link escapecharacter, which doesn’t print anything.

>>code + 0
ans =
16 74 88 88 94
When your encode function produces the correct output, work on your decode function. It is very
similar, but there is a catch. In your encode function, you used something like key(c) to get the value ofkey at index c. In the decode function, we want to go the opposite direction. Given a value, c, we wantto know the index of c in the matrix key. Do you have a guess at how you would find an index for avalue?
The find function returns EVERY index (as a matrix) for non-zero values in an array. So, the syntax isnot anything like: find(key, c). It is simply find(key). It returns the index of every non-zero value inkey. You have anarray named key and a value named c. You want a matrix that has one 1 value and a bunch of 0 values.
The index you get from above will be appended to an initially empty plain matrix. After the for loop,the plain matrix will be converted to a string with the char function.
You should have two working functions now. Test them. Encode a string with the sub_encode functionand a key. Decode the string with the sub_decode function and the same key.
>>clear
>> key = [1:2:127 2:2:127];
>> code = sub_encode(‘Hello’,key)
code = JXX^
>>sub_decode(code,key)
ans = Hello

Step 3: Rotate the key.

This method of encryption was cracked with pen andpaper in the 1500’s. We need to at least get something that wasn’t cracked until 1900. To do so, we willrotate the key.
A key rotation means that the values of the key shift every time the key is used. Consider this example:
I have a plain text string ‘Hello’ and my key is [5 22 85 17 121 3 58] (the key is too short, but this is justfor an example). In the first round of my for loop, ‘H’ is replaced using the key. The key becomes [58 5 22 85 17 121 3].
To implement this, add code inside the for loop after your do your character replacement. This codeshould remove the last item of key and append it to the beginning of the key matrix.
Test your code now and you will see that the two L’s in hello don’t encode as the same character. That isbecause the key shifted after the first L was encoded.
>>clear
>> key = [1:2:127 2:2:127];
>> code = sub_encode(‘Hello’,key)
code = HTRV
>>sub_decode(code,key)
ans = Hello
When your code works, submit both sub_encode.m and sub_decode.m on Blackboard.

Solution 

sub_decode.m 

% sub_decode function decripts a cyphered text into a plain text with the

% help of a key matrix

% inputs –

% cypher – text to be decrypted

% key – matrix key to be used in decryption

function plain = sub_decode(cypher, key)

for i = 1:length(cypher)

keyf = double(not(logical(key-cypher(i)))); %matrix with zeros where value is not equal to cyphered text and 1 otherwise

plain(i) = find(keyf); %finding the index whre 1 exists, i.e. finding the index of cyphered text

key = [key(length(key)) key(1:length(key)-1)]; %rotating the key

end

plain = char(plain); %plain converted from ascii to char

end 

sub_encode.m 

% sub_encode function encripts a plain text into a cyphered text with the

% help of a key matrix

% inputs –

% plain – text to be encrypted

% key – matrix key to be used in encryption

function cypher = sub_encode(plain, key)

for i=1:length(plain)

cypher(i) = key(plain(i));  %cyphered text is the key matrix value with the index equal to plain text

key = [key(length(key)) key(1:length(key)-1)]; %key is rotated

end

cypher = char(cypher); %cypher converted from ascii to char

end