how to convert byte array to key format?
i would like to know how to convert byte array into key.
i am doing an AES encryption/decryption. instead of generating a key, i would like to use my generated byte array.
byte[] clientCK = Milenage.f3(sharedSecret16, RANDbytes, opc);
开发者_运维知识库
let say i have a byte array called clientCK, stated above. i want to use it in AES encryption as shown below.
Cipher c = Cipher.getInstance("AES");
c.init(Cipher.ENCRYPT_MODE, key);
byte[] encValue = c.doFinal(valueToEnc.getBytes());
String encryptedValue = new BASE64Encoder().encode(encValue);
therefore, i need to convert that byte array clientCK into key format. please help.
You may want to use a SecretKeySpec
:
public SecretKeySpec(byte[] key, String algorithm)
Constructs a secret key from the given byte array.
U will require policy jars names below in required places, which give higher key sizes for certain algorithms/and set higher algorithms for specific countries (as per US export crypto policy) Eg JDK directory on my PC. C:\Program Files\Java\jdk1.5.0_17\jre\lib\security
U also require java.security file setup with proper crypto provider as given in shown in your java.security file as below (in directory above)
Example : security.provider.7=org.bouncycastle.jce.provider.BouncyCastleProvider
security.provider.6=com.sun.crypto.provider.SunJCE
security.provider.5=cryptix.provider.Cryptix
The security provider numbering as seen above tells the order in which the underlying security providers will be used by respective JVM. In above example 3 security providers are set for use in underlying JVM They are BouncyCastle, SunJCE, and Cryptix.
local_policy.jar and US_export_policy.jar files to be put in your JVM lib\security directory
You will need to install the Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files listed above.If you don't, the keysize is limited due to US export laws.(The above error could also be because of really wrong keysize given in application eg AES keysize given as 20 bytes instead of 32 bytes in application)
精彩评论