o
    CDiqk                     @   sZ   d Z ddlZejd ZdZdZdZdZG dd deZ	G dd de	Z
G d	d
 d
e	ZdS )a6	  A pure python implementation of the DES and TRIPLE DES encryption algorithms.

Class initialization
--------------------
pyDes.des(key, [mode], [IV], [pad], [padmode])
pyDes.triple_des(key, [mode], [IV], [pad], [padmode])

key     -> Bytes containing the encryption key. 8 bytes for DES, 16 or 24 bytes
	   for Triple DES
mode    -> Optional argument for encryption type, can be either
	   pyDes.ECB (Electronic Code Book) or pyDes.CBC (Cypher Block Chaining)
IV      -> Optional Initial Value bytes, must be supplied if using CBC mode.
	   Length must be 8 bytes.
pad     -> Optional argument, set the pad character (PAD_NORMAL) to use during
	   all encrypt/decrpt operations done with this instance.
padmode -> Optional argument, set the padding mode (PAD_NORMAL or PAD_PKCS5)
	   to use during all encrypt/decrpt operations done with this instance.

I recommend to use PAD_PKCS5 padding, as then you never need to worry about any
padding issues, as the padding can be removed unambiguously upon decrypting
data that was encrypted using PAD_PKCS5 padmode.

Common methods
--------------
encrypt(data, [pad], [padmode])
decrypt(data, [pad], [padmode])

data    -> Bytes to be encrypted/decrypted
pad     -> Optional argument. Only when using padmode of PAD_NORMAL. For
	   encryption, adds this characters to the end of the data block when
	   data is not a multiple of 8 bytes. For decryption, will remove the
	   trailing characters that match this pad character from the last 8
	   bytes of the unencrypted data block.
padmode -> Optional argument, set the padding mode, must be one of PAD_NORMAL
	   or PAD_PKCS5). Defaults to PAD_NORMAL.
	  

Example
-------
from pyDes import *

data = "Please encrypt my data"
k = des("DESCRYPT", CBC, "        ", pad=None, padmode=PAD_PKCS5)
# For Python3, you'll need to use bytes, i.e.:
#   data = b"Please encrypt my data"
#   k = des(b"DESCRYPT", CBC, b"        ", pad=None, padmode=PAD_PKCS5)
d = k.encrypt(data)
print "Encrypted: %r" % d
print "Decrypted: %r" % k.decrypt(d)
assert k.decrypt(d, padmode=PAD_PKCS5) == data


See the module source (pyDes.py) for more examples of use.
You can also run the pyDes.py file without and arguments to see a simple test.

Note: This code was not written for high-end systems needing a fast
      implementation, but rather a handy portable solution with small usage.

    N      c                   @   s   e Zd ZeddefddZdd Zdd Zdd	 Zd
d Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd ZdS )_baseDesNc                 C   sz   |r|  |}|r|  |}d| _|r|tkrtd|r/t|| jkr/tdt| j d || _|| _|| _|| _	d S )N   )Cannot use a pad character with PAD_PKCS52Invalid Initial Value (IV), must be a multiple of  bytes)
_guardAgainstUnicode
block_size	PAD_PKCS5
ValueErrorlenstr_mode_iv_padding_padmode)selfmodeIVpadpadmode r   H/var/www/Datamplify/venv/lib/python3.10/site-packages/smb/utils/pyDes.py__init__l   s   


z_baseDes.__init__c                 C      | j S )zgetKey() -> bytes)_baseDes__keyr   r   r   r   getKey~      z_baseDes.getKeyc                 C   s   |  |}|| _dS )z*Will set the crypting key for this object.N)r	   r   r   keyr   r   r   setKey   s   

z_baseDes.setKeyc                 C   r   )z#getMode() -> pyDes.ECB or pyDes.CBCr   r   r   r   r   getMode   r   z_baseDes.getModec                 C   
   || _ dS z6Sets the type of crypting mode, pyDes.ECB or pyDes.CBCNr#   r   r   r   r   r   setMode      
z_baseDes.setModec                 C   r   )z5getPadding() -> bytes of length 1. Padding character.)r   r   r   r   r   
getPadding   r   z_baseDes.getPaddingc                 C   s   |dur	|  |}|| _dS z5setPadding() -> bytes of length 1. Padding character.N)r	   r   )r   r   r   r   r   
setPadding   s   

z_baseDes.setPaddingc                 C   r   )z3getPadMode() -> pyDes.PAD_NORMAL or pyDes.PAD_PKCS5r   r   r   r   r   
getPadMode   r   z_baseDes.getPadModec                 C   r%   zBSets the type of padding mode, pyDes.PAD_NORMAL or pyDes.PAD_PKCS5Nr-   r'   r   r   r   
setPadMode   r)   z_baseDes.setPadModec                 C   r   )zgetIV() -> bytes)r   r   r   r   r   getIV   r   z_baseDes.getIVc                 C   s<   |r	t || jkrtdt| j d | |}|| _dS )=Will set the Initial Value, used in conjunction with CBC moder   r   N)r   r
   r   r   r	   r   )r   r   r   r   r   setIV   s   

z_baseDes.setIVc                 C   s   |d u r|   }|r|tkrtd|tkrDt|| j dkr!|S |s'|  }|s4tdt| j d || jt|| j  | 7 }|S |tkrhdt|| j  }tdk r_||t	| 7 }|S |t
|g| 7 }|S )Nr   r   zData must be a multiple of zA bytes in length. Use padmode=PAD_PKCS5 or set the pad character.r      )r.   r   r   
PAD_NORMALr   r
   r*   r   _pythonMajorVersionchrbytesr   datar   r   pad_lenr   r   r   _padData   s(   	z_baseDes._padDatac                 C   s   |s|S |r|t krtd|d u r|  }|tkr7|s |  }|r5|d | j  || j d  | }|S |t krQtdk rFt|d }n|d }|d |  }|S )Nr   r4   )	r   r   r.   r5   r*   r
   rstripr6   ordr9   r   r   r   
_unpadData   s(   
z_baseDes._unpadDatac                 C   sR   t dk rt|trtd|S t|tr'z|dW S  ty&   Y tdw |S )Nr4   z4pyDes can only work with bytes, not Unicode strings.asciiz6pyDes can only work with encoded strings, not Unicode.)r6   
isinstancer   r   encodeUnicodeEncodeError)r   r:   r   r   r   r	      s   
	
z_baseDes._guardAgainstUnicode)__name__
__module____qualname__ECBr5   r   r   r"   r$   r(   r*   r,   r.   r0   r1   r3   r<   r@   r	   r   r   r   r   r   k   s    r   c                	   @   s   e Zd ZdZg dZg dZg dZg dZg dZg dg dg d	g d
g dg dg dg dgZ	g dZ
g dZdZdZeddefddZdd Zdd Zdd Zdd Zdd Zd d! Zd"d# Zd(d$d%Zd(d&d'ZdS ))desa  DES encryption/decrytpion class

	Supports ECB (Electronic Code Book) and CBC (Cypher Block Chaining) modes.

	pyDes.des(key,[mode], [IV])

	key  -> Bytes containing the encryption key, must be exactly 8 bytes
	mode -> Optional argument for encryption type, can be either pyDes.ECB
		(Electronic Code Book), pyDes.CBC (Cypher Block Chaining)
	IV   -> Optional Initial Value bytes, must be supplied if using CBC mode.
		Must be 8 bytes in length.
	pad  -> Optional argument, set the pad character (PAD_NORMAL) to use
		during all encrypt/decrpt operations done with this instance.
	padmode -> Optional argument, set the padding mode (PAD_NORMAL or
		PAD_PKCS5) to use during all encrypt/decrpt operations done
		with this instance.
	)88   0   (             r   r   9   1   )   !         	   r   :   2   *   "         
   r   ;   3   +   #   >   6   .   &               =   5   -   %               <   4   ,   $                        r4   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )0rp   rO   r]      r   ry   r   rz   rh   rq   rw   rV   rg   r\   r|   r4   rT         ri   r[   r{   rx   r   rL   r_   rf   ru   rd   rc   rn   '   rX   rt   rM   /   r`   rK   re   7   rS   rs   rl   rR   rQ   ra   rv      )@rP   rQ   rR   rS   rT   rU   rV   r   r^   r_   r`   ra   rz   r{   r|   r4   rj   rk   rl   rm   rn   ro   rp   rq   ?   r   r   r   r   r}   r   r~   rJ   rK   rL   rM   rN   rO   r   r   rW   rX   rY   rZ   r[   r\   r]   r   rr   rs   rt   ru   rv   rw   rx   ry   rb   rc   rd   re   rf   rg   rh   ri   )0r   r   r   r   r4   ry   r4   ry   rq   ri   r~   r   r~   r   rV   r]   r|   rx   r|   rx   rp   rh   r   rO   r   rO   rU   r\   r{   rw   r{   rw   ro   rg   r}   rN   r}   rN   rT   r[   rz   rv   rz   rv   rn   rf   r   r   )@rh   ry   rp   r   r   r   r|   r   r4   r]   ri   rx   rq   rV   r   r~   r   r   r~   ry   rh   r   rp   r   r]   ri   rx   r|   rV   rq   r4   r   ry   r   rh   r   rp   ri   r   r|   r   rx   rV   r~   r4   r]   rq   r   r   rx   r   r   ry   rV   r   r~   rq   r|   r4   rh   r]   r   ri   rp   )@r   r   r   rh   ri   r|   r4   ry   rV   r~   r   rp   rx   r   rq   r]   r4   rp   ry   r~   r   r   r   rh   rx   r   r   r]   ri   rV   r|   rq   r   rh   r~   r|   r]   ry   rp   r   rq   r   rx   ri   rV   r4   r   r   rp   r   r]   r   r4   r   ry   r   r|   ri   r~   rx   r   rq   rh   rV   )@r]   r   rV   rh   ri   r4   r   rq   r   rp   rx   r~   r|   ry   r   r   rp   r~   r   rV   r4   ry   ri   r]   r   r   rq   rh   rx   r|   r   r   rp   ri   ry   rV   r   r   r4   r   r|   r   r   rx   rq   r]   rh   r~   r   r]   rp   r   ri   rV   r   r~   ry   r   rh   r4   r|   rq   r   rx   )@r~   rp   rh   r4   r   ri   rV   r]   r   r   r   rq   r|   rx   ry   r   rp   r   r|   rq   ri   r   r   r4   ry   r~   r   rx   r   r]   rh   rV   r]   ri   rV   r   rx   r|   r~   rp   r   r   r4   rh   rq   r   r   ry   r4   r   r   ri   r]   r   rp   r   rV   ry   rq   r|   rx   r~   r   rh   )@r   rx   ry   r   r~   r]   r|   ri   r   rq   r4   r   rp   r   rh   rV   rh   r|   r   rx   ry   r~   rp   r   rq   r   r   r]   r4   rV   r   ri   ry   r   r   r|   r]   rp   r~   r   r   rV   rx   rq   ri   r4   r   rh   r|   r   rx   r~   r   rh   r   rp   ri   r   r   rV   r]   ry   rq   r4   )@rx   r   r]   r   rV   r   ri   r   r   rp   r4   ry   rh   r~   rq   r|   r]   r   ry   r   r~   rx   rV   rq   ri   r   rp   rh   r   r|   r4   r   rV   rh   r   rq   r   r   rx   r4   r~   r   ry   r]   r   rp   r|   ri   ry   r4   r   rx   rV   rq   r   r]   r|   rh   r   r~   ri   r   r   rp   )@ry   r|   r   rh   r   r   r   rp   r4   rx   rV   r~   rq   r]   ri   r   rp   r   r|   r~   ry   rV   r   r]   rh   r4   rq   rx   r   r   r   ri   r   ry   r|   rp   rx   r4   r~   rh   r]   r   ri   r   r   rq   rV   r   ri   r|   rp   r   r   ry   r]   r~   rV   rq   r   r   rh   r   r4   rx   )@rp   r   r   ry   ri   r   r|   r   r]   rV   r4   rh   rq   r   rx   r~   r   r   rp   r   r]   r4   r~   ry   rx   rq   ri   r|   r   rh   rV   r   r~   r|   ry   r   rV   rx   rh   r   r   ri   r]   rp   r   r4   rq   r   r   r   rh   r~   ry   r]   r   rp   r   rx   rV   r   r4   rq   ri   r|   ) r   ri   r{   rw   rv   r|   rz   rO   r   rh   rg   rT   ry   rU   rf   rV   r   r~   r}   rp   r   r[   r   r   r\   rx   rn   rq   ro   r]   r4   rN   )@r   r~   r   r   r   r}   r   r   re   ri   rd   rh   rc   rg   rb   rf   rm   rq   rl   rp   rk   ro   rj   rn   ru   ry   rt   rx   rs   rw   rr   rv   ra   r4   r`   r|   r_   r{   r^   rz   rZ   r   rY   r]   rX   r\   rW   r[   rS   r   rR   rV   rQ   rU   rP   rT   rM   r   rL   r   rK   rO   rJ   rN   r   r   Nc                 C   s^   t |dkr
tdt| |||| d| _g | _g | _dgd gd | _g | _| 	| d S )Nr   z7Invalid DES key size. Key must be exactly 8 bytes long.r   rK   rO   )
r   r   r   r   key_sizeLRKnfinalr"   r   r!   r   r   r   r   r   r   r   r     s   zdes.__init__c                 C   s   t | | |   dS )z;Will set the crypting key for this object. Must be 8 bytes.N)r   r"   _des__create_sub_keysr    r   r   r   r"     s   z
des.setKeyc                 C   s   t dk rdd |D }t|d }dg| }d}|D ]%}d}|dkr?|d|> @ dkr/d||< nd||< |d7 }|d8 }|dks"q|S )z2Turn the string data, into a list of bits (1, 0)'sr4   c                 S      g | ]}t |qS r   )r?   .0cr   r   r   
<listcomp>      z+des.__String_to_BitList.<locals>.<listcomp>r   r   r~   r   )r6   r   )r   r:   lresultposchir   r   r   __String_to_BitList  s    

zdes.__String_to_BitListc                 C   s   g }d}d}|t |k r/||| d|d  > 7 }|d dkr%|| d}|d7 }|t |k stdk r=ddd |D S t|S )	z,Turn the list of bits -> data, into a stringr   r~   r   r   r4    c                 S   r   r   )r7   r   r   r   r   r     r   z+des.__BitList_to_String.<locals>.<listcomp>)r   appendr6   joinr8   )r   r:   r   r   r   r   r   r   __BitList_to_String  s   
zdes.__BitList_to_Stringc                    s   t  fdd|D S )z-Permutate this block with the specified tablec                    s   g | ]} | qS r   r   )r   xblockr   r   r     r   z#des.__permutate.<locals>.<listcomp>)list)r   tabler   r   r   r   __permutate  s   zdes.__permutatec                 C   s   |  tj| |  }d}|dd | _|dd | _|dk rfd}|tj| k rN| j| jd  | jd= | j| jd  | jd= |d7 }|tj| k s)|  tj	| j| j | j
|< |d7 }|dk s dS dS )z6Create the 16 subkeys K[1] to K[16] from the given keyr   Nrv   rO   r   )_des__permutaterI   	_des__pc1_des__String_to_BitListr   r   r   _des__left_rotationsr   	_des__pc2r   )r   r!   r   jr   r   r   __create_sub_keys  s    
zdes.__create_sub_keysc              
   C   sD  |  tj|}|dd | _|dd | _|tjkrd}d}nd}d}d}|dk r| jdd }|  tj| j| _ttdd	 | j| j	| | _| jdd
 | jd
d | jdd | jdd | jdd | jdd | jdd | jdd g}d}dgd }	d}
|dk r|| d d> || d  }|| d d> || d d>  || d d>  || d  }tj
| |d> |  }|d@ d? |	|
< |d@ d? |	|
d < |d@ d? |	|
d < |d@ |	|
d < |
d7 }
|d7 }|dk s|  tj|	| _ttdd	 | j| j| _|| _|d7 }||7 }|dk s*|  tj| j| j | _| jS )z4Crypt the block of data through DES bit-manipulationNrM   r   r   r   r=   rO   c                 S      | |A S Nr   r   yr   r   r   <lambda>      z!des.__des_crypt.<locals>.<lambda>ri   rx   r\   rN   rf   ru   rY   r   rq   r4   r   ry   c                 S   r   r   r   r   r   r   r   r   !  r   )r   rI   _des__ipr   r   ENCRYPT_des__expansion_tabler   mapr   
_des__sbox_des__p_des__fpr   )r   r   
crypt_type	iterationiteration_adjustmentr   tempRBr   Bnr   mnvr   r   r   __des_crypt  sH   

d
<:zdes.__des_cryptc           	      C   s  |sdS t || j dkr<|tjkrtdt| j d |  s,tdt| j d || jt || j  |   7 }|  tkrR| 	 rN| 
| 	 }ntdd}i }g }|t |k r| 
|||d  }|  tkr|tjkr~ttdd	 ||}| ||}|tjkrttd
d	 ||}|}n	|}n| ||}|| | |d7 }|t |k s^tdk rd|S td|S )z8Crypt the data in blocks, running it through des_crypt()r   r   z0Invalid data length, data must be a multiple of z bytes
.z3 bytes
. Try setting the optional padding characterzBFor CBC mode, you must supply the Initial Value (IV) for cipheringr   c                 S   r   r   r   r   r   r   r   r   ^  r   zdes.crypt.<locals>.<lambda>c                 S   r   r   r   r   r   r   r   r   g  r   r4   )r   r
   rI   DECRYPTr   r   r*   r$   CBCr1   r   r   r   r   _des__des_cryptr   _des__BitList_to_Stringr6   r   r8   fromhex)	r   r:   r   ivr   dictr   r   processed_blockr   r   r   crypt4  s@   
 


.
z	des.cryptc                 C   s8   |  |}|dur|  |}| |||}| |tjS )a  encrypt(data, [pad], [padmode]) -> bytes

		data : Bytes to be encrypted
		pad  : Optional argument for encryption padding. Must only be one byte
		padmode : Optional argument for overriding the padding mode.

		The data must be a multiple of 8 bytes and will be encrypted
		with the already specified key. Data does not have to be a
		multiple of 8 bytes if the padding character is supplied, or
		the padmode is set to PAD_PKCS5, as bytes will then added to
		ensure the be padded data is a multiple of 8 bytes.
		N)r	   r<   r   rI   r   r   r:   r   r   r   r   r   encrypt  s
   

zdes.encryptc                 C   s8   |  |}|dur|  |}| |tj}| |||S )a  decrypt(data, [pad], [padmode]) -> bytes

		data : Bytes to be encrypted
		pad  : Optional argument for decryption padding. Must only be one byte
		padmode : Optional argument for overriding the padding mode.

		The data must be a multiple of 8 bytes and will be decrypted
		with the already specified key. In PAD_NORMAL mode, if the
		optional padding character is supplied, then the un-encrypted
		data will have the padding characters removed from the end of
		the bytes. This pad removal only occurs on the last 8 bytes of
		the data (last data block). In PAD_PKCS5 mode, the special
		padding end markers will be removed from the data after decrypting.
		N)r	   r   rI   r   r@   r   r   r   r   decrypt  s
   

zdes.decryptNN)rE   rF   rG   __doc__r   r   r   r   r   r   r   r   r   r   rH   r5   r   r"   r   r   r   r   r   r   r   r   r   r   r   r   rI      s<    4
O
NrI   c                   @   s^   e Zd ZdZeddefddZdd Zdd Zd	d
 Z	dd Z
dd ZdddZdddZdS )
triple_desa  Triple DES encryption/decrytpion class

	This algorithm uses the DES-EDE3 (when a 24 byte key is supplied) or
	the DES-EDE2 (when a 16 byte key is supplied) encryption methods.
	Supports ECB (Electronic Code Book) and CBC (Cypher Block Chaining) modes.

	pyDes.des(key, [mode], [IV])

	key  -> Bytes containing the encryption key, must be either 16 or
	        24 bytes long
	mode -> Optional argument for encryption type, can be either pyDes.ECB
		(Electronic Code Book), pyDes.CBC (Cypher Block Chaining)
	IV   -> Optional Initial Value bytes, must be supplied if using CBC mode.
		Must be 8 bytes in length.
	pad  -> Optional argument, set the pad character (PAD_NORMAL) to use
		during all encrypt/decrpt operations done with this instance.
	padmode -> Optional argument, set the padding mode (PAD_NORMAL or
		PAD_PKCS5) to use during all encrypt/decrpt operations done
		with this instance.
	Nc                 C   s    t | |||| | | d S r   )r   r   r"   r   r   r   r   r     s   ztriple_des.__init__c                 C   s   d| _ t|| j krt|dkrd| _ ntd|  tkr7|  s*|d| j | _t|  | jkr7tdt|dd | j	| j| j
| j| _t|dd | j	| j| j
| j| _| j dkrc| j| _nt|dd | j	| j| j
| j| _t| | dS )zFWill set the crypting key for this object. Either 16 or 24 bytes long.rN   rO   zCInvalid triple DES key size. Key must be either 16 or 24 bytes longNz%Invalid IV, must be 8 bytes in lengthr   )r   r   r   r$   r   r1   r
   r   rI   r   r   r   _triple_des__key1_triple_des__key2_triple_des__key3r   r"   r    r   r   r   r"     s,   

ztriple_des.setKeyc                 C   0   t | | | j| j| jfD ]}|| qdS r&   )r   r(   r   r   r   r   r   r!   r   r   r   r(        ztriple_des.setModec                 C   r   r+   )r   r,   r   r   r   )r   r   r!   r   r   r   r,     r   ztriple_des.setPaddingc                 C   r   r/   )r   r0   r   r   r   r   r   r   r   r0     r   ztriple_des.setPadModec                 C   r   )r2   N)r   r3   r   r   r   )r   r   r!   r   r   r   r3     r   ztriple_des.setIVc           	      C   sJ  t j}t j}| |}|dur| |}| |||}|  tkr| j| 	  | j
| 	  | j| 	  d}g }|t|k r| j|||d  |}| j
||}| j||}| j| | j
| | j| || |d7 }|t|k sCtdk rd|S td|S | j||}| j
||}| j||S )a  encrypt(data, [pad], [padmode]) -> bytes

		data : bytes to be encrypted
		pad  : Optional argument for encryption padding. Must only be one byte
		padmode : Optional argument for overriding the padding mode.

		The data must be a multiple of 8 bytes and will be encrypted
		with the already specified key. Data does not have to be a
		multiple of 8 bytes if the padding character is supplied, or
		the padmode is set to PAD_PKCS5, as bytes will then added to
		ensure the be padded data is a multiple of 8 bytes.
		Nr   r   r4   r   )rI   r   r   r	   r<   r$   r   r   r3   r1   r   r   r   r   r   r6   r   r8   r   )	r   r:   r   r   r   r   r   r   r   r   r   r   r     s8   


	
ztriple_des.encryptc           
      C   sR  t j}t j}| |}|dur| |}|  tkr| j|   | j	|   | j
|   d}g }|t|k rz|||d  }| j
||}	| j	|	|}	| j|	|}	| j| | j	| | j
| ||	 |d7 }|t|k s<tdk rd|}ntd|}n| j
||}| j	||}| j||}| |||S )a  decrypt(data, [pad], [padmode]) -> bytes

		data : bytes to be encrypted
		pad  : Optional argument for decryption padding. Must only be one byte
		padmode : Optional argument for overriding the padding mode.

		The data must be a multiple of 8 bytes and will be decrypted
		with the already specified key. In PAD_NORMAL mode, if the
		optional padding character is supplied, then the un-encrypted
		data will have the padding characters removed from the end of
		the bytes. This pad removal only occurs on the last 8 bytes of
		the data (last data block). In PAD_PKCS5 mode, the special
		padding end markers will be removed from the data after
		decrypting, no pad character is required for PAD_PKCS5.
		Nr   r   r4   r   )rI   r   r   r	   r$   r   r   r3   r1   r   r   r   r   r   r6   r   r8   r   r@   )
r   r:   r   r   r   r   r   r   r   r   r   r   r   r   '  s:   



ztriple_des.decryptr   )rE   rF   rG   r   rH   r5   r   r"   r(   r,   r0   r3   r   r   r   r   r   r   r     s    
,r   )r   sysversion_infor6   rH   r   r5   r   objectr   rI   r   r   r   r   r   <module>   s   <
	    ;