2016-11-30 78 views
1

我更新我的問題,因爲起初我認爲我有最好的解決方案但是,直到現在我還沒有。這是我在執行過程中的錯誤。我認爲錯誤來自於文件C中的循環。我試圖從文本文件「Plaintext.txt」中讀取行。如何迭代python中的文本文件中的行?

e0370734313198a2885a308d3243f6a8 
ccddeeff8899aabb4455667700112233 
8e73b0f7da0e6452c810f32bc4567a22 

現在,它包含拖行,我把只有兩個,爲了使簡單的測試,但我必須把更多的則1000名的文本(指超過1000線),我想讀的每一行,然後將其發送到UART,我會爲每一個明文做加密(加密算法是在C):這是我的腳本:

,你告訴我,但我仍然有一條線的加密我編輯

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 

在C文件中:

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 

所以問題是,它只需在最後一行,並重復所有的時間線相同的加密:

$************************** 
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 
.................... 

我會很感激,如果你能幫助我。

回答

0

的問題是在python文件空間:for循環必須包含

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

通過這種方式,它不會拿剛剛過去的明文來自明文文件。

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 

我的問題已解決。非常感謝您的幫助。

2

您可能需要做如下:

f = open("your_file", "r") 
for line in f: 
    do_something(line) 
f.close() 

或評論指出:

with open("your_file", "r") as f: 
    for line in f: 
     do_something(line) 

Python將每一行遍歷這裏給線變線串。你可以用這種方式處理文件中的每一行。另外,這樣做,python每次讀取一行,所以對於較大的文件是有效的。

+1

我個人使用'open(「your_file」,「r」)作爲f:for line in f:do_something(line)'。這樣可以避免在例外情況下無法關閉文件。 –

+0

@DaanTimmer我同意。我個人不經常使用「with」關鍵詞,但這是一個更好的做法。 – Musen

+0

我很抱歉,它只有一次,然後我回來加密一行 – nani92

0

在你的for循環

for a in range (0,2): 

    line_array=lines[a] 
    plaintxt_16b=line_array[0:32] 

    plaintext=binascii.unhexlify(plaintxt_16b) 
    clear_msg=b'\x24'+b'\x73'+b'\x10'+plaintext 

您覆蓋在每個迭代變量clear_msg。 一旦你離開循環它包含最後一個值。在你的情況下,最後一行(加密)。然後您幾次發送變量clear_msg而不更改其內容。 您需要縮進以下塊,以及:

print(clear_msg) 
ser.write(clear_msg) 
time.sleep(0.4) 
+0

刪除了評論,誤讀'readline' readlines' –

+0

No. readlines()返回所有行的列表。 – jonie83

+0

是的,我知道;-)只是認爲它說readline ;-)而不是readline_s_ –

0

this,調用readlines方法()使你的代碼速度較慢,更明確,不夠簡明,對於絕對沒有好處。

一個好的方法來打開一個文件,而不需要關閉它是通過使用while

with open(file) as f: 
    line = f.read().splitlines() 

line是包含文件的所有行的列表。您可以迭代,並得到任何條目,你會在列表中做

+0

它給我錯誤的分界線,它就像它不能讀取任何線 – nani92

+0

什麼樣的錯誤。分裂線是爲了確保你不會得到'\ n'字符。 –

+0

對不起,這是我的一個語法錯誤,但它仍然只給我一行。 – nani92

相關問題