public byte [ ] seal ( byte [ ] nonce , byte [ ] plaintext ) { final XSalsa20Engine xsalsa20 = new XSalsa20Engine ( ) ; final Poly1305 poly1305 = new Poly1305 ( ) ; // initialize XSalsa20 xsalsa20 . init ( true , new ParametersWithIV ( new KeyParameter ( key ) , nonce ) ) ; // generate Poly1305 subkey final byte [ ] sk = new byte [ Keys . KEY_LEN ] ; xsalsa20 . processBytes ( sk , 0 , Keys . KEY_LEN , sk , 0 ) ; // encrypt plaintext final byte [ ] out = new byte [ plaintext . length + poly1305 . getMacSize ( ) ] ; xsalsa20 . processBytes ( plaintext , 0 , plaintext . length , out , poly1305 . getMacSize ( ) ) ; // hash ciphertext and prepend mac to ciphertext poly1305 . init ( new KeyParameter ( sk ) ) ; poly1305 . update ( out , poly1305 . getMacSize ( ) , plaintext . length ) ; poly1305 . doFinal ( out , 0 ) ; return out ; }
Encrypt a plaintext using the given key and nonce .