# simple algorithm to encrypt/decrypt a text file

I need to write a first program in one language that saves data in a text file, and write a second program in a second language to decrypt that file. The two languages in question don't include any crypto libraries, so I'm on my own.

Could someone recommend, point me to, or summarize a simple algorithm (pseudo code) that provides some level of basic encryption?

I don't have much experience in this area, but if you can summarize clearly in pseudo-code, I can implement it (don't assume prior knowledge).

It doesn't have to be bullet-proof, but just something that deters the user from understanding the text file. Something more secure than just a simple character-substitution (1 for 1), like perhaps a key that generates a random string used to XOR or similar complexity. I have no idea where to start.

• Out of curiosity, what are the two languages in question? Jun 11, 2015 at 3:25
• Matlab, Actionscript3 Jun 11, 2015 at 4:57
• If you really need to implement the algorithms yourself I'd suggest using Threefish in CTR Mode and Skein-MAC for authentication along with PBKDF2 using Skein-MAC instead of HMAC. Jun 11, 2015 at 7:28
• @SOJPM, way over my head... Jun 11, 2015 at 15:27
• Wait a little longer and I'll provide you with references and code and you'll see it's quite simple :) BTW do you have 64-bit unsigned integers? Jun 11, 2015 at 15:31

My advice would be to use a stream cipher.

First of all, it is easy to implement, because you won't have to think about dividing into blocks, padding, etc..

Secondly, the idea of stream ciphers is very easy: you generate a pseudorandom sequence of bits out of the private key. Then you XOR this sequence with a plaintext. You will only have to code the generation, the rest is elementary.

Finally, there are very easy stream ciphers, e.g. RC4 http://en.wikipedia.org/wiki/RC4; another example is Salsa20 http://en.wikipedia.org/wiki/Salsa20

And when I say "simple" I mean not only the amount of lines of code, but also the ease of debugging/ flaws search.

• Just keep in mind that RC4 is not suitable for production use. It sounds like this is an assignment for a class or something similar, in which case that's not a problem. But it's worth noting. Jun 11, 2015 at 17:57
• Not for a class. My intent is to keep most users honest. I need it to deter rather than be foolproof. Jun 11, 2015 at 18:02
• +1, I would have suggested the same (well ChaCha is more advanced but who cares), you may want to mention that authentication (HMAC/BLAKE2?) may be something the OP may desire as is key-stretching and salting (PBKDF2?). But it looks like active attacks isn't what the OP wants to counter... Jun 11, 2015 at 21:37

An alternative: You talk about text files. If you want the encrypted file to also be a text file, rather than a byte, then you could consider the Vigenère cipher. With a long randomly generated key it can be reasonably secure. If the key is as long as the plaintext file and is only used once, then it becomes a form of One Time Pad.

If you want byte-based encryption, but still want text in the encrypted file, then use Base64 to encode bytes as text. Of course you will need to decode from Base64 back to bytes before decrypting.