Basic symmetric encryption example : 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 
Basic symmetric encryption example

import java.security.Security;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

/**
 * Basic symmetric encryption example
 */
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[] { 0x000x010x020x030x040x050x060x070x080x09,
        0x0a0x0b0x0c0x0d0x0e0x0f0x100x110x120x130x140x150x160x17 };

    SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");
    Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding""BC");
    System.out.println("input text : " new String(input));

    // encryption pass

    byte[] cipherText = new byte[input.length];
    cipher.init(Cipher.ENCRYPT_MODE, key);
    int ctLength = cipher.update(input, 0, input.length, cipherText, 0);
    ctLength += cipher.doFinal(cipherText, ctLength);
    System.out.println("cipher text: " new String(cipherText" bytes: " + ctLength);

    // decryption pass

    byte[] plainText = new byte[ctLength];
    cipher.init(Cipher.DECRYPT_MODE, key);
    int ptLength = cipher.update(cipherText, 0, ctLength, plainText, 0);
    ptLength += cipher.doFinal(plainText, ptLength);
    System.out.println("plain text : " new String(plainText" bytes: " + ptLength);
  }
}

           
       
BasicSymmetricEncryptionExample.zip( 1,199 k)
Related examples in the same category
1. Encryption and decryption with AES/ECB/PKCS7Padding
2. Cipher with AESECBPKCS7Padding BC
3. Basic symmetric encryption example with CTR using DES
4. Basic symmetric encryption example with padding and CBC using DES
5. Basic symmetric encryption example with padding and ECB using DES
6. CBC using DES with an IV based on a nonce: a hypothetical message number
7. Example of using PBE with a PBEParameterSpec
8. Get Cipher Instance Blowfish
9. Message without tampering with MAC (DES), encryption AES in CTR mode
10. Example of using PBE without using a PBEParameterSpec
w__ww.__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.