1

I've faced the next problem: I don't know where to get a byte array of private key for calling correct NTRUSigningPrivateKeyParameters.

Here is my code:

    byte [] b = new byte[16];
    Security.addProvider(new BouncyCastleProvider());

    NTRUSigningKeyGenerationParameters ntruSigningKeyGenerationParameters = NTRUSigningKeyGenerationParameters.TEST157;
    NTRUSigningPrivateKeyParameters ntruSigningPrivateKeyParameters = null;
    NTRUSigner ntruSigner = new NTRUSigner(ntruSigningKeyGenerationParameters.getSigningParameters());

    try {
        ntruSigningPrivateKeyParameters = new NTRUSigningPrivateKeyParameters(b, ntruSigningKeyGenerationParameters); // here I need to get byte array from private key
    } catch (IOException e) {
        e.printStackTrace();
    }

    ntruSigner.init(true, ntruSigningPrivateKeyParameters);
    byte [] res = ntruSigner.generateSignature(); 

Does anyone had the same problem?

user3038475
  • 343
  • 1
  • 3
  • 8
  • Is your question a) how to load a private key file into a byte array, b) how to get a private key, or c) other? Also note that you can load the private key from an InputStream; it doesn't have to be a byte array - see [javadocs](https://github.com/bcgit/bc-java/blob/master/core/src/main/java/org/bouncycastle/pqc/crypto/ntru/NTRUSigningPrivateKeyParameters.java). – DNA Apr 14 '14 at 22:23
  • @DNA a and b questions, I've seen the javadocs, but I didn't see any samples of using it – user3038475 Apr 14 '14 at 22:25

1 Answers1

1

Have your tried NTRUSigningKeyPairGenerator, and calling getEncoded() on the retrieved private key?

NTRUSigningKeyPairGenerator ntruSigningKeyPairGenerator = new NTRUSigningKeyPairGenerator();
NTRUSigningKeyGenerationParameters ntruSigningKeyGenerationParameters = NTRUSigningKeyGenerationParameters.TEST157;
ntruSigningKeyPairGenerator.init(ntruSigningKeyGenerationParameters);
AsymmetricCipherKeyPair asymmetricCipherKeyPair = ntruSigningKeyPairGenerator.generateKeyPair();
NTRUSigningPrivateKeyParameters params = (NTRUSigningPrivateKeyParameters) asymmetricCipherKeyPair.getPrivate();
System.out.println(Hex.encodeHexString(params.getEncoded()));
Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263
  • no, I haven't, but I in sources don't see anything suitable, that looks like a private key, where I can call `getEncoded()` method – user3038475 Apr 14 '14 at 22:57
  • On [the resulting private key](https://github.com/bcgit/bc-java/blob/master/core/src/main/java/org/bouncycastle/pqc/crypto/ntru/NTRUSigningPrivateKeyParameters.java) as already indicated. – Maarten Bodewes Apr 14 '14 at 22:59
  • before I can call getEncoded, first I must create a new instance. How to do it ? – user3038475 Apr 14 '14 at 23:01
  • NTRUSigningKeyPairGenerator ntruSigningKeyPairGenerator = new NTRUSigningKeyPairGenerator(); AsymmetricCipherKeyPair asymmetricCipherKeyPair = ntruSigningKeyPairGenerator.generateKeyPair(); asymmetricCipherKeyPair.getPrivate(); - it's not suitable for me – user3038475 Apr 14 '14 at 23:05
  • so where can I get the private key? – user3038475 Apr 14 '14 at 23:07
  • Not suitable to you? What *is* suitable for you? – Maarten Bodewes Apr 14 '14 at 23:15
  • I think correct object class is suitable, but not a formal wrapper, that does nothing – user3038475 Apr 14 '14 at 23:17
  • just type the code I gave in comment and see, that it's does no use in this situation :( – user3038475 Apr 14 '14 at 23:32
  • I'm so slow :D. I didn't acknowledge, that NTRUSigningPrivateKeyParameters extends AsymmetricKeyParameter. Thank you very much! Sorry my misunderstanding took a lot of your time – user3038475 Apr 15 '14 at 00:04
  • Never mind that, if you don't know about the init and the cast it can take a bit of puzzling. This is just 13.5 years of experience speaking :P – Maarten Bodewes Apr 15 '14 at 00:05
  • yep, experience is a great thing. It's a pitty, that creators of bouncycastle don't provide at least short testing samples, so that users can easily see and do all the stuff in a right way – user3038475 Apr 15 '14 at 00:13
  • I guess David is busy. Feel free to provide support :) I did provide some implementations already. – Maarten Bodewes Apr 15 '14 at 00:47