1. Conversion hexadécimal vers base64

1. Conversion hexadécimal vers base64

Convert hex to base64

The string:
49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d
Should produce:
SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29t
So go ahead and make that happen. You'll need to use this code for the rest of the exercises. Cryptopals Rule
Always operate on raw bytes, never on encoded strings. Only use hex and base64 for pretty-printing.

Le but de ce premier exercice est de convertir une chaîne de caractère hexadécimal en une chaine de bytes, puis en une chaîne en base 64.

Une solution en Python peut être la suivante:

import codecs

def hex_to_base64(hex_input):
    bytes_string = codecs.decode(hex_input, "hex")
    return codecs.encode(bytes_string,"base64").decode()

hex_string = "49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d"
print(hex_to_base64(hex_string))

A noter que le résultat retourné par la fonction hex_to_base64 a besoin d'être transformé depuis bytes string vers string via la méthode decode().

En exécutant ce script, le résultat est le suivant:

$ python hex_to_base64.py
SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29t