Results 1 to 4 of 4
  1. #1
    ThisIsJAVA is offline Member
    Join Date
    Mar 2012
    Location
    Greece
    Posts
    10
    Rep Power
    0

    Question How do i store an encryption key in a file?

    Greetings fellow java programmers. I have constructed a chat application and my last requirement seems to be a simple encryption. So i have created a DESede encryption/decryption class. Thing is , every time i call the class it creates a new key. i know where the problem is in that. However i need to have this key in my server too as it is necesarry. Therefore i just want to create a DESede key and save it in a file(any type of file as long as it works will do). After that i will only call a class that loads the key from the file and does the encryption/decryption. I am fully aware that this type of security has flaws. But for now this is my objective. Any help appreciated. Thank you in advance!

  2. #2
    gauravfzd's Avatar
    gauravfzd is offline Member
    Join Date
    Oct 2010
    Location
    India
    Posts
    72
    Rep Power
    0

    Default Re: How do i store an encryption key in a file?

    You can create a key from a particular String and can store the string in a file. Then you can read the content of the file to create the same key any time you want.

    This is a example for AES:

    Java Code:
    static public String encrypt(String message, String key) throws Exception {
        byte[] raw = Base64Decoder.decodeToBytes(key);
        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        // Instantiate the cipher
        Cipher cipher = Cipher.getInstance("AES");
    
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
        System.out.println("msg is" + message + "\n raw is" + raw);
        byte[] encrypted = cipher.doFinal(message.getBytes());
        String cryptedValue = new String(encrypted);
        System.out.println("encrypted string: " + cryptedValue);
        return cryptedValue;
    }

  3. #3
    ThisIsJAVA is offline Member
    Join Date
    Mar 2012
    Location
    Greece
    Posts
    10
    Rep Power
    0

    Default Re: How do i store an encryption key in a file?

    This seems quite usefull! I can use any word as the string who initialises the key? Or is it something that has to be writen in a particular way? also, can this secretkeyspec work in place of the ordinary <<key>> i have? (these questions may seem a bit basic and silly, though i have never got busy with encryption before and its sort of a new world for me). Thank you for your help.

  4. #4
    ThisIsJAVA is offline Member
    Join Date
    Mar 2012
    Location
    Greece
    Posts
    10
    Rep Power
    0

    Default Re: How do i store an encryption key in a file?

    Do you think :
    byte[] init = base64.decodeBuffer(dec);
    will do the same job? because a method such as the one you wrote does not exist within the base64 given methods.

Similar Threads

  1. File Encryption with PGP
    By lollybanty in forum Advanced Java
    Replies: 2
    Last Post: 06-06-2011, 07:12 PM
  2. how to store the instances of this file
    By aneuryzma in forum New To Java
    Replies: 1
    Last Post: 03-27-2011, 02:42 PM
  3. Replies: 0
    Last Post: 12-23-2010, 12:32 AM
  4. how to store string in text file
    By santhosh_el in forum AWT / Swing
    Replies: 2
    Last Post: 04-03-2009, 07:21 AM
  5. store file
    By kazitula in forum Java Applets
    Replies: 0
    Last Post: 02-17-2008, 10:45 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •