Search code examples
pythoncuart

How to iterate lines from a text file in python?


I update my question because At first I think that I have the best solution However, I don't have it until now.It was my mistake in execution. I think that the error comes from the loop while in the file C.

I am trying to read lines from a text file"Plaintext.txt".

e0370734313198a2885a308d3243f6a8
ccddeeff8899aabb4455667700112233
8e73b0f7da0e6452c810f32bc4567a22

It contains now tow lines, I put just two in order to make simple test, but I must put more then 1000 texts (means more than 1000 lines) I want to read each line then send it to the uart where I will do encryption for every plaintext (The encryption algorithm is in C): This is my script:

I edit it as you tell me but I still have the encryption of one line

    import string
import serial
import time
from array import array
import struct
import binascii

ser = serial.Serial(
                    port='COM4',\
                    baudrate=230400,\
                    parity=serial.PARITY_NONE,\
                    stopbits=serial.STOPBITS_ONE,\
                    bytesize=serial.EIGHTBITS,\
                    timeout=0)  

f = open(r'C:\\Users\\user\\Plaintxt.txt', 'r')
for a in f:
   plaintxt_16b=a[0:32]
   plaintext=binascii.unhexlify(plaintxt_16b)
   clear_msg=b'\x24'+b'\x73'+b'\x10'+plaintext

ser.write(clear_msg)
time.sleep(0.4)

while True: 
  print(ser.read(70))
ser.close()                # close ports

In the C file:

   while(1)
    {
        int rx_length = dev_uart_ptr->uart_read((void*)rx_buffer, 19);

        if (rx_length <19)
        {

            if (rx_buffer[0]=='\x24')
            {
                if (rx_buffer[1]=='\x73')
                {
                    if (rx_buffer[2]=='\x10')
                    {
                        plaintext[0] = (rx_buffer[3] << 24)  |
                                (rx_buffer[4] << 16)  |
                                (rx_buffer[5] << 8)   |
                                rx_buffer[6];
                        plaintext[1] = (rx_buffer[7] << 24)  |
                                (rx_buffer[8] << 16)  |
                                (rx_buffer[9] << 8)   |
                                rx_buffer[10];
                        plaintext[2] = (rx_buffer[11] << 24) |
                                (rx_buffer[12] << 16) |
                                (rx_buffer[13] << 8)  |
                                rx_buffer[14];
                        plaintext[3] = (rx_buffer[15] << 24) |
                                (rx_buffer[16] << 16) |
                                (rx_buffer[17] << 8)  |
                                rx_buffer[18];

                        xprintf("**************************\n");
                        xprintf("%8x %8x %8x %8x \n",plaintext[0],plaintext[1],plaintext[2],plaintext[3]);
                        aes2_set_msg((unsigned int *)plaintext);    /** Reset AES message buffer */
                        aes2_set_key128((unsigned int *)key128);    /** Put the key 128 into AES */
                        /** Configure  AES register  to enable IRQ and ENCODE */
                        regs_aes2_ptr-> CFG =  AES2_CFG_ENC_DEC_BIT | AES2_CFG_IRQ_MASK_BIT;
                        /** Reset AES internaly */
                        regs_aes2_ptr-> CTRL = AES2_CTRL_SWRESET_BIT;

#if DEBUG
                        xprintf("Go encrypt..\n");
#endif
                        /** Start the ENCODE function */
                        regs_aes2_ptr-> CTRL = AES2_CTRL_START_BIT;

                        while(!aes2_irq_flag); /** Wait for irq flag */
                        aes2_irq_flag=0;    /** Reset irq flag */

#if DEBUG
                        xprintf("Encrypt done..\n");
#endif

                        aes2_get_msg((unsigned int *)ciphertext);   /** Retrieve encrypted message */
                        xprintf("%8x %8x %8x %8x \n",ciphertext[0],ciphertext[1],ciphertext[2],ciphertext[3]);
                        xprintf("**************************\n");

                    }
                    else
                    {
                        printf ("false");
                    }

                }
                else
                {
                    printf ("false");
                }
            }

        }

    }// End While

}//end of C_Entry

So the problem is that It takes just the last line and repeat all the time the same encryption of that line:

    $**************************
ccddeeff 8899aabb 44556677   112233
Go encrypt..
Encrypt do
ne..
d6e4d64b 27d8d055 c5c7573a 8df4e9aa
**************************
******************
********
ccddeeff 8899aabb 44556677   112233
Go encrypt..
Encrypt done..
d6e4d64b 27d
8d055 c5c7573a 8df4e9aa
**************************
**************************
ccddeeff
 8899aabb 44556677   112233
Go encrypt..
Encrypt done..
d6e4d64b 27d8d055 c5c7573a 8df
4e9aa
**************************
**************************
ccddeeff 8899aabb 44556677
   112233
Go encrypt..
Encrypt done..
d6e4d64b 27d8d055 c5c7573a 8df4e9aa
**********
****************
**************************
ccddeeff 8899aabb 44556677   112233
Go enc
rypt..
Encrypt done..
d6e4d64b 27d8d055 c5c7573a 8df4e9aa
....................

I would be very grateful if you could help me.


Solution

  • The problem was spaces in python file: the for loop must contains

           ser.write(clear_msg)
           time.sleep(0.4)
           print(ser.read(70))
    

    By that way, it will not take just the last plaintext from the plaintext file.

    import string
    import serial
    import time
    from array import array
    import struct
    import binascii
    
    ser = serial.Serial(
                        port='COM4',\
                        baudrate=230400,\
                        parity=serial.PARITY_NONE,\
                        stopbits=serial.STOPBITS_ONE,\
                        bytesize=serial.EIGHTBITS,\
                        timeout=0)  
    
    f = open(r'C:\\Users\\user\\Plaintxt.txt', 'r')
    for a in f:
       plaintxt_16b=a[0:32]
       plaintext=binascii.unhexlify(plaintxt_16b)
       clear_msg=b'\x24'+b'\x73'+b'\x10'+plaintext
       ser.write(clear_msg)
       time.sleep(0.4)
       print(ser.read(70))
    ser.close()                # close ports
    

    My problem is resolved. Thank you very much for your help.