Solved

encryption and decryption - sample source code

Posted on 2001-09-16
3
325 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
The viewer will learn how to implement Singleton Design Pattern in Java.

691 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