开发者

Need algorithm to protect string with password?

开发者 https://www.devze.com 2023-03-13 05:10 出处:网络
I am looking for a simple and FAST algorithm to enc开发者_开发百科rypt/decrypt a string (length is about 128 bytes) with a password.

I am looking for a simple and FAST algorithm to enc开发者_开发百科rypt/decrypt a string (length is about 128 bytes) with a password.

Any good algorithms?

ADDED: Custom algorithm is absolutely OK. Less memory it take - better it is (for my case). No extra classes - perfect.


AES Algorithm : Implementation

AES is a federal standard for private-key or symmetric cryptography. It supports combinations of key and block sizes of 128, 192, and 256.


How about IDEA - International Data Encryption Algorithm ?

IDEA is the name of the patented and universally applicable block encryption algorithm, which permits the effective protection of transmitted and stored data against unauthorized access by third parties.

See for the implementation : How to implement IDEA?


AES or 3DES are pretty "standard" symmetrical key encryptions. Blowfish is another.

Check http://java.sun.com/developer/technicalArticles/Security/AES/AES_v1.html for using AES with Java, for instance.

Side note: If this is intended for securing something like passwords, you should really use a one-way hashing method instead (like MD5 or similar). Unless you absolutely have to be able to decrypt the text, one-way hashing is much safer. When, for instance, storing passwords in a database you would hash the password (with something like MD5) and store it. Validating a login is then done by hashing the user input and comparing it with the hashed value stored in the database.


See Java Crypto! for Encryption and Decryption


There is the Tiny Encryption Algorithm ( http://en.wikipedia.org/wiki/XXTEA ).

It's pretty simple and fast (for an encryption algorithm) and there are Java implementations.


Here is a simple encryption/decryption method. It is pretty weak, so I present it for, say education purpose:

    public static String encDec(String input, String password) {

      byte[] in = input.getBytes();
      byte[] key = password.getBytes();
      byte[] result = new byte[in.length];
      int k = 0;
      for (int i = 0; i < in.length; i++) {
         result[i] = (byte)(in[i] ^ key[k]);
         k++;
         if (k == key.length) 
           k=0;
      }

      return new String(result);
    }

It simply xors the bytes of a phrase with the bytes of a password. The same method can be used to encrypt and decrypt. Not a big challenge for a crypto analyst, by the way, but an easy start if you just need to obfuscate some data.

To make it a slightly better: don't pass a password String but a byte array with random values. But you wanted a method with a password, that's why I've implemented it that way ;)

0

精彩评论

暂无评论...
验证码 换一张
取 消