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

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Collections Data Structure
8. Database SQL JDBC
9. Design Pattern
10. Development Class
11. Email
12. Event
13. File Input Output
14. Game
15. Hibernate
16. J2EE
17. J2ME
18. JDK 6
19. JSP
20. JSTL
21. Language Basics
22. Network Protocol
23. PDF RTF
24. Regular Expressions
25. Security
26. Servlets
27. Spring
28. Swing Components
29. Swing JFC
30. SWT JFace Eclipse
31. Threads
32. Tiny Application
33. Velocity
34. Web Services SOA
35. XML
Microsoft Office Word 2007 Tutorial
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java » Security » EncryptionScreenshots 
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[] argsthrows Exception {
    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
    byte[] input = "www.java2s.com".getBytes();
    byte[] keyBytes = new byte[] { 0x730x2f0x2d0x33(byte0xc80x010x730x2b0x72,
        0x060x750x6c(byte0xbd0x44(byte0xf9(byte0xc1(byte0xc10x03,
        (byte0xdd(byte0xd90x7c0x7c(byte0xbe(byte0x8e };
    byte[] ivBytes = new byte[] { (byte0xb00x7b(byte0xf50x22(byte0xc8(byte0xd6,
        0x08(byte0xb8 };

    // 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(
        ivBytes));

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

    char[] password = "password".toCharArray();
    byte[] salt = new byte[] { 0x7d0x600x430x5f0x02(byte0xe9(byte0xe0(byte0xae };
    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
w___w__w__.___j___a__v___a__2_s___.__c___o_m | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.