Example of using PBE without using a PBEParameterSpec : Encryption « Security « Java

Example of using PBE without using a PBEParameterSpec

import java.security.Key;
import java.security.Security;

import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;

 * Example of using PBE without using a PBEParameterSpec
public class MainClass {
  public static void main(String[] args) throws Exception {
    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
    byte[] input = "www.java2s.com".getBytes();
    byte[] keyBytes = new byte[] { 0x73, 0x2f, 0x2d, 0x33, (byte) 0xc8, 0x01, 0x73, 0x2b, 0x72,
        0x06, 0x75, 0x6c, (byte) 0xbd, 0x44, (byte) 0xf9, (byte) 0xc1, (byte) 0xc1, 0x03,
        (byte) 0xdd, (byte) 0xd9, 0x7c, 0x7c, (byte) 0xbe, (byte) 0x8e };
    byte[] ivBytes = new byte[] { (byte) 0xb0, 0x7b, (byte) 0xf5, 0x22, (byte) 0xc8, (byte) 0xd6,
        0x08, (byte) 0xb8 };

    // encrypt the data using precalculated keys

    Cipher cEnc = Cipher.getInstance("DESede/CBC/PKCS7Padding", "BC");
    cEnc.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(keyBytes, "DESede"), new IvParameterSpec(

    byte[] out = cEnc.doFinal(input);
    // decrypt the data using PBE

    char[] password = "password".toCharArray();
    byte[] salt = new byte[] { 0x7d, 0x60, 0x43, 0x5f, 0x02, (byte) 0xe9, (byte) 0xe0, (byte) 0xae };
    int iterationCount = 2048;
    PBEKeySpec pbeSpec = new PBEKeySpec(password, salt, iterationCount);
    SecretKeyFactory keyFact = SecretKeyFactory.getInstance("PBEWithSHAAnd3KeyTripleDES");

    Cipher cDec = Cipher.getInstance("PBEWithSHAAnd3KeyTripleDES");
    Key sKey = keyFact.generateSecret(pbeSpec);

    cDec.init(Cipher.DECRYPT_MODE, sKey);

    System.out.println("cipher : " + new String(out));
    System.out.println("gen key: " + new String(sKey.getEncoded()));
    System.out.println("gen iv : " + new String(cDec.getIV()));
    System.out.println("plain  : " + new String(cDec.doFinal(out)));


Related examples in the same category

1.Basic symmetric encryption example
2.Encryption and decryption with AES/ECB/PKCS7Padding
3.Cipher with AESECBPKCS7Padding BC
4.Basic symmetric encryption example with CTR using DES
5.Basic symmetric encryption example with padding and CBC using DES
6.Basic symmetric encryption example with padding and ECB using DES
7.CBC using DES with an IV based on a nonce: a hypothetical message number
8.Example of using PBE with a PBEParameterSpec
9.Get Cipher Instance Blowfish
10.Message without tampering with MAC (DES), encryption AES in CTR mode
11.Getting the Bytes of a Generated Symmetric Key
12.Encryption and Decryption using Symmetric Keys
13.Encrypt a password
14.Cryptography Streams: True Mirror
15.Get the formats of the encoded bytes
16.Create an encrypted string for password
17.Cryptography Streams: URLDigest
18.Easy Blowfish encryption
19.This program tests the AES cipher
20.This program tests the RSA cipher
21.Crypt demo
22.Encrypt User name
23.Crypt Utils
24.Encode a string using algorithm specified in web.xml and return the resulting encrypted password.
25.Encrypts the string along with salt, Decrypts the string and removes the salt