Encrypting and decrypting does not work with key created by createKeyWithSeed unless another key created with createKey is created before or after it.
The createKeyWithSeed generates a key pair object that does not work with the encrypt and decrypt methods.
Test:
it('should be able to encrypt and decrypt with key from createKeyWithSeed', function () {
const seed = new Uint8Array([11, 12, 13, 14, 15, 16, 17, 18, 11, 12, 13, 14, 15, 16, 17, 18,
11, 12, 13, 14, 15, 16, 17, 18, 11, 12, 13, 14, 15, 16, 17, 18, 11, 12, 13, 14, 15, 16, 17, 18,
11, 12, 13, 14, 15, 16, 17, 18, 11, 12, 13, 14, 15, 16, 17, 18, 11, 12, 13, 14, 15, 16, 17, 18
])
const keyPair = NTRU.createKeyWithSeed(seed)
const plaintext = new Uint8Array([104, 101, 108, 108, 111, 0]) // "hello"
let encrypted = NTRU.encrypt(plaintext, keyPair.public)
let decrypted = NTRU.decrypt(encrypted, keyPair.private) // same as plaintext
assert.deepEqual(plaintext, decrypted)
});
Result:
AssertionError: expected <Buffer 68 65 6c 6c 6f 00;> to deeply equal <Buffer 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ... >
+ expected - actual
[
- 104
- 101
- 108
- 108
- 111
0
+ 0
....
]
But it does work when calling NTRU.createKey() before it:
The test succeeds when creating a normal key after createKeyWithSeed:
it('should be able to encrypt and decrypt with key from createKeyWithSeed', function () {
const seed = new Uint8Array([11, 12, 13, 14, 15, 16, 17, 18, 11, 12, 13, 14, 15, 16, 17, 18,
11, 12, 13, 14, 15, 16, 17, 18, 11, 12, 13, 14, 15, 16, 17, 18, 11, 12, 13, 14, 15, 16, 17, 18,
11, 12, 13, 14, 15, 16, 17, 18, 11, 12, 13, 14, 15, 16, 17, 18, 11, 12, 13, 14, 15, 16, 17, 18
])
const dummyKeyPair = NTRU.createKey(); // <== Added this
const keyPair = NTRU.createKeyWithSeed(seed)
const plaintext = new Uint8Array([104, 101, 108, 108, 111, 0]) // "hello"
let encrypted = NTRU.encrypt(plaintext, keyPair.public)
let decrypted = NTRU.decrypt(encrypted, keyPair.private) // same as plaintext
assert.deepEqual(plaintext, decrypted)
});
Why is this?
Encrypting and decrypting does not work with key created by
createKeyWithSeedunless another key created withcreateKeyis created before or after it.The
createKeyWithSeedgenerates a key pair object that does not work with the encrypt and decrypt methods.Test:
Result:
But it does work when calling
NTRU.createKey()before it:The test succeeds when creating a normal key after
createKeyWithSeed:Why is this?