Hi, guys and girls using this post I will show you how to encrypt and decrypt a file using python(no any module).
let's start coding
First, you need to create the 'key' variable. you can use any number for this variable value,
key = 15
now you need to open which file is you need to encrypt(python file handling)
file = open('path to file you need to encrypt','rb')
file_data = file.read()
file.close()
now you need to convert above 'file_data' variable to byte array
byte_array = bytearray(file_data)
now you need to do one by one byte_array item combine with 'key' using bitwise XOR (^)operator (python operators)
for index,value in enumerate(byte_array):
byte_array[index] = value^key
now you need to do write to the new file changed byte array
new_file = open('path to new file','wb')
new_file.write(byte_array)
new_file.close()
now you need to convert above 'file_data' variable to byte array
byte_array = bytearray(file_data)
now you need to do one by one byte_array item combine with 'key' using bitwise XOR (^)operator (python operators)
for index,value in enumerate(byte_array):
byte_array[index] = value^key
now you need to do write to the new file changed byte array
new_file = open('path to new file','wb')
new_file.write(byte_array)
new_file.close()
before encrypt
after encrypt
you decrypt encrypted file reverse this process
Thank you!
Post a Comment