Hi, all
I have code like this for encrypt - decrypt. but every time i run, result encrypt always different.
so how to value in varible key constant.
then result same every time run
thanks.
import java.io.*;
> import java.security. *;
> import javax.crypto. *;
> import sun.misc.*;
> import com.sun.crypto. provider. *;
> public class SecretWriting {
> public static void main(String[ ] args) throws
> Exception {
> Key key;
> KeyGenerator generator =
> KeyGenerator. getInstance( "DES");
>
> key = generator.generateK ey();
>
> Cipher cipher =
> Cipher.getInstance( "DES/ECB/ PKCS5Padding" );
> cipher.init( Cipher.ENCRYPT_ MODE,key) ;
>
> String amalgam = "ABCD1234";
>
> //encrypt
> for (int i = 2; i < args.length; i++)
> amalgam += " " + args;
> byte[] stringBytes = amalgam.getBytes( "UTF8");
> byte[] raw = cipher.doFinal( stringBytes) ;
> BASE64Encoder encoder = new BASE64Encoder( );
>
> String base64 = encoder.encode( raw);
>
> //decrypt
> cipher.init( Cipher.DECRYPT_ MODE, key);
> BASE64Decoder decoder = new BASE64Decoder( );
> byte[] raw2 = decoder.decodeBuffe r(base64) ;
> byte[] stringBytes2 = cipher.doFinal( raw2);
> String result = new String(stringBytes, "UTF8");
>
> System.out.println( "Text = " + amalgam);
> System.out.println( "result encrypt = " + base64);
> System.out.println( "result decrypt = " + result);
>
> }
> }