MeMikhael
MeMikhael

Reputation: 21

Encrypt file using Hill Cipher and Java?

As we know, Hill cipher is a classic cipher in cryptography and is mostly used for encrypting text. I need to encrypt a file (such as .doc, .ppt, .jpeg, etc), and not just the contents of file. I already searched on the Internet, but I didn't find much research that focuses on file encryption.
What I found : encrypting text content in .txt doesn't encrypt the .txt file.
Using Java or .Net or Python (pick one or some), how to implement Hill Cipher to encrypt files as I explained above?

As a note, this question is not for my homework or assignment. I am just confused and curious about how one can implement the Hill Cipher to encrypt a file. Thank you.

Upvotes: 2

Views: 2532

Answers (1)

Ilmari Karonen
Ilmari Karonen

Reputation: 50338

The Hill cipher, like most classical ciphers from the pre-computer era, was traditionally used to only encrypt letters: that is, the valid inputs would consist only of the 26 letters from A to Z (and, in some variants, possibly a few extra symbols to make the alphabet size a prime number).

That said, there's no reason why you couldn't use a variant of the Hill cipher with, say, an alphabet size of 256, allowing you to directly encrypt input consisting of arbitrary bytes.

For a key, you'd need a random matrix that is invertible modulo 256, that is, a matrix consisting of random values from 0 to 255 chosen such that its determinant is an odd number. An easy way to generate such matrices is to just pick the matrix elements uniformly at random, calculate the determinant, and start over if it happens to be even. On average, you'll need two tries to succeed. Of course, for decryption, you'll also need to actually calculate the inverse matrix.

All that said, it's worth noting that the Hill cipher, on its own, is very easy to break. Indeed, even its inventor Lester S. Hill realized this, and only recommended it for use in combination with a substitution cipher, in what we might today consider a primitive substitution-permutation network.

Of course, nowadays we have access to much more efficient and secure ciphers, such as, say, AES. For any practical encryption tasks (as opposed to just learning exercises), you should use those rather than trying to develop your own.

Upvotes: 1

Related Questions