• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Encryption.... need help!!! :(

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
author
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So what can i do to fix this problem???
 
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how can i save the key?
 
Ulf Dittmer
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Serialization might be an option.
 
Henry Wong
author
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your help henry and ulf...
 
Self destruct mode activated. Instructions for deactivation encoded in this tiny ad.
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic