2

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.

charAt
  • 39
  • 9
MeMik
  • 21
  • 4
  • 1
    Your requirements are weird. Isn't the whole point of using a classical cipher that it doesn't need a computer? But JPEGs etc. always need a computer. So why not use a modern cipher? – CodesInChaos Apr 21 '13 at 10:23
  • At a glance, the idea came into my head. Thanks for your suggestion. – MeMik Apr 21 '13 at 15:23
  • erm ... you say that you want to encrypt a file, but dont want "just the content" encrypted ... basically that's what ALL cyphers do ... there is no such thing as "encrypt a file" ... it's allways "just the content" ... sometimes the file gets a new fancy file ending like .enc but that's pure cosmetics, even if that ending is associated with the decryption tool, or if the file is embedded in a self decrypting binary ... it stays just encrypted content... – DarkSquirrel42 Apr 21 '13 at 20:30
  • I see. Let's say there is an image inserted to a word (.doc) file, named plaintext.doc. We know that Hill cipher just for message (in letter A to Z) encryption. Then, all ciphers do encrypt the content/text in a .doc file. So, it means that Hill cipher can't encrypt plaintext.doc or is it possible? Please explain it for me so it'll be clear for me. Thanks. – MeMik Apr 24 '13 at 06:18

1 Answers1

1

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.

Community
  • 1
  • 1
Ilmari Karonen
  • 49,047
  • 9
  • 93
  • 153
  • From some references I found in internet, hill cipher is a traditional symmetric cipher, right? Correct me if I'm wrong, please! Then when I have an idea to combine two ciphers : Hill (symmetric) and RSA (asymmetric), is it possible to encrypt files using combination of both ciphers? How? Please give me explanation. Thanks. – MeMik Apr 21 '13 at 15:29
  • You _could_ certainly combine the Hill cipher with RSA to obtain a [hybrid cryptosystem](http://en.wikipedia.org/wiki/Hybrid_cryptosystem). Basically, you'd do this by generating a random key for the Hill cipher and then encrypting that key with RSA and sending it along with the message. However, such a system is only as strong as its weakest component, which in this case would be the Hill cipher, by a huge margin. – Ilmari Karonen Apr 21 '13 at 15:31
  • Thanks for your explanation. But, I become more curious and confuse about file encryption. When we encrypt a file, let's say word (.doc) file, shall we encrypt the file's content then create a new .enc file? Or, shall we encode the file into binary then encrypt it and change it to a new .enc file? Which is the correct way? – MeMik Apr 23 '13 at 17:37
  • I'm not sure what you mean by "the file's content" as opposed to "the file". If you mean something like, say, the text in a Word document, then no, modern file encryption program generally don't do that; they just encrypt the raw bytes making up the file as it's stored on the disk. The main reason for that is that an encryption program usually won't know, and shouldn't _need_ to know, _how_ to, say, extract the text from a Word document. Just encrypting the raw bytes lets it encrypt _any_ file. – Ilmari Karonen Apr 23 '13 at 17:48
  • Would you like to give me an example about "encrypting the raw bytes," please? If available, it'll be easier for me to understand from code example (in java, python, etc). Thanks. – MeMik Apr 23 '13 at 18:24
  • Googling for _encrypt file in java_ gives me [this question](http://stackoverflow.com/questions/5307499/how-to-encrypt-a-file-in-java-using-aes) as the first hit. If that doesn't help, you may want to ask a separate question. – Ilmari Karonen Apr 23 '13 at 20:02