Encoder y Decoder de Base64 - Python3

Iniciado por R.altk#6050, Noviembre 22, 2021, 04:31:14 PM

Tema anterior - Siguiente tema

0 Miembros y 2 Visitantes están viendo este tema.

Noviembre 22, 2021, 04:31:14 PM Ultima modificación: Febrero 21, 2023, 03:22:28 PM por ANTRAX

Hola, buenas a todos. Disculpen mi inactividad.
Recientemente estaba revisando mi equipo en busca de un archivo perdido y de casualidad encontré una antigua tool que cifra y descifra en Base64, la revisé y parecía ser algo decente, sé que no es la mejor del mundo, pero es pasable.
Si tienen algún error o duda no olviden contactarme, les leo!.





Hay muchas páginas en internet que te cifran y descifran en Base64, pero estaría bueno tener un Script al que puedas acceder rápidamente desde tu consola y no hacer mucho lio abriendo el navegador.
La Tool consta de 2 versiones, la de Windows y la de Linux.
¿Por qué es esto?
Recuerdo que la hice así por que en la versión de Windows utilizo la librería llamada "Msvcrt", la cual me facilita la programación del código, este modulo en Linux trae unos problemas que me llevaron a hacer otro código un poco modificado para ello, igual es casi lo mismo, no hay mucha diferencia.

La Tool se vería algo así:


Como pueden ver, la Tool es bastante sencilla e intuitiva.

Help < Para ver los comandos.
1 < Para cifrar
2 < Para descifrar
0 < Para contactarme vía discord.

Aunque si tienen una duda pueden dejármela abajo o mandar mensaje a Discord
Alex. < Stepped Out#9628




Código Windows:

Código: python
#!/usr/bin/env python

import os,base64,sys,colorama,msvcrt
from colorama import Fore,Style,Back

# ! By R.ALTK#6050 < Tool basica

def clear():
    if os.name == "nt":
        os.system("cls")
    else:
        os.system("clear")

clear()

def teca():
    while True:
        print(Style.BRIGHT + Fore.GREEN+"""

 ______                     __   __       _______            _
(____  \                   / /  / /      (_______)          | |
 ____)  ) ____  ___  ____ / /_ / /____    _       ___   ___ | |
|  __  ( / _  |/___)/ _  ) __ \___   _)  | |     / _ \ / _ \| |
| |__)  | ( | |___ ( (/ ( (__) )  | |    | |____| |_| | |_| | |
|______/ \_||_(___/ \____)____/   |_|     \______)___/ \___/|_|"""+Fore.RED+""" V 2.0

    """)
        print(Fore.GREEN+"[ "+Fore.YELLOW+"^ "+Fore.GREEN+"]"+Fore.YELLOW+" Enter Help!\n")

        op1 = input(Fore.CYAN+"#"+Fore.RED+" > "+Fore.GREEN)
        op1 = op1.lower()
        if op1=="1":
            print(Fore.RED+"\n- -\n")
            print(Fore.GREEN+"[ "+Fore.YELLOW+"^ "+Fore.GREEN+"]"+Fore.YELLOW+" Write what you want to encrypt!\n")
            op2 = input(Fore.RED+"#> "+Fore.GREEN)

            dec = base64.b64encode(op2.encode('utf-8'))
            dec2 = dec.decode('ascii')

            if op2=="":
                print(Fore.RED+"\n[ ! ] Error, you have not written something valid.\n")
                print(Fore.CYAN+"Press a key to continue...\n")
                msvcrt.getch()
                clear()
            else:
                print(Fore.GREEN+"\n[ "+Fore.YELLOW+"+ "+Fore.GREEN+"]"+Fore.YELLOW+" Encrypted Result: "+Fore.GREEN+dec2+"\n")
                print(Fore.CYAN+"Press a key to continue...\n")

                msvcrt.getch()
                clear()


        elif op1=="2":

            try:
                print(Fore.RED+"\n- -\n")
                print(Fore.GREEN+"[ "+Fore.YELLOW+"^ "+Fore.GREEN+"]"+Fore.YELLOW+" Write what you want to decipher!\n")
                op3 = input(Fore.RED+"#> "+Fore.GREEN)

                base64_bytes = op3.encode('ascii')
                message_bytes = base64.b64decode(base64_bytes)
                des = message_bytes.decode('ascii')

                if op2=="":
                    print(Fore.RED+"\n[ ! ] Error, you have not written something valid.\n")
                    print(Fore.CYAN+"Press a key to continue...\n")
                    msvcrt.getch()
                    clear()
                else:
                    print(Fore.GREEN+"\n[ "+Fore.YELLOW+"- "+Fore.GREEN+"]"+Fore.YELLOW+" Decrypted Result: "+Fore.GREEN+des+"\n")
                    print(Fore.CYAN+"Press a key to continue...\n")

                    msvcrt.getch()
                    clear()




            except ValueError:
                print(Fore.RED+"\n[ ! ] Error, you have not written something valid.\n")
                print(Fore.CYAN+"Press a key to continue...\n")
                msvcrt.getch()
                clear()
        elif op1=="help":
            print(Fore.RED+"\n-")
            print(Fore.YELLOW+"1. "+Fore.GREEN+" Encode")
            print(Fore.YELLOW+"2. "+Fore.GREEN+" Decode")
            print(Fore.YELLOW+"0. "+Fore.RED+" Contact")
            print(Fore.RED+"-\n")
            print(Fore.CYAN+"Press a key to continue...\n")
            msvcrt.getch()
            clear()
        elif op1=="0":
            print(Fore.RED+"\n-\n")
            print(Fore.GREEN+"[ "+Fore.RED+"80 "+Fore.GREEN+"]"+Fore.YELLOW+" Creator Discord: "+Fore.RED+"Alex. < Stepped Out#9628\n")
            print(Fore.RED+"-\n")
            print(Fore.CYAN+"Press a key to continue...\n")
            msvcrt.getch()
            clear()

        else:
            clear()


def ext():
    try:
        teca()
    except KeyboardInterrupt:
        print("Bye... :)\n")
        sys.exit(0)

ext()

Código Linux:

Código: python
#!/usr/bin/env python

import os,base64,sys,colorama
from colorama import Fore,Style,Back

# ! By R.ALTK#6050 < Tool basica

def clear():
    if os.name == "nt":
        os.system("cls")
    else:
        os.system("clear")

clear()

def teca():
    while True:
        print(Style.BRIGHT + Fore.GREEN+"""

 ______                     __   __       _______            _
(____  \                   / /  / /      (_______)          | |
 ____)  ) ____  ___  ____ / /_ / /____    _       ___   ___ | |
|  __  ( / _  |/___)/ _  ) __ \___   _)  | |     / _ \ / _ \| |
| |__)  | ( | |___ ( (/ ( (__) )  | |    | |____| |_| | |_| | |
|______/ \_||_(___/ \____)____/   |_|     \______)___/ \___/|_|"""+Fore.RED+""" V 2.0

    """)
        print(Fore.GREEN+"[ "+Fore.YELLOW+"^ "+Fore.GREEN+"]"+Fore.YELLOW+" Enter Help!\n")

        op1 = input(Fore.CYAN+"#"+Fore.RED+" > "+Fore.GREEN)
        op1 = op1.lower()
        if op1=="1":
            print(Fore.RED+"\n- -\n")
            print(Fore.GREEN+"[ "+Fore.YELLOW+"^ "+Fore.GREEN+"]"+Fore.YELLOW+" Write what you want to encrypt!\n")
            op2 = input(Fore.RED+"#> "+Fore.GREEN)

            dec = base64.b64encode(op2.encode('utf-8'))
            dec2 = dec.decode('ascii')

            if op2=="":
                print(Fore.RED+"\n[ ! ] Error, you have not written something valid.\n")
                print(Fore.CYAN+"Press a key to continue...\n")
                input(Fore.BLACK)
                clear()
            else:
                print(Fore.GREEN+"\n[ "+Fore.YELLOW+"+ "+Fore.GREEN+"]"+Fore.YELLOW+" Encrypted Result: "+Fore.GREEN+dec2+"\n")
                print(Fore.CYAN+"Press a key to continue...\n")

                input(Fore.BLACK)
                clear()


        elif op1=="2":

            try:
                print(Fore.RED+"\n- -\n")
                print(Fore.GREEN+"[ "+Fore.YELLOW+"^ "+Fore.GREEN+"]"+Fore.YELLOW+" Write what you want to decipher!\n")
                op3 = input(Fore.RED+"#> "+Fore.GREEN)

                base64_bytes = op3.encode('ascii')
                message_bytes = base64.b64decode(base64_bytes)
                des = message_bytes.decode('ascii')

                if op2=="":
                    print(Fore.RED+"\n[ ! ] Error, you have not written something valid.\n")
                    print(Fore.CYAN+"Press a key to continue...\n")
                    input(Fore.BLACK)
                    clear()
                else:
                    print(Fore.GREEN+"\n[ "+Fore.YELLOW+"- "+Fore.GREEN+"]"+Fore.YELLOW+" Decrypted Result: "+Fore.GREEN+des+"\n")
                    print(Fore.CYAN+"Press a key to continue...\n")

                    input(Fore.BLACK)
                    clear()




            except ValueError:
                print(Fore.RED+"\n[ ! ] Error, you have not written something valid.\n")
                print(Fore.CYAN+"Press a key to continue...\n")
                input(Fore.BLACK)
                clear()
        elif op1=="help":
            print(Fore.RED+"\n-")
            print(Fore.YELLOW+"1. "+Fore.GREEN+" Encode")
            print(Fore.YELLOW+"2. "+Fore.GREEN+" Decode")
            print(Fore.YELLOW+"0. "+Fore.RED+" Contact")
            print(Fore.RED+"-\n")
            print(Fore.CYAN+"Press a key to continue...\n")
            input()
            clear()
        elif op1=="0":
            print(Fore.RED+"\n-\n")
            print(Fore.GREEN+"[ "+Fore.RED+"80 "+Fore.GREEN+"]"+Fore.YELLOW+" Creator Discord: "+Fore.RED+"Alex. < Stepped Out#9628\n")
            print(Fore.RED+"-\n")
            print(Fore.CYAN+"Press a key to continue...\n")

            
            input(Fore.BLACK)
            clear()

        else:
            clear()


def ext():
    try:
        teca()
    except KeyboardInterrupt:
        print("Bye... :)\n")
        sys.exit(0)

ext()



Me encantó!! Muy bueno che! Muchas gracias por compartirlo!
Muy limpio el código!