Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

help with encryption (java)

Options
  • 22-02-2007 11:45am
    #1
    Closed Accounts Posts: 488 ✭✭


    Hey I hav a proble with encrption,
    I can encrypt no hassle its decrypt its the problem, how to pass the cipher any suggestions.

    package ie.tippinst. me.impl;


    //imports

    public class EncrytFileSave {
    private String cyphercode;
    public String filepath;
    public boolean success;
    public String decryptCode;

    /* Pathmaker to add the extension to the file
    * @author me
    * @return pathmaker (string to use as the file extension)
    */
    public String timeNum(){
    String pathMaker;
    Calendar currt=Calendar.getInstance();
    int minute=currt.get(Calendar.MINUTE);
    int hour=currt.get(Calendar.HOUR_OF_DAY);
    int day=currt.get(Calendar.DAY_OF_MONTH);
    int month=currt.get(Calendar.MONTH)+1;
    int year=currt.get(Calendar.YEAR);

    pathMaker=(month+" "+day + " "+year +" "+ hour+" "+ minute);

    return pathMaker;

    }

    public EncrytFileSave(String contents){
    JOptionPane j= new JOptionPane();

    try{
    String path=timeNum();
    File file = new File("C:\\Documents and Settings\\access\\Desktop\\test\\"+ path);

    success = file.createNewFile();
    filepath=file.getAbsolutePath();

    encrypt(contents);

    } catch ( IOException e) {
    JOptionPane.showMessageDialog(j,"Sorry, an error has" +
    " occured:\n" + e.getMessage());
    } catch (BadPaddingException e) {
    e.printStackTrace();
    }

    }


    public void encrypt(String contents) throws IOException, BadPaddingException{
    // create a key generator based upon the Blowfish cipher
    KeyGenerator keygenerator;
    try {
    JOptionPane j= new JOptionPane();

    keygenerator = KeyGenerator.getInstance("Blowfish");

    SecretKey secretkey = keygenerator.generateKey();
    Cipher cipher = Cipher.getInstance("Blowfish");

    // initialise cipher to with secret key
    cipher.init(Cipher.ENCRYPT_MODE, secretkey);

    String inputText = contents;
    byte[] encrypted = cipher.doFinal(inputText.getBytes());

    cyphercode=new String(encrypted);
    // save the file
    BufferedWriter outfile =
    new BufferedWriter(new FileWriter(filepath));
    outfile.write(cyphercode);
    outfile.close();

    if(success){
    JOptionPane.showMessageDialog(j,"File saved successfully" +
    " \n", null, 0 );
    }

    } catch (NoSuchAlgorithmException e) {
    e.printStackTrace();
    } catch (IllegalBlockSizeException e) {
    e.printStackTrace();
    } catch (InvalidKeyException e) {
    e.printStackTrace();
    } catch (NoSuchPaddingException e) {
    e.printStackTrace();
    }

    }

    public void decrypt(String encrypted) throws IOException{
    KeyGenerator keygenerator;

    try {
    keygenerator = KeyGenerator.getInstance("Blowfish");

    SecretKey secretkey = keygenerator.generateKey();
    Cipher cipher = Cipher.getInstance("Blowfish");

    cipher.init(Cipher.ENCRYPT_MODE, secretkey);
    cipher.init(Cipher.DECRYPT_MODE, secretkey);

    // decrypt message
    byte[] decrypted = cipher.doFinal(encrypted.getBytes());


    decryptCode = new String(decrypted);

    System.out.println("The text : "+ decryptCode);
    } catch (NoSuchAlgorithmException e) {
    e.printStackTrace();
    } catch (IllegalBlockSizeException e) {
    e.printStackTrace();
    } catch (InvalidKeyException e) {
    e.printStackTrace();
    } catch (NoSuchPaddingException e) {
    e.printStackTrace();
    } catch (BadPaddingException e) {
    e.printStackTrace();
    }

    }

    }


Comments

  • Registered Users Posts: 37,485 ✭✭✭✭Khannie


    The simplest way is to write out a key object (file output stream + object output stream). Use this to initialise your cipher.

    myCipher = Cipher.getInstance("Blowfish");
    myCipher.init(Cipher.DECRYPT_MODE, key);


  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    on a side note if it like password checking your doing you should never decrypt the key. Instead encrypt the string to compare and compare results.

    btw, another thing (thats annoyed me) is whats to stop someone decompiling the java and finding the passkey? Is there a way around that where you can still decrypt the data?


  • Closed Accounts Posts: 210 ✭✭deimos


    erm, public key cryptography?


  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    You would still need the private key to decrypt it.


Advertisement