create javax.crypto.SecretKey from byte array !

i have a byte array (SecretKey Value), and i know what algorithm that SecretKey is for.
So how can i create javax.crypto.SecretKey from that byte array and algorith String.

exactly, my problem is that: i use secretkey of a KerberosTicket to encrypt my SOAP Message.
To encrypt SOAPMsg i use XMLCipher obj of XMLSecurity (from apache) obj.
To init XMLCipher i must supply a SecretKey.
So i must create a javax.crypto.SecretKey from byte array which i get from KerberosTicket but how ?

Anybody help ?
hoaivanAsked:
Who is Participating?
 
aozarovCommented:
If you have already the KerberosTicket then just call its  getSessionKey()  which will return you the SecretKey.
0
 
gnoonConnect With a Mentor Commented:
To form SecretKey from raw bytes and algorithm, try this

 SecretKeySpec keySpec = new SecretKeySpec(rawKeyBytes, algorithmString);
 SecretKey key = null;
 try
 {
     SecretKeyFactory.getInstance(algorithmString).generateSecret(keySpec);
 }
 catch(Exception e)
 {
     e.printStackTrace();
 }
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
gnoonCommented:
SecretKeyFactory.getInstance(algorithmString).generateSecret(keySpec);

should be

key = SecretKeyFactory.getInstance(algorithmString).generateSecret(keySpec);
0
 
aozarovCommented:
Hi hoaivan,
Just for the benifit of my knowledge. I though you are using Kerberos and you got the KerberosTicket, am I wrong?
If not then how do you know which algorithm the Kerberos used for this seesion based secret key (if you want to use SecretKeyFactory or descide upon DES)?
0
 
hoaivanAuthor Commented:
format of a EncryptionKey in Kerberos: you see the keytype there

          EncryptionKey ::=   SEQUENCE {
                              keytype[0]    INTEGER,
                              keyvalue[1]   OCTET STRING
          }

and

---------------+-----------+----------+----------------+---------------
Encryption type|etype value|block size|minimum pad size|confounder size
---------------+-----------+----------+----------------+---------------
NULL                0            1              0              0
des-cbc-crc         1            8              4              8
des-cbc-md4         2            8              0              8
des-cbc-md5         3            8              0              8
0
 
aozarovCommented:
Ok, so I guess I didn't understand you well as I though you already have the KerberosTicket and you wanted to get the PK encapsulated in it. "i use secretkey of a KerberosTicket to encrypt my SOAP Message."
 
0
 
hoaivanAuthor Commented:
ya, i got secretkey encapsulated in KerberosTicket but just an array of byte.
When do sign/encrypt with XMLSecurity, it need that secretkey as an instance of javax.crypto.SecretKey.

=> SecretKey sk = new SecretKeySpec(bytArr, strAlgo);

:)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.