DES Crypter and Decrypter : DES « Security « Java






DES Crypter and Decrypter

    
import java.security.Security;
import java.security.spec.AlgorithmParameterSpec;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;

public class MainClass {
  public static void main(String args[]) throws Exception {
    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
    SecretKey key = KeyGenerator.getInstance("DES").generateKey();

    // for CBC; must be 8 bytes
    byte[] initVector = new byte[] { 0x10, 0x10, 0x01, 0x04, 0x01, 0x01, 0x01, 0x02 };

    AlgorithmParameterSpec algParamSpec = new IvParameterSpec(initVector);
    Cipher m_encrypter = Cipher.getInstance("DES/CBC/PKCS5Padding");
    Cipher m_decrypter = Cipher.getInstance("DES/CBC/PKCS5Padding");

    m_encrypter.init(Cipher.ENCRYPT_MODE, key, algParamSpec);
    m_decrypter.init(Cipher.DECRYPT_MODE, key, algParamSpec);

    byte[] clearText = "www.java2s.com".getBytes();

    byte[] encryptedText = m_encrypter.doFinal(clearText);

    byte[] decryptedText = m_decrypter.doFinal(encryptedText);

    System.out.println(new String(clearText));
    System.out.println(new String(encryptedText));
    System.out.println(new String(decryptedText));

  }

}
           
         
    
    
    
  








DESCrypterandDecrypter.zip( 1,198 k)

Related examples in the same category

1.Decrypt an object with DES
2.Encrypt an object with DES
3.Encrypting a String with DES
4.Encrypting an Object with DES
5.Encrypting a File or Stream with DES
6.Triple DES
7.Encrypting with DES Using a Pass Phrase
8.DES Engine
9.DES algorithm
10.Des Encrypter
11.DES Decrypt
12.DES Encrypt