Sections :
RSS Feed
You too, please publish your useful code snippets in any programming language : write an article !
Plateforme d'envoi de gros fichiers en ligne
Dépannage site web
Blog infogérance
Hébergement e-mail
|
Olivier Ligny - - 12/03/2008 - vue 10602 fois
DES 256 bits encrypter/decrypter - Java source code
A java class to encrypt/decrypt data using 96 bits DES algorithm.
Usage :
String encrypted = DesEncrypter.cryptString("hello world", "ABCDEFABCDEFABCDEFABCDEF");
String plain = DesEncrypter.decryptString(encrypted, "ABCDEFABCDEFABCDEFABCDEF");
The key is a 24 hex string (= 12 bytes = 96 bits)
This class uses the javax.crypto extensions, you must have it in your classpath.
import java.io.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import javax.crypto.interfaces.*;
import com.sun.crypto.provider.SunJCE;
import java.security.spec.*;
import java.util.Date;
public class DesEncrypter {
   Cipher ecipher;
   Cipher dcipher;
   // 8-byte Salt
   static byte[] salt = {
       (byte)0xA9, (byte)0x9B, (byte)0xC8, (byte)0x32,
       (byte)0x56, (byte)0x35, (byte)0xE3, (byte)0x03
   };
   // Iteration count
   static int iterationCount = 19;
   public static String cryptString(String str, String key) {
      String encrypted = "";
      try {
          DesEncrypter encrypter = new DesEncrypter(key);
          encrypted = encrypter.encrypt(str);
      } catch (Exception e) {
         System.out.println(e);
         e.printStackTrace();
      }
      return encrypted;     Â
   }
   public static String decryptString(String str, String key) {
      String decrypted = "";
      try {
          DesEncrypter encrypter = new DesEncrypter(key);
          decrypted = encrypter.decrypt(str);
      } catch (Exception e) {
         System.out.println(e);
         e.printStackTrace();
      }
      return decrypted;     Â
   }
   DesEncrypter(String passPhrase) {
       try {
           // Create the key
           KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), salt, iterationCount);
           SecretKey key = SecretKeyFactory.getInstance(
               "PBEWithMD5AndDES").generateSecret(keySpec);
           ecipher = Cipher.getInstance(key.getAlgorithm());
           dcipher = Cipher.getInstance(key.getAlgorithm());
           // Prepare the parameter to the ciphers
           AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterationCount);
           // Create the ciphers
           ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
           dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
       } catch (java.security.InvalidAlgorithmParameterException e) {
       } catch (java.security.spec.InvalidKeySpecException e) {
       } catch (javax.crypto.NoSuchPaddingException e) {
       } catch (java.security.NoSuchAlgorithmException e) {
       } catch (java.security.InvalidKeyException e) {
       }
  }
   public String byteArrayToString(byte[] data) {
      String res = "";
      StringBuffer sb = new StringBuffer();
      for(int i=0; i<data.length; i++) {
         int n = (int) data[i];
         if(n<0) n += 256;
         sb.append((char) n);
      }
      res = sb.toString();
      return res;
   }
  Â
   public byte[] stringToByteArray(String s){
      byte[] temp = new byte[s.length()];
      for(int i=0;i<s.length();i++){
         temp[i] = (byte) s.charAt(i);
      }
      return temp;
   }
   public String encrypt(String str) {
       try {
           // Encode the string into bytes using utf-8
           byte[] utf8 = str.getBytes("UTF8");
           // Encrypt
           byte[] enc = ecipher.doFinal(utf8);
           return byteArrayToString(enc);
       } catch (javax.crypto.BadPaddingException e) {
       } catch (IllegalBlockSizeException e) {
       } catch (UnsupportedEncodingException e) {
       } catch (java.io.IOException e) {
       }
       return null;
   }
   public String decrypt(String str) {
       try {
           byte[] dec = stringToByteArray(str);
           // Decrypt
           byte[] utf8 = dcipher.doFinal(dec);
           // Decode using utf-8
           return new String(utf8, "UTF8");
       } catch (javax.crypto.BadPaddingException e) {
       } catch (IllegalBlockSizeException e) {
       } catch (UnsupportedEncodingException e) {
       } catch (java.io.IOException e) {
       }
       return null;
   }
  Â
  Â
  Â
   public static void main(String[] args) {
     Â
     Â
   }
  Â
    public static String static_intArrayToString(int[]t){
      StringBuffer sb = new StringBuffer();
      for(int i=0;i<t.length;i++){
         sb.append((char)t[i]);
      }
      return sb.toString();
   }
 Â
}
|