#๐Ÿ”’ How can I make input() take bytes for .decode() to work?

56 messages ยท Page 1 of 1 (latest)

fierce crow
#

Really need help with an encryption/decryption program

orchid bayBOT
#

@fierce crow

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

fierce crow
#
import base64

a = int(input("Enter how many times you want your code encrypted/decrypted: "))
type = input("Encryption ('a') or decryption ('b'): ")
random_code = input("Enter code: ")

def encode(a):

    global random_code
    global d

    for i in range(0,a):
        enc = random_code.encode(encoding="utf-32-le")
        enc1 = base64.a85encode(enc)
        enc2 = base64.b16encode(enc1)
        enc3 = base64.b32encode(enc2)
        d = base64.b64encode(enc3)

def decode(a):

    global random_code
    global d

    for i in range(0,a):
        dec = random_code.decode(encoding="utf-32-le")
        dec1 = base64.a85decode(dec)
        dec2 = base64.b16decode(dec1)
        dec3 = base64.b32encode(dec2)
        d = base64.b64decode(dec3)

if type == "a":
    encode(a)

elif type == "b":
    decode(a)

print(d)
#

this is my entire program

#

it gets mad because it thinks im inputting a str into random_code.decode(encoding="utf-32-le")

#

which im not

#

is there anything like str(input()) but for bytes?

untold spoke
fierce crow
#

would that return the original string from encode(a) though?

#

so like

untold spoke
#

I meant the str.encode() method

fierce crow
#

so

#

random_code.encode()?

untold spoke
#

!e

print("hello ะถะถะถ world".encode("utf-8"))
orchid bayBOT
fierce crow
#

works

#

now my decodes don't wanna work

#

RIP

wet charm
#

this is a case where type hints might help

#

they'd help you remember if your functions take and return strings or bytes

fierce crow
#

ah

#

it takes a string and turns it into bytes

#

hm

#

all of them do

wet charm
#

that's fine, as long as you're consistent

untold spoke
fierce crow
#

b

#

im really new to python (less than 1 month) so im sorry if thats the wrong answer

#

wait a would make more sense

#

because a85 went last before so it should go first

untold spoke
#

exactly

fierce crow
#

non-base16 digit found

#

after i reversed the locations

fierce crow
wet charm
#

if you'd like, I can show you how I'd do it.

fierce crow
#

sure im trying to learn

wet charm
#

I think they key thing is: when you decrypt, you need to call various decode functions, and in the reverse order.

fierce crow
#

woah

#

thats really hard to understand for me

#

what does enc = f(enc) mean?

wet charm
#

it means "call the function f, passing it enc, then assign the result to enc"

fierce crow
#

ooh

#

so f is like

#

the function

#

?

#

doesnt work though

#

UnicodeDecodeError

wet charm
#

works for me. Are you running exactly the code I pasted?

fierce crow
#

i think so yeah

#

try doing a double encryption with something like "abcdefghijklmnopqrstuvwxyz"

wet charm
#

I don't know what you mean by "doing a double encryption", but I see what you mean

#

This is better. It doesn't use the windows encoding

#
(echo 3; echo b; echo R05C...OU0Q=) | python3 gerry.py 
Enter how many times you want your code encrypted/decrypted: Encryption ('a') or decryption ('b'): Enter code: abcdefghijklmnopqrstuvwxyz
orchid bayBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.