0

Working on a college project for simple matrix multiplication based encryption.

The project outline is such;

text file to matrix multiplied by encryption key matrix= Encrypted file.

Encrypted file to matrix multiplied by inverse of key matrix = Decrypted file.

But I want to go a bit further and be able to do any file (text, mp3, gif etc etc).

I have been researching for hours trying to solve this problem and am starting to get a little frustrated.

The best way (and only) I can think of is for the program to read raw binary and perform encryption on that.

So--> questions:

  1. Can I extract raw binary from a file, put into matrix, perform matrix multiplication and (essentially) write back binary to file?

  2. Also what is the viability of such a method on different computers and platforms? ( I am thinking that maybe if I convert from binary to int and on decryption convert back, it might change-- different size allocations on different computers etc?)

Also, I am welcome to opinion on better solutions

---> But the basic algorithm should be based on matrix multiplication.

My code:

int writetomatrix(int current_variable)
{
    if (counter == 9){
        counter=0;
        b=0;
        a=0;}

    if (b==3) b=0;
    if (a==3) {b++;
                a=0;}
    counter++;
    B[a][b]=current_variable;
    a++;

}
    int main () {
        int *buffer= new int[1];
        ifstream input;
        input.open ("input.txt",ios::in|ios::binary);
        input.read ((char*)&buffer, 1);
        writetomatrix(buffer);
    }

The error I get:

initializing argument 1 of ‘int writetomatrix(int)’
midnightBlue
  • 1,735
  • 2
  • 13
  • 13
  • Some (old) versions of Unix `crypt` used the Hill cipher, which is pretty much what you've described. I haven't looked, but you could probably find source to at least one such implementation fairly easily. – Jerry Coffin Mar 07 '12 at 22:05
  • Thanks for the reply, but I wanted to know if you could do binary calculations like I said? – midnightBlue Mar 07 '12 at 22:08
  • The short answer those is "yes" -- you can read and manipulate binary data. Typically you'd do it as an array (or vector, etc.) of `unsigned char`. – Jerry Coffin Mar 07 '12 at 22:10
  • Thanks again for the reply :), So if I understand this correctly, you could say-- multiply an unsigned char by another unsigned char? EDIT: and that would perform binary multiplication? – midnightBlue Mar 07 '12 at 22:13
  • Yes -- `unsigned char` is just a small integer type (usually with the range 0..255). You can do math on it just like any other integer type (though it'll be converted to `int` first, so you may have to add a cast to get the result back to `unsigned char`). – Jerry Coffin Mar 07 '12 at 22:15
  • Oh... ok I'm gonna try that right now – midnightBlue Mar 07 '12 at 22:16
  • I just realised I need bigger range than 255 values... :/ – midnightBlue Mar 07 '12 at 22:18
  • In that case, you'll probably need to read unsigned chars, convert them to something like `int`, then write out the individual bytes of the `int` for the encrypted value. – Jerry Coffin Mar 07 '12 at 22:20
  • Right ok.. I am trying to write this code; (code in main question) I know its something simple going wrong somewhere, but can't find what. – midnightBlue Mar 07 '12 at 22:22
  • But anyway thank you for your replies and your time. – midnightBlue Mar 07 '12 at 22:28

1 Answers1

0

You can read the binary file using fread to an array of char or of int. As long as byte order remains the same, you can read any file and write it back. You can do what you want with the bytes or words you read. You can use sizeof to know the size of an int. On most platforms today it is 4 bytes.

Charles Brunet
  • 21,797
  • 24
  • 83
  • 124