Sphincs+ and SubjectPublicKeyInfo

Hey I was trying to generate a Sphincs+ public key/private key. And I wanted to write tbe public key into a pem file for future use. My appraoch was to use SubjectPublicKeyInfo to get the public key encoded.

package cdss;

import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
import org.bouncycastle.crypto.params.AsymmetricKeyParameter;
import org.bouncycastle.pqc.crypto.sphincsplus.SPHINCSPlusKeyGenerationParameters;
import org.bouncycastle.pqc.crypto.sphincsplus.SPHINCSPlusKeyPairGenerator;
import org.bouncycastle.pqc.crypto.sphincsplus.SPHINCSPlusParameters;
import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
import org.bouncycastle.pqc.crypto.util.SubjectPublicKeyInfoFactory;
import java.security.SecureRandom;

public class Sphincs2 {

    public static void main(String[] args) throws Exception {
        // Create a SPHINCSPlusKeyPairGenerator
        SPHINCSPlusKeyPairGenerator keyGen = new SPHINCSPlusKeyPairGenerator();

        // Create a SecureRandom
        SecureRandom random = new SecureRandom();

        // Set up SPHINCSPlusKeyGenerationParameters
        SPHINCSPlusKeyGenerationParameters params = new SPHINCSPlusKeyGenerationParameters(random,
                SPHINCSPlusParameters.haraka_128s_simple);

        // Initialize SPHINCSPlusKeyPairGenerator
        keyGen.init(params);

        // Generate SPHINCSPlusKeyPair
        AsymmetricCipherKeyPair pair = keyGen.generateKeyPair();

        // Extract the public key
        AsymmetricKeyParameter publicKey = pair.getPublic();
      
        // Convert the public key to SubjectPublicKeyInfo format
        SubjectPublicKeyInfo info = SubjectPublicKeyInfoFactory.createSubjectPublicKeyInfo(publicKey);

        // Print the encoded public key
        byte[] encodedPublicKey = info.getEncoded();
 
        
    }
}


However, when I complied it, it said the following:
” Exception in thread “main” java.lang.NullPointerException: ‘element’ cannot be null
at org.bouncycastle.asn1.ASN1EncodableVector.add(ASN1EncodableVector.java:37)
at org.bouncycastle.asn1.x509.AlgorithmIdentifier.toASN1Primitive(AlgorithmIdentifier.java:97)
at org.bouncycastle.asn1.DERSequence.encode(DERSequence.java:116)
at org.bouncycastle.asn1.ASN1OutputStream.writePrimitive(ASN1OutputStream.java:224)
at org.bouncycastle.asn1.ASN1Primitive.encodeTo(ASN1Primitive.java:19)
at org.bouncycastle.asn1.ASN1Object.getEncoded(ASN1Object.java:34)
at cdss.Sphincs2.main(Sphincs2.java:38)”

I wonder what is the problem?

PQC algorithms aren’t integrated into org.bouncycastle.crypto.util.SubjectPublicKeyInfoFactory yet, try org.bouncycastle.pqc.crypto.util.SubjectPublicKeyInfoFactory instead.

Leave a Comment