DEV Community

Cover image for ๐Ÿ” BF-VecCrypt: A Brainfuck-Based Encryption System
hmza
hmza

Posted on

๐Ÿ” BF-VecCrypt: A Brainfuck-Based Encryption System

๐Ÿ” BF-VecCrypt: A Brainfuck-Based Encryption System

๐Ÿง  Abstract

This paper presents BF-VecCrypt, a lightweight and obfuscated encryption method built upon the esoteric Brainfuck programming language. The encryption mechanism generates Brainfuck-compatible code to represent plaintext characters, using a shifting key vector (k_vec) and a starting position (s_pos). This approach simulates a virtual pointer and memory manipulation process to encode a message into a Brainfuck-like script.


๐Ÿ”‘ Key Concepts

๐Ÿงฉ Key Vector (k_vec)

A list of ordered symbols used as reference points for generating memory pointer shifts and values. For example:

k_vec = ["a", "b", "c", ..., "z", "[", "]", "<", ">", "+", "-", ".", ",", "0", ..., "9"]

๐Ÿ“ Starting Position (s_pos)

The index in k_vec where the encryption process begins. This defines the initial value at the memory pointer. For example, s_pos = 2 means starting at "c".


๐Ÿงฌ Encryption Process

Given:

  • message = "this is a secret message"
  • k_vec = ["a", "b", "c", ..., "z"]
  • s_pos = 1 (i.e., "b")

The encryption algorithm works by:

  1. Initializing the pointer at the s_pos.
  2. For each character ch in the message:
    • Determine its index in k_vec: target_pos.
    • Calculate the difference from the current position: delta = target_pos - current_pos.
    • Output > or < abs(delta) times to move the pointer.
    • Output + ord(ch) times to increase the cell's value.
    • Output . to emit the character.
    • Update current_pos = target_pos.

๐Ÿงฎ Example

Letโ€™s say:


k_vec = ["a", "b", "c", ..., "z"]<br>
s_pos = 1   โ†’ current char = "b"<br>
message = "d"

Enter fullscreen mode Exit fullscreen mode
  • "d" is at index 3
  • From "b" (index 1) โ†’ to "d" (index 3) = delta 2
  • BF steps:
    • >> (move pointer 2 steps to the right)
    • +++ (increase cell value 3 times to simulate 'd' if cell is empty)
    • . (output the character)

So encrypted form = >>+++.

Repeat this per character in the message. This generates a valid (though mostly non-functional) Brainfuck-like script as a cryptic ciphertext.


๐Ÿ”“ Decryption

The recipient:

  • Loads the k_vec and s_pos.
  • Parses the BF-like code.
  • Simulates pointer moves and increments to extract each intended character from memory.
  • Uses the emitted ASCII values (from +, -, and . patterns) to reconstruct the plaintext.

๐Ÿ›ก๏ธ Security Notes

  • Obfuscation: The ciphertext is essentially a scrambled Brainfuck script, making it unreadable to humans.
  • Key secrecy: Security relies on keeping k_vec and s_pos secret.
  • Not cryptographically strong: This is a fun/educational encryption toy, not secure for real-world applications.

๐Ÿ’ก Use Cases

  • Educational purposes (understanding interpreters & encoding).
  • Creating esoteric puzzle messages.
  • Obfuscation in code golfing or creative art/code pieces.

๐Ÿ“Œ Conclusion

BF-VecCrypt shows how the minimalistic nature of Brainfuck can inspire creative encryption schemes. While not suitable for secure communication, it provides a fascinating blend of pointer arithmetic and symbolic encoding โ€” a digital maze that turns ordinary text into a chaotic dance of >+<-.

๐Ÿงช Example Usage of BF-VecCrypt


โœ‰๏ธ Original Message

message = "bad"


๐Ÿง  Brainfuck Compiler in Python

bf_encrypt.py


k_vec = [chr(i) for i in range(97, 123)]  # 'a' to 'z'<br>
s_pos = 1  # Starts at 'b'<br>
<br>
def encrypt_bf(message, k_vec, s_pos):<br>
    bf_code = ""<br>
    current_pos = s_pos<br>
    for ch in message:<br>
        target_pos = k_vec.index(ch)<br>
        delta = target_pos - current_pos<br>
        if delta > 0:<br>
            bf_code += ">" * delta<br>
        elif delta < 0:<br>
            bf_code += "<" * (-delta)<br>
        bf_code += "+" * (ord(ch) - ord(k_vec[target_pos]))<br>
        bf_code += "."<br>
        current_pos = target_pos<br>
    return bf_code<br>
<br>
# Example usage<br>
message = "bad"<br>
bf_code = encrypt_bf(message, k_vec, s_pos)<br>
print("๐Ÿ” Encrypted BF Code:")<br>
print(bf_code)

Enter fullscreen mode Exit fullscreen mode

๐Ÿงพ Example Encrypted Output


Encrypted BF Code:<br>
<<<.>>>>>>>>>>>>.<<<.

Enter fullscreen mode Exit fullscreen mode

๐Ÿ” Brainfuck Reverse Compiler (Decryption)

bf_decrypt.py


def decrypt_bf(bf_code, k_vec, s_pos):<br>
    pointer = s_pos<br>
    output = ""<br>
    for cmd in bf_code:<br>
        if cmd == ">":<br>
            pointer += 1<br>
        elif cmd == "<":<br>
            pointer -= 1<br>
        elif cmd == ".":<br>
            output += k_vec[pointer]<br>
    return output<br>
<br>
# Example usage<br>
code = "<<<.>>>>>>>>>>>>.<<<."<br>
k_vec = [chr(i) for i in range(97, 123)]<br>
s_pos = 1<br>
print("๐Ÿ”“ Decrypted message:")<br>
print(decrypt_bf(code, k_vec, s_pos))

Enter fullscreen mode Exit fullscreen mode

โœ… Output of Reverse Compilation


Decrypted message:<br>
bad

Enter fullscreen mode Exit fullscreen mode

๐Ÿ’ก Notes

  • This assumes k_vec is 1-to-1 mapped to lowercase letters a-z.
  • It can be extended to include more characters (numbers, symbols, etc).
  • The encryption here does not encode ASCII values via +, but rather symbolically tracks the index position โ€” a simplified obfuscated form, perfect for BF-themed encryption puzzles.

Let me know if you want:

  • The + and - to encode true ASCII values
  • Full memory cell simulation
  • Or a .py or .md export version.

Top comments (1)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.