• Post Reply Bookmark Topic Watch Topic
  • New Topic

Encryption.... need help!!! :(  RSS feed

 
Armando Pangan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was confuse why my code is not working...
this my 1st code

import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.SealedObject;
import java.security.*;
import java.io.*;

import javax.crypto.*;

import javax.crypto.KeyAgreement;

import sun.io.Converters;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class DataEncrypter {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String password = "abcde";
byte[] pw = null;
Security.addProvider(new com.sun.crypto.provider.SunJCE());
try
{
sun.misc.BASE64Decoder decoder = new BASE64Decoder();
sun.misc.BASE64Encoder encoder = new BASE64Encoder();
KeyGenerator kg = KeyGenerator.getInstance("DESede");
Key key = kg.generateKey();
Cipher cipher = Cipher.getInstance("DESede", "SunJCE");
cipher.init(Cipher.ENCRYPT_MODE,key);
byte bb[] = new byte[1024];
bb = (byte[])password.getBytes();
byte[] ciphertext = cipher.doFinal(bb);
String base64 = encoder.encode(ciphertext);
System.out.println(bb);
System.out.println(password);
System.out.println("a: "+base64);
byte[] ciphertext1 = decoder.decodeBuffer(base64);
System.out.println(ciphertext1);
Cipher des = Cipher.getInstance("DESede", "SunJCE");

des.init(Cipher.DECRYPT_MODE,key);
byte[] decrypted_password = des.doFinal(ciphertext1);
String decryptedPassword = new String(decrypted_password, "UTF8");
System.out.println("b: "+(decryptedPassword));

}
catch(NoSuchAlgorithmException nsae)
{
System.out.println("No Such Algorithm Exception " + nsae.getMessage());
}
catch(NoSuchPaddingException nspe)
{
System.out.println("No Such Padding Exception " + nspe.getMessage());
}
catch(InvalidKeyException ike)
{
System.out.println("Invalid Key Exception " + ike.getMessage());
}
catch(IllegalStateException ise)
{
System.out.println("Illegal State Exception " + ise.getMessage());
}
catch(IllegalBlockSizeException ibse)
{
System.out.println("Illegal Block Size Exception " + ibse.getMessage());
}
catch(BadPaddingException bpe)
{
System.out.println("Bad Padding Exception " + bpe.getMessage());
System.out.println();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchProviderException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}

}

i tried to modified this code by changing base64(the bold one) with the value generated when i first run this code. I'm confuse why does 2 are different...
thanks in advance...
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i tried to modified this code by changing base64(the bold one) with the value generated when i first run this code. I'm confuse why does 2 are different...


So basically, you ran the program twice. And expected the encypted text from the first run, to be decrypted in the second run?

Take a look at this line...



This generates a new key. In other words, every run of your application uses a new key. The keys between the two runs are different. You can't expect something to be encrypted by one key to be properly decrypted by another key, right?

Henry
 
Armando Pangan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what can i do to fix this problem???
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry's reply suggests that you need to use the same key for both encryption and decryption. I haven't looked at the code details, but it can't be too hard to save the key from the first program run so that you can use it in the second run, can it?
 
Armando Pangan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how can i save the key?
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Serialization might be an option.
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another option is to *not* generate the key. Instead to specify a key from a fix array of bytes. For triple des, you need 24 bytes... something like this...



Henry
 
Armando Pangan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your help henry and ulf...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!