Link to home
Start Free TrialLog in
Avatar of k_s_ashok
k_s_ashok

asked on

encryption and decryption - sample source code

how can i do encryption and decryption of the particlar field? my back-end data store sybase. i am using entity bean for storing data in the datastore.  which algorithm is the beat one? my application server  :  BEA

send the sample source code.
Avatar of superschlonz
superschlonz

Here is a little example:

import java.security.*;
import javax.crypto.*;
import au.net.aba.crypto.provider.TwofishKey;

public class crypt1
{
     private final static byte kb[] = {
          (byte)0x01, (byte)0x23, (byte)0x45, (byte)0x67,
          (byte)0x89, (byte)0xab, (byte)0xcd, (byte)0xef,
          (byte)0xfe, (byte)0xdc, (byte)0xba, (byte)0x98,
          (byte)0x76, (byte)0x54, (byte)0x32, (byte)0x10,
          (byte)0x10, (byte)0x32, (byte)0x54, (byte)0x76,
          (byte)0x98, (byte)0xba, (byte)0xdc, (byte)0xfe,
          (byte)0xef, (byte)0xcd, (byte)0xab, (byte)0x89,
          (byte)0x67, (byte)0x45, (byte)0x23, (byte)0x01
     };

     public static void main( String args[] )
     {
          String d = "Hello World! Encrypting and decrypting is cool!";
          Cipher c;
          byte iv[] = new byte[] {
               (byte)0x10, (byte)0x32, (byte)0x54, (byte)0x76,
               (byte)0x98, (byte)0xba, (byte)0xdc, (byte)0xfe,
               (byte)0xef, (byte)0xcd, (byte)0xab, (byte)0x89,
               (byte)0x67, (byte)0x45, (byte)0x23, (byte)0x01
          };
          byte pd[];
          byte cd[];
          Security.addProvider(
                    new au.net.aba.crypto.provider.ABAProvider() );
          try
          {
               System.out.println( "get Cipher" );
               c = Cipher.getInstance( "Twofish/CBC/PKCS7Padding" );
               System.out.println( "create Key" );
               Key k = new TwofishKey( kb );
               System.out.println( "init Cipher" );
               c.init( Cipher.ENCRYPT_MODE, k );
               System.out.println( "encrypt some data" );
               pd = d.getBytes();
               c.update( iv );
               cd = c.doFinal( pd );
               System.out.println( "init Cipher" );
               c.init( Cipher.DECRYPT_MODE, k );
               System.out.println( "decrypt data" );
               pd = c.doFinal( cd );
               System.out.println( "data: " + new String(pd,16,pd.length-16) );
               System.out.println( "" );
          }
          catch( Exception ex )
          {
               ex.printStackTrace();
          }
     }
}
ASKER CERTIFIED SOLUTION
Avatar of superschlonz
superschlonz

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

- Points for superschlonz

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Venabili
EE Cleanup Volunteer