[ACCEPTED]-Java simple encryption-des

Accepted answer
Score: 22

Check out the Java Simplified Encryption (Jasypt).

Jasypt is a java library 5 which allows the developer to add basic 4 encryption capabilities to his/her projects 3 with minimum effort, and without the need of 2 having deep knowledge on how cryptography 1 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
Score: 9

I'm using this simple One-Time-Pad algorithm:

import org.apache.commons.codec.binary.Base64;
public class Cipher {
  private static final String KEY = "some-secret-key-of-your-choice";
  public String encrypt(final String text) {
    return Base64.encodeBase64String(this.xor(text.getBytes()));
  }
  public String decrypt(final String hash) {
    try {
      return new String(this.xor(Base64.decodeBase64(hash.getBytes())), "UTF-8");
    } catch (java.io.UnsupportedEncodingException ex) {
      throw new IllegalStateException(ex);
    }
  }
  private byte[] xor(final byte[] input) {
    final byte[] output = new byte[input.length];
    final byte[] secret = this.KEY.getBytes();
    int spos = 0;
    for (int pos = 0; pos < input.length; ++pos) {
      output[pos] = (byte) (input[pos] ^ secret[spos]);
      spos += 1;
      if (spos >= secret.length) {
        spos = 0;
      }
    }
    return output;
  }

Don't forget 1 to add commons-codec to classpath.

Score: 7

Encryption algorithms work on raw bytes, not 11 characters.

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

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

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

Score: 4

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

Here's a basic implementation 1 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();
  }
}
Score: 2

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

Score: 0

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

Score: 0

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

More Related questions