Help Writing a Program: Solving Encryption with Matrix and Key Sequence Method

In summary, the user is asking for help with a homework problem. They say that they need help with the equation for solving the problem, and that they are not sure how to do it. You explain that you can solve it without storing the text as a Pascal 2-D array, and that you would apply the transposition as you read it in.
  • #1
doktorwho
181
6

Homework Statement


For our final test preparation we were given a few problems to write and this is one of them:
Matrix.JPG

I would be very grateful if you could help me find the easiest way to solve it.

Homework Equations


3. The Attempt at a Solution [/B]
First let me tell you my plan for writing this program so you can see if my idea is good. So let's say everything was nicely inputted and i have to do the encription now. My idea is this: I read the first element of the key sequence. Let's say its 3. Then since i know its 3 my first coloumn of the matrix (since its first element) goes to the third place (swap the third and the first). So now my first coloumn is read third. Wherever i find the element 1 in the key that matrix coloumn is my first: ( let's say its on the last position of the key, then my last coloumn is the first). When i read it i read it in the standard way. Does this seem good to you?
Here is the code till now and the place i got stuck at:
Code:
program firstprogram;
const
  MAX=10;
type
  matrix=array[1..MAX,1..MAX] of char;
  key=array[1..MAX] of integer;
var
  a:matrix;
  b:key;
  c:char;
  setchar:set of char;
  n,i:integer;

begin
  writeln('Enter the number of key elements');
  readln(n);
  if (n<5) or (n>MAX) then
     exit;
  writeln('Enter the key elements');
  for i:=1 to n do
      begin
           readln(b[i]);
           if (b[i]<1) or (b[i]>n) then
              exit;
      end;
  setchar:=['a'..'z']; {i need this to check if the matrix input is a charachter and not something else}

  {This is the place i need to ask the user to input the matrix with the original text, 
  it should type it in one line and i should divide it's charachters into places in the matrix..
  i don't know how to do this, could you help me? And possible without me having to
    take a long time to come up with it couse its kinda averagly urgent :), thanks}
end.
 
Physics news on Phys.org
  • #2
First, are there any other constraints that you need to apply to the key?

It's not quite clear, but I think you are saying that the original text comes one row at a time, but is transposed by columns and read out in columns.
You do not need to store the text as a Pascal 2-D array. You can just keep it as a string, knowing in your code where the column boundaries are.

To avoid excessive processing, I would apply the transposition as the text is read in. I.e., calculate where in the string each character is to go as you read it. If you do it right, as soon as you have finished reading it in you have the encrypted text ready as a string.
 
  • #3
haruspex said:
First, are there any other constraints that you need to apply to the key?

It's not quite clear, but I think you are saying that the original text comes one row at a time, but is transposed by columns and read out in columns.
You do not need to store the text as a Pascal 2-D array. You can just keep it as a string, knowing in your code where the column boundaries are.

To avoid excessive processing, I would apply the transposition as the text is read in. I.e., calculate where in the string each character is to go as you read it. If you do it right, as soon as you have finished reading it in you have the encrypted text ready as a string.
It is necessary to keep it in the 2-d array as that's their way to see if we can manipulate the matrices. Let me give you an example so you can understand better.
Lets say your key has 5 elements (1,2,3,5,4), you matrix then has 5 coloumns and let's suppose you write on paper you matrix with blank cells. Now you insert the text with each letter in one block. If the text is for example "youdotalkaboutfightclun" it would be like this (cant draw matrix really but here is what it would look like,
y o u d o
t a l k a
b o u t f
i g h t c
l u b
And now if the key is just (1,2,3,4,5) it would read out by coloumns like
ytbiloaoguuluhbdkttoafc but since its (1,2,3,5,4) you read almost the same way but when you gets to the forth coloumn you read the fifth couse that's where the element says 5 ( so at forth place read coloumn 5). I am suppose to permute them by the key so every coloumn would get whete it belongs but having trouble importing charachters in matrix from one long string. Ideas?
 

Related to Help Writing a Program: Solving Encryption with Matrix and Key Sequence Method

1. How do you create a matrix for encryption?

To create a matrix for encryption, you will need to determine the size of the matrix based on the length of the message and the key sequence. For example, if your message has 10 characters and your key sequence is 4, your matrix will be a 4x3 matrix. Then, fill the matrix with the characters of the message in a left-to-right, top-to-bottom order. If there are any remaining spaces, fill them with random characters.

2. What is the purpose of the key sequence in encryption?

The key sequence is used to determine the order in which the columns of the matrix will be rearranged. This adds an extra layer of security to the encryption process by making it more difficult for someone to decipher the message without the key sequence.

3. How does the matrix and key sequence method work for encryption?

The matrix and key sequence method works by rearranging the columns of the matrix according to the key sequence. Then, the characters of the message are read out in a top-to-bottom, left-to-right order, resulting in a scrambled message that is difficult to decipher without the key sequence.

4. Can this method be used to decrypt a message as well?

Yes, this method can be used to decrypt a message as well. To decrypt, you will need to use the same key sequence to rearrange the columns of the matrix in the correct order. Then, read out the characters in a left-to-right, top-to-bottom order to reveal the original message.

5. Is this method of encryption considered secure?

While the matrix and key sequence method adds an extra layer of security compared to simple substitution methods, it may not be considered completely secure. Advanced encryption methods such as RSA or AES are generally considered more secure and are commonly used for sensitive information.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
18
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
17
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
2K
Back
Top