I am trying to initialize a Mac instance using an empty key. But it is failing with "InvalidKeyException". Following is the snippet of code working with Oracle Java, but giving exception with IBM J9. . Can someone please help me in resolving the problem?
import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException;
import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec;
public class TestHM {
public static void main(String[] args) throws NoSuchAlgorithmException, InvalidKeyException {
String alg = "HmacSHA256";
Mac m = Mac.getInstance(alg);
SecretKeySpec secretKey = new SecretKeySpec(new byte[] {0}, 1, 0, alg);
m.init(secretKey);
System.out.println("End");
}
} Current output: Exception in thread "main" java.security.InvalidKeyException: Missing key data at com.ibm.crypto.provider.HmacSHA256.engineInit(Unknown Source) at javax.crypto.Mac.a(Unknown Source) at javax.crypto.Mac.init(Unknown Source) at TestHM.main(TestHM.java:18)
Java Version: java -version java version "1.8.0" Java(TM) SE Runtime Environment (build pap6480sr3fp10-20160720_02(SR3fp10)) IBM J9 VM (build 2.8, JRE 1.8.0 AIX ppc64-64 Compressed References 20160719_312156 (JIT enabled, AOT enabled) J9VM - R28_Java8_SR3_20160719_1144_B312156 JIT - tr.r14.java_20160629_120284.01 GC - R28_Java8_SR3_20160719_1144_B312156_CMPRSS J9CL - 20160719_312156) JCL - 20160719_01 based on Oracle jdk8u101-b13