Basic symmetric encryption example : Encryption : Security : Java examples (example source code) Organized by topic

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. JSP
19. JSTL
20. Language Basics
21. Network Protocol
22. PDF RTF
23. Regular Expressions
24. Security
25. Servlets
26. Spring
27. Swing Components
28. Swing JFC
29. SWT JFace Eclipse
30. Threads
31. Tiny Application
32. Velocity
33. XML
Java Tutorial
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
Home | Contact Us
Copyright 2003 - 07 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.