Solved

encryption and decryption - sample source code

Posted on 2001-09-16
3
309 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to get all the API from website? 11 82
jar file executable 12 51
How to determine if a string is a valid SHA value 7 28
Problem to Alipay 10 17
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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 learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

815 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