views:

857

answers:

6

I would like to encrypt a textual (configuration) file stored on disk. Trying to use DES encryption I've had fatal error on client machines, I later found out the algorithm could not handle accented characters (!) I suspect that was because I was using old packages (sun.misc.BASE64Decoder) - but I'm not sure that is the reason.

However, I'm looking for a simpler solution - I need a really simple encryption (I know some people would not agree on that) - not RSA of 128 bit keys or so, just obscuring the text from curious eyes.

It is really weird that I could not find a simple trivial solution on the web.

How can I implement a simple encryption scheme?

+2  A: 

How about ROT13? It's probably the most simple and worst encryption ever (it was also called the Caeser's Cipher)

Here's a basic implementation in Java by Jay Kominek:

import java.io.*;

public class rot13 {
  public static void main (String args[]) {
    int abyte = 0;
    try { while((abyte = System.in.read())>=0) {
      int cap = abyte & 32;
      abyte &= ~cap;
      abyte = ((abyte >= 'A') && (abyte <= 'Z') ? ((abyte - 'A' + 13) % 26 + 'A') : abyte) | cap;
      System.out.print(String.valueOf((char)abyte));
    } } catch (IOException e) { }
    System.out.flush();
  }
}
rogeriopvl
That also cannot handle accented characters.
SLaks
I don't think ROT13 is really defined for accented characters.Also, pleased to see someone getting any sort of use out of my old code. :)
Jay Kominek
A: 

If you have a single piece of text to encrypt, what about a one-time-pad? A one-time-pad is very easy to create; all you need is a random sequence of bytes the same length as the data you are encrypting

oxbow_lakes
+1  A: 

If you're not looking to really encrypt the text, why not encode with Base64? It'll look like nonsense, and it's very easy to decode. Plus, you're already using Base64 code...

Yuval
Thats a good idea, I was going to say to just add 1 to all the bytes and then subtract 1 when you want to read it. But the Base64 encoding sounds even better, could just use: org.apache.commons.codec.binary.Base64
DutrowLLC
A: 

See How do I use 3des encryption/decryption in Java? BASE64Encoder is used to represent the ciphered array of bytes, not the actual input.

eed3si9n
+5  A: 

Encryption algorithms work on raw bytes, not characters.

The reason you couldn't handle accented characters was because the code you were using to convert the characters to and from raw bytes didn't handle Unicode.

You should use AES; for an example of how to use it in Java, see here.

EDIT: Right now, you might just be hiding it from curious eyes, but there's no telling what the future will hold, and it is always much better to use strong encryption now and not find out, to late, that you should have but didn't.

SLaks
+8  A: 

Check out the Java Symplified Encryption (Jasypt).

Jasypt is a java library which allows the developer to add basic encryption capabilities to his/her projects with minimum effort, and without the need of having deep knowledge on how cryptography works.

  • High-security, standards-based encryption techniques, both for unidirectional and bidirectional encryption. Encrypt passwords, texts, numbers, binaries...
  • Transparent integration with Hibernate.
  • Suitable for integration into Spring-based applications and also transparently integrable with ACEGI (Spring Security).
  • Integrated capabilities for encrypting the configuration of applications (i.e. datasources).
  • Open API for use with any JCE provider.
  • ...and much more
Mads Hansen
Thanks for the cleanup, Alan M
Mads Hansen