python - xor each byte with 0x71 -
i needed read byte file, xor 0x71 , write file. however, when use following, reads byte string, xoring creates problems.
f = open('a.out', 'r') f.read(1)
so ended doing same in c.
#include <stdio.h> int main() { char buffer[1] = {0}; file *fp = fopen("blah", "rb"); file *gp = fopen("a.out", "wb"); if(fp==null) printf("error opening file\n"); int rc; while((rc = fgetc(fp))!=eof) { printf("%x", rc ^ 0x71); fputc(rc ^ 0x71, gp); } return 0; }
could tell me how convert string on using f.read()
on hex value xor 0x71 , subsequently write on file?
if want treat array of bytes, want bytearray
behaves mutable array of bytes:
b = bytearray(open('a.out', 'rb').read()) in range(len(b)): b[i] ^= 0x71 open('b.out', 'wb').write(b)
indexing byte array returns integer between 0x00 , 0xff, , modifying in place avoid need create list , join again. note file opened binary ('rb') - in example use 'r' isn't idea.
Comments
Post a Comment