Skip to Main Content

Java Security

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Retrieving a DESede key from a file problem

843811Jun 9 2005 — edited Jun 10 2005
I am creating a DESede key and writing it to a file, but when I retrieve the value is not the same, so I can't decrypt the ciphertext.

With the following code I write the key to the file "keyFile":
        SecretKey key=null;
        try {
            KeyGenerator generator = KeyGenerator.getInstance("DESede");
            generator.init(new SecureRandom());
            key = generator.generateKey();
            byte[] keyBytes = key.getEncoded();
            FileOutputStream keyfos = new FileOutputStream("keyFile");
            keyfos.write(keyBytes);
            keyfos.close();
    	} catch(Exception e) {
            e.printStackTrace();
        }
And with the the following I retrieve it from the file:
        SecretKey key = null;
    	try {
            FileInputStream in = new FileInputStream("keyFile");
            StringBuffer keyString = new StringBuffer ();
            int c = 0;
            while((c = in.read ()) != -1)
                keyString.append ((char)c);
            byte[] keyBytes = keyString.toString().getBytes();
            DESedeKeySpec spec = new DESedeKeySpec(keyBytes);
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
            key = keyFactory.generateSecret(spec);
            in.close();
        } catch(Exception e){
            e.printStackTrace();
    	}
Can you help me cope with my problem?
Thanks in advance.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 8 2005
Added on Jun 9 2005
2 comments
169 views