StdProcedure Class

The StdProcedure class encapsulates the encrypting and decrypting procedures outlined in References. In particular, see references [5] and [7]. This class takes care of the high level details of inserting various message indicators into an encrypted message, and stripping them off during decrypt. These message indicators tell the recipient what key list and initial key wheel settings to use when configuring their M-209 for decrypt.

class m209.procedure.StdProcedure([m_209=None[, key_list=None]])
Parameters:
  • m_209 – an instance of a M209 can optionally be provided to the procedure object. If None the procedure object will create one for internal use.
  • key_list – an instance of a KeyList can be provided if known ahead of time. Before an encrypt or decrypt operation can be performed, a key list must be provided. This can be done after object creation via set_key_list(). Note that the letter_check attribute of the KeyList is not accessed by the procedure object, and can be None if not known.

Before an encrypt() operation can be performed, a valid key list must be installed, either during procedure object construction, or by the set_key_list() method.

Decrypt operations are performed in a 3-step process.

  1. First, a call to the set_decrypt_message() method passes the message to be decrypted to the procedure and establishes the parameters to be used for the actual decrypt() operation. These decrypt parameters are returned to the caller.
  2. The caller can examine the decrypt parameters to determine which key list must be installed before a successful decrypt() operation can be carried out. The caller may call get_key_list() to examine the currently installed key list. It is up to the caller to obtain the required key list and install it with set_key_list(), if necessary. This is done by ensuring the installed key list indicator matches the key_list_ind field of the decrypt parameters.
  3. Finally decrypt() can be called. If the procedure does not have the key list necessary to decrypt the message, a ProcedureError is raised.

StdProcedure objects have the following methods:

get_key_list()
Returns:the currently installed KeyList object or None if one has not been set
set_key_list(key_list)

Establishes the KeyList to be used for future encrypt() and decrypt() operations

Parameters:key_list – the new KeyList to use
encrypt(plaintext[, spaces=True[, ext_msg_ind=None[, sys_ind=None]]])

Encrypts a plaintext message using the installed KeyList and by following the standard procedure. The encrypted text with the required message indicators are returned as a string.

Parameters:
  • plaintext – the input string to be encrypted
  • spaces – if True, space characters in the input plaintext are allowed and will be replaced with Z characters before encrypting
  • ext_msg_ind – this is the external message indicator, which, if supplied, must be a valid 6-letter string of key wheel settings. If not supplied, one will be generated randomly.
  • sys_ind – this is the system indicator, which must be a string of length 1 in the range A - Z, inclusive. If None, one is chosen at random.
Returns:

the encrypted text with the required message indicators

Raises ProcedureError:
 

if the procedure does not have a KeyList or the input indicators are invalid

set_decrypt_message(msg)

Prepare to decrypt the supplied message.

Parameters:msg – the messsage to decrypt. The message can be grouped into 5-letter groups separated by spaces or accepted without spaces.
Returns:a DecryptParams named tuple to the caller (see below)

The DecryptParams named tuple has the following attributes:

  • sys_ind - the system indicator
  • ext_msg_ind - the external message indicator
  • key_list_ind - the key list indicator
  • ciphertext - the cipher text with all indicators removed

The caller should ensure the procedure instance has the required KeyList before calling decrypt(). The key_list_ind attribute of the returned DecryptParams named tuple identifies the key list that should be installed with set_key_list().

decrypt()

Decrypt the message set in a previous set_decrypt_message() call. The resulting plaintext is returned as a string.

Returns:the decrypted plaintext as a string
Raises ProcedureError:
 if the procedure instance has not been previously configured with the required KeyList via set_key_list()

Here is a simple interactive example of performing an encrypt operation. Here we choose a random key list from our key list file, and use random indicators:

>>> from m209.keylist.config import read_key_list
>>> from m209.procedure import StdProcedure
>>>
>>> key_list = read_key_list('m209keys.cfg')
>>> proc = StdProcedure(key_list=key_list)
>>> ct = proc.encrypt('ORDER THE PIZZA AT TWELVE HUNDRED HOURS')
>>> ct
'YYGBM ENNHT VBMTJ PEEFV JWLUU PAFTS VOHEA QEPEQ OKVUA XDAUX YYGBM ENNHT'
>>>

The first and last two groups of this message contain the indicators. Here we can see the system indicator was Y, the external message indicator is GBMENN, and the key list indicator is HT.

An example session for decrypting the above message might look like:

>>> proc = StdProcedure()
>>> ct = 'YYGBM ENNHT VBMTJ PEEFV JWLUU PAFTS VOHEA QEPEQ OKVUA XDAUX YYGBM ENNHT'
>>> params = proc.set_decrypt_message(ct)
>>> params
DecryptParams(sys_ind='Y', ext_msg_ind='GBMENN', key_list_ind='HT', ciphertext='VBMTJ PEEFV JWLUU PAFTS VOHEA QEPEQ OKVUA XDAUX')
>>> key_list = read_key_list('m209keys.cfg', params.key_list_ind)
>>> proc.set_key_list(key_list)
>>> pt = proc.decrypt()
>>> pt
'ORDER THE PI  A AT TWELVE HUNDRED HOURS '
>>>

Project Versions

Previous topic

M209 Class

This Page