Muddyblack k
Muddyblack k

Reputation: 254

Python- encode base64 print new line

I want to try to encrypt my python code with base64. But when I use \n I get an error:

print("hello 
IndentationError: unexpected indent

This is my code I used:

import base64

def encode(data):
    try:
        # Standard Base64 Encoding
        encodedBytes = base64.b64encode(data.encode("utf-8"))
        return str(encodedBytes, "utf-8")
    except:
        return ""
    
def decode(data):
    try:
        message_bytes = base64.b64decode(data)
        return message_bytes.decode('utf-8')
    except:
        return ""

your_code = encode("""
    print("hello \n world")

""")

exec(decode(your_code))

I could use twice the print() function instead of \n but is there a way to use \n?

I hope you can help me out

Upvotes: 0

Views: 437

Answers (1)

Emil105
Emil105

Reputation: 158

First, you have to remove the indent in the your_code section. Second you have to replace \n with \\n

import base64


def encode(data):
    try:
        # Standard Base64 Encoding
        encodedBytes = base64.b64encode(data.encode("utf-8"))
        return str(encodedBytes, "utf-8")
    except:
        return ""


def decode(data):
    try:
        message_bytes = base64.b64decode(data)
        return message_bytes.decode('utf-8')
    except:
        return ""


your_code = encode("""
print("hello \\n world")

""")

exec(decode(your_code))

Upvotes: 2

Related Questions