Brute force
In cryptography, a brute-force attack means substituting all possible values to crack a particular cipher. Most encryption methods are theoretically insecure against brute-force attacks, and encrypted information can be decrypted if there is enough time.
In the case of symmetric key encryption (a type of encryption algorithm, which means an algorithm that uses the same encryption key for encryption and decryption), the encrypted information can be restored by finding the key used in the encryption, and the length of the key used here The maximum time required for a brute force attack is determined according to. When the encryption key is n bits, there are a maximum of 2^n possible values.
Breaking symmetric key encryption with brute force
For example, we could use brute force to solve the following problem.
# You are a hacker. You have data that is encrypted by stealing the transmitted data, and you know that this data is in plain text.
# You need to find out the original data and the secret key (8bits).
data = '6e5253491a5e5b4e5b1a53491a5b5649551a4c5f48431a53574a55484e5b544e1a4e5f424e1a5e5b4e5b141a6a565f5b495f1a4e5b515f1a595b485f1a4d525f541a525b545e5653545d14'
If you are implementing symmetric key encryption in Python, you can implement it like this:
import binascii
def encoder(data:str, key:str) -> bytes:
encoded_data = []
## xor
for ch in data:
result = ord(ch) ^ int(key, 2)
encoded_data.append(result)
## Convert binary data to hexadecimal representation
encoded_data_hex = binascii.hexlify(bytes(encoded_data))
return encoded_data_hex
def decoder(encoded_data:bytes, key:str) -> str:
decoded_data = ''
## Converted hexadecimal string to binary data
encoded_data_byte = binascii.unhexlify(encoded_data)
## xor
for byte in encoded_data_byte:
decoded_data += chr(byte ^ int(key, 2))
return decoded_data
# We use 8bits key
key = '11001010'
# Suppose the data is long text
data = 'This data is very important text data. Take care when handling.'
# Encrypt data to be transmitted.
encoded = encoder(data, key)
# Encrypted data
print(encoded)
# A secret key is required for decryption.
print(decoder(encoded,key))
Result:
b'9ea2a3b9eaaeabbeabeaa3b9eabcafb8b3eaa3a7baa5b8beaba4beeabeafb2beeaaeabbeabe4ea9eaba1afeaa9abb8afeabda2afa4eaa2aba4aea6a3a4ade4'
This data is very important text data. Take care when handling.
More detailed code can be found through the following link.
https://github.com/hotchya/basic-algorithm/tree/main/symmetric%20key%20encryption
References
'Algorithm' 카테고리의 다른 글
a^3 + b^3 = c^3 + d^3 (0) | 2021.11.02 |
---|---|
Data structure - BFS, DFS (0) | 2021.09.12 |
Data structure - Heap (0) | 2021.09.12 |