# Public ciphering

This chapter is dedicated to the public ciphering, a subpart of the standard security module.

## Asymmetric cipher

A public cipher is an object that encodes an input stream with a asymmetric cipher bound to a public and secret key. In theory, there is no difference between a block cipher and a public cipher. Furthermore, the interface provided by the engine is the same for both objects.

## Public cipher

A public cipher is an asymmetric stream cipher which operates with an asymmetric key. The main difference between a block cipher and a public cipher is the key nature as well as the encoded block size. With an asymmetric cipher, the size of the message to encode is generally not the same as the encoded block, because a message padding operation must occurs for each message block.

trans count (cipher:stream os is)

The `stream` method returns the number of characters that have been encoded. Like the block cipher, the `stream` method encodes an input stream or a buffer object. The number of encoded bytes is returned by the method.

## Creating a public cipher

A `PublicCipher` object can be created with a cipher constructor. The RSA asymmetric cipher is the typical example of public cipher. It is created by binding a RSA key to it. For security reasons, the key size must be large enough, typically with a size of at lease 1024 bits.

const key (afnix:sec:Key afnix:sec:Key:KRSA 1024) const rsa (afnix:sec:Rsa key)

A block cipher can be created with a key and eventually a reverse flag. Additional constructors are available to support various padding mode. Such padding mode depends on the cipher type. For example, the RSA cipher supports the ISO 18033-2 padding mode with a KDF1 or KDF2 object. Such constructor requires a hasher object as well.

# create a 1024 bits rsa key const key (afnix:sec:Key afnix:sec:KRSA 1024) # create a SHA-1 hasher const ash (afnix:sec:Sha1) # create a rsa public cipher const rsa (afnix:sec:Rsa key ash "Demo") # set the padding mode rsa:set-padding-mode afnix:sec:Rsa:PAD-OAEP-K1

## Public cipher padding mode

Like any cipher, a padding mode can be associated with the cipher. The `set-padding-mode` method can be used to set or change the padding mode. Depending on the padding mode type, additional objects might be needed at construction.

## Supported cipher padding mode

Cipher | Padding mode | Default
--- | --- | ---
RSA | PKCS 1.5, PKCS 2.1, ISO/IEC 18033-2 | PKCS 1.5

The default padding mode depends on the cipher type. For RSA, the default padding mode is set to PKCS 1.5 for compatibility reason.