Solved

encryption and decryption - sample source code

Posted on 2001-09-16
3
298 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn how to implement Singleton Design Pattern in Java.

762 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

11 Experts available now in Live!

Get 1:1 Help Now