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
M209can optionally be provided to the procedure object. IfNonethe procedure object will create one for internal use.key_list – an instance of a
KeyListcan 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 viaset_key_list(). Note that theletter_checkattribute of theKeyListis not accessed by the procedure object, and can beNoneif 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.
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 actualdecrypt()operation. These decrypt parameters are returned to the caller.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 callget_key_list()to examine the currently installed key list. It is up to the caller to obtain the required key list and install it withset_key_list(), if necessary. This is done by ensuring the installed key list indicator matches thekey_list_indfield of the decrypt parameters.Finally
decrypt()can be called. If the procedure does not have the key list necessary to decrypt the message, aProcedureErroris raised.
StdProcedure objects have the following methods:
- set_key_list(key_list)¶
Establishes the
KeyListto be used for futureencrypt()anddecrypt()operations
- Parameters:
key_list – the new
KeyListto use
- encrypt(plaintext[, spaces=True[, ext_msg_ind=None[, sys_ind=None]]])
Encrypts a plaintext message using the installed
KeyListand 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 withZcharacters before encryptingext_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. IfNone, one is chosen at random.- Returns:
the encrypted text with the required message indicators
- Raises:
ProcedureError – if the procedure does not have a
KeyListor 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
DecryptParamsnamed tuple to the caller (see below)The
DecryptParamsnamed 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 removedThe caller should ensure the procedure instance has the required
KeyListbefore callingdecrypt(). Thekey_list_indattribute of the returnedDecryptParamsnamed tuple identifies the key list that should be installed withset_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
KeyListviaset_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 '
>>>