DES Decrypt : DES « Security « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » Security » DESScreenshots 
DES Decrypt
     
/* Copyright c 2005-2012.
 * Licensed under GNU  LESSER General Public License, Version 3.
 * http://www.gnu.org/licenses
 */
package org.beangle.security.codec;

import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

public class DESDecrypt {

  public DESDecrypt(byte desKey[]) {
    this.desKey = desKey;
  }

  public byte[] doDecrypt(byte encryptText[]) throws Exception {
    SecureRandom sr = new SecureRandom();
    byte rawKeyData[] = desKey;
    DESKeySpec dks = new DESKeySpec(rawKeyData);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
    javax.crypto.SecretKey key = keyFactory.generateSecret(dks);
    Cipher cipher = Cipher.getInstance("DES");
    cipher.init(2, key, sr);
    byte encryptedData[] = encryptText;
    byte decryptedData[] = cipher.doFinal(encryptedData);
    return decryptedData;
  }

  public static void main(String args[]) throws Exception {
    String key = "ABCDEFGH";
    String value = "AABBCCDDEE";
    DESEncrypt desEncrypt = new DESEncrypt(key.getBytes());
    byte encryptText[] = desEncrypt.doEncrypt(value.getBytes());
    System.out.println("doEncrypt - " + toHexString(encryptText));
    System.out.println("doEncrypt - " new String(encryptText));
    DESDecrypt desDecrypt = new DESDecrypt(key.getBytes());
    byte decryptText[] = desDecrypt.doDecrypt(encryptText);
    System.out.println("doDecrypt - " new String(decryptText));
    System.out.println("doDecrypt - " + toHexString(decryptText));
  }

  public static String toHexString(byte value[]) {
    String newString = "";
    for (int i = 0; i < value.length; i++) {
      byte b = value[i];
      String str = Integer.toHexString(b);
      if (str.length() 2str = str.substring(str.length() 2);
      if (str.length() 2str = "0" + str;
      newString = newString + str;
    }

    return newString.toUpperCase();
  }

  private byte desKey[];
}

   
    
    
    
    
  
Related examples in the same category
1.DES Crypter and Decrypter
2.Decrypt an object with DES
3.Encrypt an object with DES
4.Encrypting a String with DES
5.Encrypting an Object with DES
6.Encrypting a File or Stream with DES
7.Triple DES
8.Encrypting with DES Using a Pass Phrase
9.DES Engine
10.DES algorithm
11.Des Encrypter
12.DES Encrypt
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.