Solved

encryption and decryption - sample source code

Posted on 2001-09-16
3
302 Views
Last Modified: 2013-11-24
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.
0
Comment
Question by:k_s_ashok
  • 2
3 Comments
 
LVL 3

Expert Comment

by:superschlonz
ID: 6486301
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();
          }
     }
}
0
 
LVL 3

Accepted Solution

by:
superschlonz earned 100 total points
ID: 6486319
I had a little problem decrypting the first block so I added a byte array named iv and removed it after decrypting because I don't know how to do it the right way ;-)
In ECB mode it works fine but ECB is less secure.

I can't tell you which cipher to use because that depends on many things.
I bought the book 'Applied Cryptography' from Bruce Schneier and am reading it now. It explaines everything you need to know.
0
 
LVL 20

Expert Comment

by:Venabili
ID: 8916121
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
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Unable to open debugger port in Intellij idea 6 139
eclipse formatting 6 70
mockito example issue 8 62
javap not working 8 36
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now