Question

I have encrypted my file using DES algorithm and Stored tat DES Secretkey into database converting it into String. Now i want to Convert that String to Secretkey.

Here is the code.

        secret_key = KeyGenerator.getInstance("DES").generateKey();
        alogrithm_specs = new IvParameterSpec(initialization_vector);
        // set encryption mode ...
        encrypt = Cipher.getInstance("DES/CBC/PKCS5Padding");
        encrypt.init(Cipher.ENCRYPT_MODE, secret_key, alogrithm_specs);
        //out.print("actual secret_key:"+secret_key);

        String keyString = encoder.encode(secret_key.getEncoded());
        //out.print("keyString:"+keyString);

        byte[] encodedKey = decoder.decodeBuffer(keyString);
        //out.print("byte[]:"+encodedKey);

        secret_key= new SecretKeySpec(encodedKey,0,encodedKey.length, "DES");
        //out.print("after encode & decode secret_key:"+secret_key);

Above code will convert String into key as below:

actual secret_key: 'com.sun.crypto.provider.DESKey@1807c'

keyString:hvsCa0XcXhY=

byte[]:[B@7c91fe

after encode & decode secret_key:'javax.crypto.spec.SecretKeySpec@1807c`

I want to Store that Secretkey into database by converting the key into String. Then while decrypting i want convert that String back to Key.

No correct solution

OTHER TIPS

You are seeing the object class and the hashcodes of 2 different instances sharing the same reference. If you want to confirm whether your key is getting decoded correctly, print the encoded version of the decoded key.

System.out.println("after encode & decode secret_key:" + Base64.encodeBase64String(secretKey.getEncoded()));

Here is the modified source which might help you confirm the keys are infact the same (please note I didn't have ecoder and decoder declarations available in your code above hence used a standard Base64):

SecretKey secretKey;
        String stringKey;
        try {
            secretKey = KeyGenerator.getInstance("DES").generateKey();
            // byte[] initialization_vector;
            // IvParameterSpec alogrithm_specs = new
            // IvParameterSpec(initialization_vector);
            // set encryption mode ...
            Cipher encrypt = Cipher.getInstance("DES/CBC/PKCS5Padding");
            encrypt.init(Cipher.ENCRYPT_MODE, secretKey);

            if (secretKey != null) {
                stringKey = Base64.encodeBase64String(secretKey.getEncoded());
                System.out.println("actual secret_key:" + stringKey);

                byte[] encodedKey = Base64.decodeBase64(stringKey);

                // out.print("byte[]:"+encodedKey);

                secretKey = new SecretKeySpec(encodedKey, 0, encodedKey.length,
                        "DES");
                System.out.println("after encode & decode secret_key:"
                        + Base64.encodeBase64String(secretKey.getEncoded()));
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

Outputs in both the sops are identical:

actual secret_key:dYBRc0Ok6dk=
after encode & decode secret_key:dYBRc0Ok6dk=
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top