Cryptography: converting C function to python
<p>I am trying to solve a cryptography problem (<a href="https://callicode.fr/pydefis/Reverse/txt" rel="nofollow">https://callicode.fr/pydefis/Reverse/txt</a>), the alghorithm make use of the following C function (F1). 
I don't know C, I tried to convert it to python(F2) without success. Thank you in advance for letting me know what I did wrong.</p>

<p><strong>F1:</strong></p>

<pre><code>void Encrypt(unsigned char key[20], unsigned char *pPlainBuffer, unsigned char *pCipherBuffer, unsigned int nLength) 
{
int nKeyPos = 0;
unsigned int n;
unsigned char KeyA = 0;

if ((pPlainBuffer != NULL) &amp;&amp; (pCipherBuffer != NULL)) {
for (n = 0; n &lt; 20; n++) 
  KeyA ^= key[n];
  nKeyPos = KeyA % 20;
for (n = 0; n &lt; nLength; n++) {
  pCipherBuffer[n] = pPlainBuffer[n]^(key[nKeyPos]*KeyA);
  KeyA += pCipherBuffer[n];
  nKeyPos = pCipherBuffer[n] % 20;
}
 }
}
</code></pre>

<p><strong>F2:</strong></p>

<pre><code>def Encrypt(plainText, key):  # plainText is a list of hexadecimals representing ascii 
                              # characters, key is a list of int size 20  each int 
                              # beetween 0 and 255
    keyA = 0

    for n in range(20):
        keyA ^= key[n]

    nKeyPos = KeyA % 20

    for n in range(len(plainText)):
         codedText[n] = plainText[n]^(key[nKeyPos]*KeyA)
         keyA += codedText[n]
         nKeyPos = codedText[n] % 20
</code></pre>

<p>you have many issues... the most glaring being that python integers are not constrained to a byte width by default so you must explicitly set the width</p>

<p>additionally you must convert the letters to numbers in python as they are fundamentally different things (in C/c++ letters are really just numbers)</p>

<pre><code>def Encrypt(plainText, key):  # plainText is a list of hexadecimals representing ascii 
                              # characters, key is a list of int size 20  each int                                   # beetween 0 and 255
    keyA = 0    
    for letter in key:
        keyA ^= ord(letter) # conver letter to number    
    keyA = keyA &amp; 0xFF # fix width to one byte
    key = itertools.cycle(key) # repeate forever
    codedText = ""
    for letter,keyCode in zip(plainText,key):
         xorVal = (ord(keyCode) * keyA) &amp; 0xFF # make one byte wide
         newLetter = chr((ord(letter) ^ xorVal )&amp;0xFF) # make one byte wide

         codedText += newLetter
         keyA += ord(newLetter)
         keyA = keyA&amp;0xFF # keep it one byte wide
</code></pre>

python c cryptography