Elysia Tools

Explora herramientas, ejemplos, explicaciones visuales y guías temáticas en un solo espacio claro.

Categorías populares

Temas

Idioma
ZHESPTRUFRDEEN

Elysia Tools

Navegación

C++

Texto Base64 C++: sustituciones, pruebas permisivas y espacios | Elysia Tools

Codificación/Decodificación Base64. Resuelva tres ejemplos Windows Cryptography C++ Samples acotados con entradas explícitas y resultados comprobables.

Ejemplos

Entradas de esta colección

CódigocppIntermedio

Cálculo de Hash

Calcular valores de hash MD5, SHA1, SHA256 y SHA512 para cadenas y archivos usando Windows Cryptography API

Dificultad

6/10

Tiempo estimado

25 min

Etiquetas

cpp, cryptography, hash, windows

Requisitos previos

Intermediate C++, Windows Cryptography API

cpp
// Windows C++ Hash Calculation Examples
// Using Windows Cryptography API (CryptoAPI/CNG)

#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <windows.h>
#include <wincrypt.h>

#pragma comment(lib, "crypt32.lib")

// 1. MD5 Hash Calculation
class MD5Hash {
public:
    static std::string calculate(const std::string& input) {
        HCRYPTPROV hProv = 0;
        HCRYPTHASH hHash = 0;

        // Get handle to crypto provider
        if (!CryptAcquireContext(&hProv, nullptr, nullptr, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) {
            std::cerr << "CryptAcquireContext failed" << std::endl;
            return "";
        }

        // Create hash object
        if (!CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash)) {
            CryptReleaseContext(hProv, 0);
            std::cerr << "CryptCreateHash failed" << std::endl;
            return "";
        }

        // Hash data
        if (!CryptHashData(hHash, reinterpret_cast<const BYTE*>(input.c_str()), input.size(), 0)) {
            CryptDestroyHash(hHash);
            CryptReleaseContext(hProv, 0);
            std::cerr << "CryptHashData failed" << std::endl;
            return "";
        }

        // Get hash value
        DWORD hashLen = 16; // MD5 is 16 bytes
        std::vector<BYTE> hash(hashLen);

        if (!CryptGetHashParam(hHash, HP_HASHVAL, hash.data(), &hashLen, 0)) {
            CryptDestroyHash(hHash);
            CryptReleaseContext(hProv, 0);
            std::cerr << "CryptGetHashParam failed" << std::endl;
            return "";
        }

        // Clean up
        CryptDestroyHash(hHash);
        CryptReleaseContext(hProv, 0);

        // Convert to hexadecimal string
        return bytesToHex(hash);
    }

    static std::string calculateFile(const std::string& filename) {
        std::ifstream file(filename, std::ios::binary);
        if (!file) {
            std::cerr << "Failed to open file: " << filename << std::endl;
            return "";
        }

        HCRYPTPROV hProv = 0;
        HCRYPTHASH hHash = 0;

        if (!CryptAcquireContext(&hProv, nullptr, nullptr, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) {
            return "";
        }

        if (!CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash)) {
            CryptReleaseContext(hProv, 0);
            return "";
        }

        // Hash file in chunks
        const int BUFFER_SIZE = 4096;
        std::vector<BYTE> buffer(BUFFER_SIZE);

        while (file) {
            file.read(reinterpret_cast<char*>(buffer.data()), BUFFER_SIZE);
            std::streamsize bytesRead = file.gcount();

            if (bytesRead > 0) {
                if (!CryptHashData(hHash, buffer.data(), static_cast<DWORD>(bytesRead), 0)) {
                    CryptDestroyHash(hHash);
                    CryptReleaseContext(hProv, 0);
                    return "";
                }
            }
        }

        DWORD hashLen = 16;
        std::vector<BYTE> hash(hashLen);

        if (!CryptGetHashParam(hHash, HP_HASHVAL, hash.data(), &hashLen, 0)) {
            CryptDestroyHash(hHash);
            CryptReleaseContext(hProv, 0);
            return "";
        }

        CryptDestroyHash(hHash);
        CryptReleaseContext(hProv, 0);

        return bytesToHex(hash);
    }

private:
    static std::string bytesToHex(const std::vector<BYTE>& bytes) {
        std::string hex;
        hex.reserve(bytes.size() * 2);

        for (BYTE b : bytes) {
            char buf[3];
            sprintf_s(buf, "%02x", b);
            hex += buf;
        }

        return hex;
    }
};

// 2. SHA1 Hash Calculation
class SHA1Hash {
public:
    static std::string calculate(const std::string& input) {
        HCRYPTPROV hProv = 0;
        HCRYPTHASH hHash = 0;

        if (!CryptAcquireContext(&hProv, nullptr, nullptr, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) {
            return "";
        }

        if (!CryptCreateHash(hProv, CALG_SHA1, 0, 0, &hHash)) {
            CryptReleaseContext(hProv, 0);
            return "";
        }

        if (!CryptHashData(hHash, reinterpret_cast<const BYTE*>(input.c_str()), input.size(), 0)) {
            CryptDestroyHash(hHash);
            CryptReleaseContext(hProv, 0);
            return "";
        }

        DWORD hashLen = 20; // SHA1 is 20 bytes
        std::vector<BYTE> hash(hashLen);

        if (!CryptGetHashParam(hHash, HP_HASHVAL, hash.data(), &hashLen, 0)) {
            CryptDestroyHash(hHash);
            CryptReleaseContext(hProv, 0);
            return "";
        }

        CryptDestroyHash(hHash);
        CryptReleaseContext(hProv, 0);

        return bytesToHex(hash);
    }

private:
    static std::string bytesToHex(const std::vector<BYTE>& bytes) {
        std::string hex;
        hex.reserve(bytes.size() * 2);

        for (BYTE b : bytes) {
            char buf[3];
            sprintf_s(buf, "%02x", b);
            hex += buf;
        }

        return hex;
    }
};

// 3. SHA256 Hash Calculation (using CNG - Cryptography Next Generation)
#include <bcrypt.h>

#pragma comment(lib, "bcrypt.lib")

class SHA256Hash {
public:
    static std::string calculate(const std::string& input) {
        BCRYPT_ALG_HANDLE hAlg = nullptr;
        BCRYPT_HASH_HANDLE hHash = nullptr;
        NTSTATUS status;
        DWORD hashLen = 0;
        DWORD resultLen = 0;

        // Open algorithm provider
        status = BCryptOpenAlgorithmProvider(&hAlg, BCRYPT_SHA256_ALGORITHM, nullptr, 0);
        if (status != 0) {
            std::cerr << "BCryptOpenAlgorithmProvider failed: " << status << std::endl;
            return "";
        }

        // Get hash length
        status = BCryptGetProperty(hAlg, BCRYPT_HASH_LENGTH, reinterpret_cast<PBYTE>(&hashLen), sizeof(hashLen), &resultLen, 0);
        if (status != 0) {
            BCryptCloseAlgorithmProvider(hAlg, 0);
            return "";
        }

        // Create hash object
        status = BCryptCreateHash(hAlg, &hHash, nullptr, 0, nullptr, 0, 0);
        if (status != 0) {
            BCryptCloseAlgorithmProvider(hAlg, 0);
            return "";
        }

        // Hash data
        status = BCryptHashData(hHash, reinterpret_cast<PBYTE>(const_cast<char*>(input.c_str())), input.size(), 0);
        if (status != 0) {
            BCryptDestroyHash(hHash);
            BCryptCloseAlgorithmProvider(hAlg, 0);
            return "";
        }

        // Get hash value
        std::vector<BYTE> hash(hashLen);
        status = BCryptFinishHash(hHash, hash.data(), hashLen, 0);
        if (status != 0) {
            BCryptDestroyHash(hHash);
            BCryptCloseAlgorithmProvider(hAlg, 0);
            return "";
        }

        // Clean up
        BCryptDestroyHash(hHash);
        BCryptCloseAlgorithmProvider(hAlg, 0);

        return bytesToHex(hash);
    }

    static std::string calculateFile(const std::string& filename) {
        BCRYPT_ALG_HANDLE hAlg = nullptr;
        BCRYPT_HASH_HANDLE hHash = nullptr;
        NTSTATUS status;
        DWORD hashLen = 0;
        DWORD resultLen = 0;

        std::ifstream file(filename, std::ios::binary);
        if (!file) {
            std::cerr << "Failed to open file: " << filename << std::endl;
            return "";
        }

        status = BCryptOpenAlgorithmProvider(&hAlg, BCRYPT_SHA256_ALGORITHM, nullptr, 0);
        if (status != 0) {
            return "";
        }

        status = BCryptGetProperty(hAlg, BCRYPT_HASH_LENGTH, reinterpret_cast<PBYTE>(&hashLen), sizeof(hashLen), &resultLen, 0);
        if (status != 0) {
            BCryptCloseAlgorithmProvider(hAlg, 0);
            return "";
        }

        status = BCryptCreateHash(hAlg, &hHash, nullptr, 0, nullptr, 0, 0);
        if (status != 0) {
            BCryptCloseAlgorithmProvider(hAlg, 0);
            return "";
        }

        // Hash file in chunks
        const int BUFFER_SIZE = 4096;
        std::vector<BYTE> buffer(BUFFER_SIZE);

        while (file) {
            file.read(reinterpret_cast<char*>(buffer.data()), BUFFER_SIZE);
            std::streamsize bytesRead = file.gcount();

            if (bytesRead > 0) {
                status = BCryptHashData(hHash, buffer.data(), static_cast<ULONG>(bytesRead), 0);
                if (status != 0) {
                    BCryptDestroyHash(hHash);
                    BCryptCloseAlgorithmProvider(hAlg, 0);
                    return "";
                }
            }
        }

        std::vector<BYTE> hash(hashLen);
        status = BCryptFinishHash(hHash, hash.data(), hashLen, 0);
        if (status != 0) {
            BCryptDestroyHash(hHash);
            BCryptCloseAlgorithmProvider(hAlg, 0);
            return "";
        }

        BCryptDestroyHash(hHash);
        BCryptCloseAlgorithmProvider(hAlg, 0);

        return bytesToHex(hash);
    }

private:
    static std::string bytesToHex(const std::vector<BYTE>& bytes) {
        std::string hex;
        hex.reserve(bytes.size() * 2);

        for (BYTE b : bytes) {
            char buf[3];
            sprintf_s(buf, "%02x", b);
            hex += buf;
        }

        return hex;
    }
};

// 4. SHA512 Hash Calculation
class SHA512Hash {
public:
    static std::string calculate(const std::string& input) {
        BCRYPT_ALG_HANDLE hAlg = nullptr;
        BCRYPT_HASH_HANDLE hHash = nullptr;
        NTSTATUS status;
        DWORD hashLen = 0;
        DWORD resultLen = 0;

        status = BCryptOpenAlgorithmProvider(&hAlg, BCRYPT_SHA512_ALGORITHM, nullptr, 0);
        if (status != 0) {
            return "";
        }

        status = BCryptGetProperty(hAlg, BCRYPT_HASH_LENGTH, reinterpret_cast<PBYTE>(&hashLen), sizeof(hashLen), &resultLen, 0);
        if (status != 0) {
            BCryptCloseAlgorithmProvider(hAlg, 0);
            return "";
        }

        status = BCryptCreateHash(hAlg, &hHash, nullptr, 0, nullptr, 0, 0);
        if (status != 0) {
            BCryptCloseAlgorithmProvider(hAlg, 0);
            return "";
        }

        status = BCryptHashData(hHash, reinterpret_cast<PBYTE>(const_cast<char*>(input.c_str())), input.size(), 0);
        if (status != 0) {
            BCryptDestroyHash(hHash);
            BCryptCloseAlgorithmProvider(hAlg, 0);
            return "";
        }

        std::vector<BYTE> hash(hashLen);
        status = BCryptFinishHash(hHash, hash.data(), hashLen, 0);
        if (status != 0) {
            BCryptDestroyHash(hHash);
            BCryptCloseAlgorithmProvider(hAlg, 0);
            return "";
        }

        BCryptDestroyHash(hHash);
        BCryptCloseAlgorithmProvider(hAlg, 0);

        return bytesToHex(hash);
    }

private:
    static std::string bytesToHex(const std::vector<BYTE>& bytes) {
        std::string hex;
        hex.reserve(bytes.size() * 2);

        for (BYTE b : bytes) {
            char buf[3];
            sprintf_s(buf, "%02x", b);
            hex += buf;
        }

        return hex;
    }
};

// 5. Compare Hash Values
class HashComparator {
public:
    static bool compare(const std::string& hash1, const std::string& hash2) {
        // Case-insensitive comparison
        if (hash1.length() != hash2.length()) {
            return false;
        }

        for (size_t i = 0; i < hash1.length(); ++i) {
            char c1 = tolower(hash1[i]);
            char c2 = tolower(hash2[i]);

            if (c1 != c2) {
                return false;
            }
        }

        return true;
    }

    static bool verifyFileIntegrity(const std::string& filename, const std::string& expectedHash) {
        std::string actualHash = SHA256Hash::calculateFile(filename);

        if (actualHash.empty()) {
            return false;
        }

        return compare(actualHash, expectedHash);
    }
};

// 6. HMAC (Hash-based Message Authentication Code)
class HMACSHA256 {
public:
    static std::string calculate(const std::string& message, const std::string& key) {
        BCRYPT_ALG_HANDLE hAlg = nullptr;
        BCRYPT_HASH_HANDLE hHash = nullptr;
        NTSTATUS status;
        DWORD hashLen = 0;
        DWORD resultLen = 0;

        // Open algorithm provider
        status = BCryptOpenAlgorithmProvider(&hAlg, BCRYPT_SHA256_ALGORITHM, nullptr, BCRYPT_ALG_HANDLE_HMAC_FLAG);
        if (status != 0) {
            std::cerr << "Failed to open algorithm provider" << std::endl;
            return "";
        }

        // Create hash with key
        status = BCryptCreateHash(hAlg, &hHash, nullptr, 0,
            reinterpret_cast<PBYTE>(const_cast<char*>(key.c_str())),
            static_cast<ULONG>(key.size()), 0);

        if (status != 0) {
            BCryptCloseAlgorithmProvider(hAlg, 0);
            return "";
        }

        // Hash message
        status = BCryptHashData(hHash, reinterpret_cast<PBYTE>(const_cast<char*>(message.c_str())),
            static_cast<ULONG>(message.size()), 0);

        if (status != 0) {
            BCryptDestroyHash(hHash);
            BCryptCloseAlgorithmProvider(hAlg, 0);
            return "";
        }

        // Get hash length
        status = BCryptGetProperty(hAlg, BCRYPT_HASH_LENGTH, reinterpret_cast<PBYTE>(&hashLen), sizeof(hashLen), &resultLen, 0);
        if (status != 0) {
            BCryptDestroyHash(hHash);
            BCryptCloseAlgorithmProvider(hAlg, 0);
            return "";
        }

        // Get hash value
        std::vector<BYTE> hash(hashLen);
        status = BCryptFinishHash(hHash, hash.data(), hashLen, 0);

        if (status != 0) {
            BCryptDestroyHash(hHash);
            BCryptCloseAlgorithmProvider(hAlg, 0);
            return "";
        }

        BCryptDestroyHash(hHash);
        BCryptCloseAlgorithmProvider(hAlg, 0);

        return bytesToHex(hash);
    }

private:
    static std::string bytesToHex(const std::vector<BYTE>& bytes) {
        std::string hex;
        hex.reserve(bytes.size() * 2);

        for (BYTE b : bytes) {
            char buf[3];
            sprintf_s(buf, "%02x", b);
            hex += buf;
        }

        return hex;
    }
};

// Main demonstration
int main() {
    std::cout << "=== Windows C++ Hash Calculation Examples ===" << std::endl;

    std::string testString = "Hello, World!";
    std::cout << "Input string: " << testString << std::endl;

    // 1. MD5
    std::cout << "\n--- MD5 Hash ---" << std::endl;
    std::string md5Hash = MD5Hash::calculate(testString);
    std::cout << "MD5: " << md5Hash << std::endl;

    // 2. SHA1
    std::cout << "\n--- SHA1 Hash ---" << std::endl;
    std::string sha1Hash = SHA1Hash::calculate(testString);
    std::cout << "SHA1: " << sha1Hash << std::endl;

    // 3. SHA256
    std::cout << "\n--- SHA256 Hash ---" << std::endl;
    std::string sha256Hash = SHA256Hash::calculate(testString);
    std::cout << "SHA256: " << sha256Hash << std::endl;

    // 4. SHA512
    std::cout << "\n--- SHA512 Hash ---" << std::endl;
    std::string sha512Hash = SHA512Hash::calculate(testString);
    std::cout << "SHA512: " << sha512Hash << std::endl;

    // 5. File hashing
    std::cout << "\n--- File Hashing ---" << std::endl;
    // Create test file
    std::ofstream testFile("test_file.txt");
    testFile << "This is a test file for hash calculation.";
    testFile.close();

    std::string fileMD5 = MD5Hash::calculateFile("test_file.txt");
    std::cout << "File MD5: " << fileMD5 << std::endl;

    std::string fileSHA256 = SHA256Hash::calculateFile("test_file.txt");
    std::cout << "File SHA256: " << fileSHA256 << std::endl;

    // 6. Hash comparison
    std::cout << "\n--- Hash Comparison ---" << std::endl;
    std::string hash1 = "a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146";
    std::string hash2 = "A591A6D40BF420404A011733CFB7B190D62C65BF0BCDA32B57B277D9AD9F146";

    bool match = HashComparator::compare(hash1, hash2);
    std::cout << "Hash comparison (case insensitive): " << (match ? "MATCH" : "NO MATCH") << std::endl;

    // Verify file integrity
    bool integrity = HashComparator::verifyFileIntegrity("test_file.txt", fileSHA256);
    std::cout << "File integrity check: " << (integrity ? "PASSED" : "FAILED") << std::endl;

    // 7. HMAC
    std::cout << "\n--- HMAC-SHA256 ---" << std::endl;
    std::string message = "Important message to authenticate";
    std::string key = "secret_key_123";

    std::string hmac = HMACSHA256::calculate(message, key);
    std::cout << "Message: " << message << std::endl;
    std::cout << "Key: " << key << std::endl;
    std::cout << "HMAC-SHA256: " << hmac << std::endl;

    std::cout << "\n=== All Hash Calculation Examples Completed ===" << std::endl;
    return 0;
}
CódigocppIntermedio

Cifrado/Descifrado AES

Implementar cifrado y descifrado AES-256 usando Windows Cryptography API (CNG)

Dificultad

8/10

Tiempo estimado

35 min

Etiquetas

cpp, cryptography, aes, encryption, windows

Requisitos previos

Advanced C++, Windows Cryptography API (CNG)

cpp
// Windows C++ AES Encryption/Decryption Examples
// Using Cryptography Next Generation (CNG) API

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <windows.h>
#include <bcrypt.h>

#pragma comment(lib, "bcrypt.lib")

// 1. Basic AES-256 Encryption
class AESEncryptor {
private:
    BCRYPT_ALG_HANDLE hAlg;
    BCRYPT_KEY_HANDLE hKey;
    std::vector<BYTE> keyObject;
    std::vector<BYTE> iv;

public:
    AESEncryptor() : hAlg(nullptr), hKey(nullptr) {}

    ~AESEncryptor() {
        cleanup();
    }

    bool initialize(const std::vector<BYTE>& key) {
        NTSTATUS status;
        DWORD resultLen;

        // Open algorithm provider (AES-256-CBC)
        status = BCryptOpenAlgorithmProvider(&hAlg, BCRYPT_AES_ALGORITHM, nullptr, 0);
        if (status != 0) {
            std::cerr << "BCryptOpenAlgorithmProvider failed: " << status << std::endl;
            return false;
        }

        // Set chaining mode to CBC
        status = BCryptSetProperty(hAlg, BCRYPT_CHAINING_MODE,
            reinterpret_cast<PBYTE>(const_cast<char*>(BCRYPT_CHAIN_MODE_CBC)),
            sizeof(BCRYPT_CHAIN_MODE_CBC), 0);

        if (status != 0) {
            std::cerr << "BCryptSetProperty failed: " << status << std::endl;
            cleanup();
            return false;
        }

        // Get key object length
        status = BCryptGetProperty(hAlg, BCRYPT_OBJECT_LENGTH,
            reinterpret_cast<PBYTE>(&resultLen), sizeof(resultLen), &resultLen, 0);

        if (status != 0) {
            cleanup();
            return false;
        }

        keyObject.resize(resultLen);

        // Generate key from key material
        status = BCryptGenerateSymmetricKey(hAlg, &hKey,
            keyObject.data(), keyObject.size(),
            const_cast<PBYTE>(key.data()), key.size(), 0);

        if (status != 0) {
            std::cerr << "BCryptGenerateSymmetricKey failed: " << status << std::endl;
            cleanup();
            return false;
        }

        // Get IV length
        status = BCryptGetProperty(hAlg, BCRYPT_BLOCK_LENGTH,
            reinterpret_cast<PBYTE>(&resultLen), sizeof(resultLen), &resultLen, 0);

        if (status != 0) {
            cleanup();
            return false;
        }

        iv.resize(resultLen, 0); // Initialize with zeros

        return true;
    }

    std::vector<BYTE> encrypt(const std::vector<BYTE>& plaintext) {
        if (hAlg == nullptr || hKey == nullptr) {
            std::cerr << "Encryptor not initialized" << std::endl;
            return {};
        }

        NTSTATUS status;
        DWORD resultLen;

        // Get ciphertext length
        status = BCryptEncrypt(hKey, const_cast<PBYTE>(plaintext.data()),
            static_cast<ULONG>(plaintext.size()), nullptr,
            iv.data(), iv.size(), nullptr, 0, &resultLen, BCRYPT_BLOCK_PADDING);

        if (status != 0) {
            std::cerr << "BCryptEncrypt (get size) failed: " << status << std::endl;
            return {};
        }

        std::vector<BYTE> ciphertext(resultLen);

        // Encrypt data
        status = BCryptEncrypt(hKey, const_cast<PBYTE>(plaintext.data()),
            static_cast<ULONG>(plaintext.size()), nullptr,
            iv.data(), iv.size(), ciphertext.data(),
            static_cast<ULONG>(ciphertext.size()), &resultLen, BCRYPT_BLOCK_PADDING);

        if (status != 0) {
            std::cerr << "BCryptEncrypt failed: " << status << std::endl;
            return {};
        }

        ciphertext.resize(resultLen);
        return ciphertext;
    }

    std::vector<BYTE> decrypt(const std::vector<BYTE>& ciphertext) {
        if (hAlg == nullptr || hKey == nullptr) {
            std::cerr << "Encryptor not initialized" << std::endl;
            return {};
        }

        NTSTATUS status;
        DWORD resultLen;

        // Get plaintext length
        status = BCryptDecrypt(hKey, const_cast<PBYTE>(ciphertext.data()),
            static_cast<ULONG>(ciphertext.size()), nullptr,
            iv.data(), iv.size(), nullptr, 0, &resultLen, BCRYPT_BLOCK_PADDING);

        if (status != 0) {
            std::cerr << "BCryptDecrypt (get size) failed: " << status << std::endl;
            return {};
        }

        std::vector<BYTE> plaintext(resultLen);

        // Decrypt data
        status = BCryptDecrypt(hKey, const_cast<PBYTE>(ciphertext.data()),
            static_cast<ULONG>(ciphertext.size()), nullptr,
            iv.data(), iv.size(), plaintext.data(),
            static_cast<ULONG>(plaintext.size()), &resultLen, BCRYPT_BLOCK_PADDING);

        if (status != 0) {
            std::cerr << "BCryptDecrypt failed: " << status << std::endl;
            return {};
        }

        plaintext.resize(resultLen);
        return plaintext;
    }

    void setIV(const std::vector<BYTE>& newIV) {
        iv = newIV;
    }

    std::vector<BYTE> getIV() const {
        return iv;
    }

private:
    void cleanup() {
        if (hKey) {
            BCryptDestroyKey(hKey);
            hKey = nullptr;
        }
        if (hAlg) {
            BCryptCloseAlgorithmProvider(hAlg, 0);
            hAlg = nullptr;
        }
    }
};

// 2. Generate Random AES Key
class KeyGenerator {
public:
    static std::vector<BYTE> generateAESKey(int bits = 256) {
        BCRYPT_ALG_HANDLE hAlg = nullptr;
        NTSTATUS status;

        status = BCryptOpenAlgorithmProvider(&hAlg, BCRYPT_RNG_ALGORITHM, nullptr, 0);
        if (status != 0) {
            std::cerr << "Failed to open RNG provider" << std::endl;
            return {};
        }

        int keyLen = bits / 8;
        std::vector<BYTE> key(keyLen);

        status = BCryptGenRandom(hAlg, key.data(), keyLen, 0);
        if (status != 0) {
            BCryptCloseAlgorithmProvider(hAlg, 0);
            return {};
        }

        BCryptCloseAlgorithmProvider(hAlg, 0);
        return key;
    }

    static std::vector<BYTE> generateIV(int size = 16) {
        BCRYPT_ALG_HANDLE hAlg = nullptr;
        NTSTATUS status;

        status = BCryptOpenAlgorithmProvider(&hAlg, BCRYPT_RNG_ALGORITHM, nullptr, 0);
        if (status != 0) {
            return {};
        }

        std::vector<BYTE> iv(size);

        status = BCryptGenRandom(hAlg, iv.data(), size, 0);
        if (status != 0) {
            BCryptCloseAlgorithmProvider(hAlg, 0);
            return {};
        }

        BCryptCloseAlgorithmProvider(hAlg, 0);
        return iv;
    }
};

// 3. String Encryption/Decryption Helper
class StringCryptor {
public:
    static std::string encryptString(const std::string& plaintext, const std::vector<BYTE>& key) {
        // Convert string to byte vector
        std::vector<BYTE> data(plaintext.begin(), plaintext.end());

        AESEncryptor encryptor;
        if (!encryptor.initialize(key)) {
            return "";
        }

        std::vector<BYTE> ciphertext = encryptor.encrypt(data);

        // Return as hex string for display
        return bytesToHex(ciphertext);
    }

    static std::string decryptString(const std::string& hexCiphertext, const std::vector<BYTE>& key) {
        // Convert hex string to byte vector
        std::vector<BYTE> ciphertext = hexToBytes(hexCiphertext);

        AESEncryptor encryptor;
        if (!encryptor.initialize(key)) {
            return "";
        }

        std::vector<BYTE> plaintext = encryptor.decrypt(ciphertext);

        return std::string(plaintext.begin(), plaintext.end());
    }

private:
    static std::string bytesToHex(const std::vector<BYTE>& bytes) {
        std::string hex;
        hex.reserve(bytes.size() * 2);

        for (BYTE b : bytes) {
            char buf[3];
            sprintf_s(buf, "%02x", b);
            hex += buf;
        }

        return hex;
    }

    static std::vector<BYTE> hexToBytes(const std::string& hex) {
        std::vector<BYTE> bytes;

        for (size_t i = 0; i < hex.length(); i += 2) {
            std::string byteString = hex.substr(i, 2);
            BYTE byte = static_cast<BYTE>(strtol(byteString.c_str(), nullptr, 16));
            bytes.push_back(byte);
        }

        return bytes;
    }
};

// 4. File Encryption/Decryption
class FileCryptor {
public:
    static bool encryptFile(const std::string& inputFile, const std::string& outputFile,
                           const std::vector<BYTE>& key) {
        // Read input file
        std::ifstream inFile(inputFile, std::ios::binary);
        if (!inFile) {
            std::cerr << "Failed to open input file" << std::endl;
            return false;
        }

        std::vector<BYTE> plaintext((std::istreambuf_iterator<char>(inFile)),
                                   std::istreambuf_iterator<char>());
        inFile.close();

        // Encrypt
        AESEncryptor encryptor;
        if (!encryptor.initialize(key)) {
            return false;
        }

        std::vector<BYTE> ciphertext = encryptor.encrypt(plaintext);
        if (ciphertext.empty()) {
            return false;
        }

        // Write output file
        std::ofstream outFile(outputFile, std::ios::binary);
        if (!outFile) {
            std::cerr << "Failed to open output file" << std::endl;
            return false;
        }

        outFile.write(reinterpret_cast<const char*>(ciphertext.data()), ciphertext.size());
        outFile.close();

        std::cout << "File encrypted: " << inputFile << " -> " << outputFile << std::endl;
        return true;
    }

    static bool decryptFile(const std::string& inputFile, const std::string& outputFile,
                           const std::vector<BYTE>& key) {
        // Read input file
        std::ifstream inFile(inputFile, std::ios::binary);
        if (!inFile) {
            std::cerr << "Failed to open input file" << std::endl;
            return false;
        }

        std::vector<BYTE> ciphertext((std::istreambuf_iterator<char>(inFile)),
                                    std::istreambuf_iterator<char>());
        inFile.close();

        // Decrypt
        AESEncryptor encryptor;
        if (!encryptor.initialize(key)) {
            return false;
        }

        std::vector<BYTE> plaintext = encryptor.decrypt(ciphertext);
        if (plaintext.empty()) {
            return false;
        }

        // Write output file
        std::ofstream outFile(outputFile, std::ios::binary);
        if (!outFile) {
            std::cerr << "Failed to open output file" << std::endl;
            return false;
        }

        outFile.write(reinterpret_cast<const char*>(plaintext.data()), plaintext.size());
        outFile.close();

        std::cout << "File decrypted: " << inputFile << " -> " << outputFile << std::endl;
        return true;
    }
};

// 5. AES-GCM (Galois/Counter Mode) - Authenticated Encryption
class AESGCM {
public:
    struct GCMResult {
        std::vector<BYTE> ciphertext;
        std::vector<BYTE> tag;
    };

    static GCMResult encrypt(const std::vector<BYTE>& plaintext, const std::vector<BYTE>& key,
                            const std::vector<BYTE>& iv, const std::vector<BYTE>& aad = {}) {
        GCMResult result;

        BCRYPT_ALG_HANDLE hAlg = nullptr;
        BCRYPT_KEY_HANDLE hKey = nullptr;
        NTSTATUS status;

        // Open AES provider
        status = BCryptOpenAlgorithmProvider(&hAlg, BCRYPT_AES_ALGORITHM, nullptr, 0);
        if (status != 0) {
            return result;
        }

        // Set GCM mode
        status = BCryptSetProperty(hAlg, BCRYPT_CHAINING_MODE,
            reinterpret_cast<PBYTE>(const_cast<char*>(BCRYPT_CHAIN_MODE_GCM)),
            sizeof(BCRYPT_CHAIN_MODE_GCM), 0);

        if (status != 0) {
            BCryptCloseAlgorithmProvider(hAlg, 0);
            return result;
        }

        // Create key
        std::vector<BYTE> keyObj;
        DWORD keyObjLen;
        BCryptGetProperty(hAlg, BCRYPT_OBJECT_LENGTH,
            reinterpret_cast<PBYTE>(&keyObjLen), sizeof(keyObjLen), &keyObjLen, 0);

        keyObj.resize(keyObjLen);

        status = BCryptGenerateSymmetricKey(hAlg, &hKey,
            keyObj.data(), keyObj.size(),
            const_cast<PBYTE>(key.data()), key.size(), 0);

        if (status != 0) {
            BCryptCloseAlgorithmProvider(hAlg, 0);
            return result;
        }

        // Get tag length
        DWORD tagLen = 16;
        BCRYPT_AUTHENTICATED_CIPHER_MODE_INFO authInfo = {0};
        authInfo.pbNonce = const_cast<PBYTE>(iv.data());
        authInfo.cbNonce = iv.size();
        authInfo.pbAuthData = const_cast<PBYTE>(aad.data());
        authInfo.cbAuthData = aad.size();
        authInfo.pbTag = result.tag.data();
        authInfo.cbTag = tagLen;

        result.tag.resize(tagLen);

        // Get output length
        DWORD ciphertextLen;
        status = BCryptEncrypt(hKey, const_cast<PBYTE>(plaintext.data()), plaintext.size(),
            &authInfo, nullptr, 0, nullptr, 0, &ciphertextLen, 0);

        if (status != 0) {
            BCryptDestroyKey(hKey);
            BCryptCloseAlgorithmProvider(hAlg, 0);
            return result;
        }

        result.ciphertext.resize(ciphertextLen);

        // Encrypt
        status = BCryptEncrypt(hKey, const_cast<PBYTE>(plaintext.data()), plaintext.size(),
            &authInfo, nullptr, 0, result.ciphertext.data(), ciphertextLen, &ciphertextLen, 0);

        result.ciphertext.resize(ciphertextLen);

        BCryptDestroyKey(hKey);
        BCryptCloseAlgorithmProvider(hAlg, 0);

        return result;
    }
};

// 6. Password-Based Key Derivation (PBKDF2)
class PasswordKeyDerivation {
public:
    static std::vector<BYTE> deriveKey(const std::string& password, const std::vector<BYTE>& salt,
                                       int iterations = 10000, int keyLen = 32) {
        BCRYPT_ALG_HANDLE hAlg = nullptr;
        NTSTATUS status;

        status = BCryptOpenAlgorithmProvider(&hAlg, BCRYPT_SHA256_ALGORITHM, nullptr, 0);
        if (status != 0) {
            return {};
        }

        // Create hash for password
        BCRYPT_HASH_HANDLE hHash = nullptr;
        std::vector<BYTE> hashObj;
        DWORD hashObjLen;
        BCryptGetProperty(hAlg, BCRYPT_OBJECT_LENGTH,
            reinterpret_cast<PBYTE>(&hashObjLen), sizeof(hashObjLen), &hashObjLen, 0);

        hashObj.resize(hashObjLen);

        status = BCryptCreateHash(hAlg, &hHash, hashObj.data(), hashObjLen,
            nullptr, 0, 0);

        if (status != 0) {
            BCryptCloseAlgorithmProvider(hAlg, 0);
            return {};
        }

        // Simple key derivation (in production, use proper KDF like BCryptDeriveKeyPBKDF2)
        std::vector<BYTE> key(keyLen);
        std::vector<BYTE> passwordBytes(password.begin(), password.end());

        // Combine password + salt and hash
        std::vector<BYTE> combined = passwordBytes;
        combined.insert(combined.end(), salt.begin(), salt.end());

        for (int i = 0; i < iterations; i++) {
            BCryptHashData(hHash, combined.data(), combined.size(), 0);

            DWORD hashLen = 32;
            std::vector<BYTE> hash(32);
            BCryptFinishHash(hHash, hash.data(), hashLen, 0);

            if (i == iterations - 1) {
                // Use final hash as key
                std::copy(hash.begin(), hash.begin() + std::min(keyLen, 32), key.begin());
            }

            // Use hash as input for next iteration
            combined = hash;
        }

        BCryptDestroyHash(hHash);
        BCryptCloseAlgorithmProvider(hAlg, 0);

        return key;
    }
};

// Main demonstration
int main() {
    std::cout << "=== Windows C++ AES Encryption/Decryption Examples ===" << std::endl;

    // 1. Generate random key
    std::cout << "\n--- Key Generation ---" << std::endl;
    std::vector<BYTE> key = KeyGenerator::generateAESKey(256);

    std::cout << "Generated AES-256 Key: ";
    for (BYTE b : key) {
        printf("%02x", b);
    }
    std::cout << std::endl;

    std::vector<BYTE> iv = KeyGenerator::generateIV(16);
    std::cout << "Generated IV: ";
    for (BYTE b : iv) {
        printf("%02x", b);
    }
    std::cout << std::endl;

    // 2. String encryption/decryption
    std::cout << "\n--- String Encryption ---" << std::endl;
    std::string plaintext = "Hello, World! This is a secret message.";
    std::cout << "Original: " << plaintext << std::endl;

    std::string encryptedHex = StringCryptor::encryptString(plaintext, key);
    std::cout << "Encrypted: " << encryptedHex << std::endl;

    std::string decrypted = StringCryptor::decryptString(encryptedHex, key);
    std::cout << "Decrypted: " << decrypted << std::endl;

    // 3. File encryption/decryption
    std::cout << "\n--- File Encryption ---" << std::endl;
    std::ofstream testFile("plain_file.txt");
    testFile << "This is the content of a secret file.";
    testFile.close();

    FileCryptor::encryptFile("plain_file.txt", "encrypted_file.bin", key);
    FileCryptor::decryptFile("encrypted_file.bin", "decrypted_file.txt", key);

    // 4. Password-based key derivation
    std::cout << "\n--- Password Key Derivation ---" << std::endl;
    std::string password = "mySecurePassword123";
    std::vector<BYTE> salt = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};

    std::vector<BYTE> derivedKey = PasswordKeyDerivation::deriveKey(password, salt, 10000, 32);

    std::cout << "Password: " << password << std::endl;
    std::cout << "Derived Key: ";
    for (BYTE b : derivedKey) {
        printf("%02x", b);
    }
    std::cout << std::endl;

    std::cout << "\n=== All AES Encryption Examples Completed ===" << std::endl;
    return 0;
}
CódigocppSimple

Codificación/Decodificación Base64

Codificar datos binarios en cadenas Base64 y decodificar cadenas Base64 de vuelta a datos binarios usando Windows API

Dificultad

4/10

Tiempo estimado

20 min

Etiquetas

cpp, base64, encoding, windows

Requisitos previos

Basic C++, Windows Cryptography API

cpp
// Windows C++ Base64 Encoding/Decoding Examples
// Using Windows Cryptography API

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <windows.h>
#include <wincrypt.h>

#pragma comment(lib, "crypt32.lib")

// 1. Basic Base64 Encoding
class Base64Encoder {
public:
    static std::string encode(const std::vector<BYTE>& data) {
        DWORD encodedSize = 0;

        // Get the required buffer size
        if (!CryptBinaryToStringA(data.data(), static_cast<DWORD>(data.size()),
            CRYPT_STRING_BASE64 | CRYPT_STRING_NOCR, nullptr, &encodedSize)) {
            std::cerr << "Failed to get encoded size" << std::endl;
            return "";
        }

        // Allocate buffer and encode
        std::string encoded;
        encoded.resize(encodedSize);

        if (!CryptBinaryToStringA(data.data(), static_cast<DWORD>(data.size()),
            CRYPT_STRING_BASE64 | CRYPT_STRING_NOCR,
            reinterpret_cast<LPSTR>(&encoded[0]), &encodedSize)) {
            std::cerr << "Failed to encode data" << std::endl;
            return "";
        }

        // Remove null terminator if present
        if (!encoded.empty() && encoded[encodedSize - 1] == '\0') {
            encoded.resize(encodedSize - 1);
        } else {
            encoded.resize(encodedSize);
        }

        return encoded;
    }

    static std::string encodeString(const std::string& text) {
        std::vector<BYTE> data(text.begin(), text.end());
        return encode(data);
    }

    static std::string encodeWithLineBreaks(const std::vector<BYTE>& data, int lineWidth = 76) {
        DWORD encodedSize = 0;

        // Get the required buffer size
        if (!CryptBinaryToStringA(data.data(), static_cast<DWORD>(data.size()),
            CRYPT_STRING_BASE64, nullptr, &encodedSize)) {
            return "";
        }

        std::string encoded;
        encoded.resize(encodedSize);

        if (!CryptBinaryToStringA(data.data(), static_cast<DWORD>(data.size()),
            CRYPT_STRING_BASE64,
            reinterpret_cast<LPSTR>(&encoded[0]), &encodedSize)) {
            return "";
        }

        if (!encoded.empty() && encoded[encodedSize - 1] == '\0') {
            encoded.resize(encodedSize - 1);
        } else {
            encoded.resize(encodedSize);
        }

        return encoded;
    }
};

// 2. Basic Base64 Decoding
class Base64Decoder {
public:
    static std::vector<BYTE> decode(const std::string& encoded) {
        DWORD decodedSize = 0;

        // Get the required buffer size
        if (!CryptStringToBinaryA(encoded.c_str(), 0, CRYPT_STRING_BASE64,
            nullptr, &decodedSize, nullptr, nullptr)) {
            std::cerr << "Failed to get decoded size" << std::endl;
            return {};
        }

        // Allocate buffer and decode
        std::vector<BYTE> decoded(decodedSize);

        if (!CryptStringToBinaryA(encoded.c_str(), 0, CRYPT_STRING_BASE64,
            decoded.data(), &decodedSize, nullptr, nullptr)) {
            std::cerr << "Failed to decode data" << std::endl;
            return {};
        }

        decoded.resize(decodedSize);
        return decoded;
    }

    static std::string decodeToString(const std::string& encoded) {
        std::vector<BYTE> data = decode(encoded);

        if (data.empty()) {
            return "";
        }

        return std::string(data.begin(), data.end());
    }
};

// 3. Base64 for URL Encoding (URL-safe variant)
class Base64URLEncoder {
public:
    static std::string encode(const std::vector<BYTE>& data) {
        // First, do standard base64 encoding
        std::string encoded = Base64Encoder::encode(data);

        // Make URL-safe: replace + with -, replace / with _, remove trailing =
        for (char& c : encoded) {
            if (c == '+') c = '-';
            else if (c == '/') c = '_';
        }

        // Remove padding characters
        size_t paddingPos = encoded.find('=');
        if (paddingPos != std::string::npos) {
            encoded = encoded.substr(0, paddingPos);
        }

        return encoded;
    }

    static std::vector<BYTE> decode(const std::string& encoded) {
        // Convert URL-safe back to standard base64
        std::string standard = encoded;

        for (char& c : standard) {
            if (c == '-') c = '+';
            else if (c == '_') c = '/';
        }

        // Add padding if necessary
        while (standard.size() % 4 != 0) {
            standard += '=';
        }

        return Base64Decoder::decode(standard);
    }
};

// 4. Base64 File Operations
class Base64File {
public:
    static bool encodeFile(const std::string& inputFile, const std::string& outputFile) {
        // Read input file
        std::ifstream inFile(inputFile, std::ios::binary);
        if (!inFile) {
            std::cerr << "Failed to open input file: " << inputFile << std::endl;
            return false;
        }

        std::vector<BYTE> fileData((std::istreambuf_iterator<char>(inFile)),
                                   std::istreambuf_iterator<char>());
        inFile.close();

        // Encode
        std::string encoded = Base64Encoder::encodeWithLineBreaks(fileData);

        // Write output file
        std::ofstream outFile(outputFile);
        if (!outFile) {
            std::cerr << "Failed to open output file: " << outputFile << std::endl;
            return false;
        }

        outFile << encoded;
        outFile.close();

        std::cout << "File encoded: " << inputFile << " -> " << outputFile << std::endl;
        std::cout << "Original size: " << fileData.size() << " bytes" << std::endl;
        std::cout << "Encoded size: " << encoded.size() << " characters" << std::endl;

        return true;
    }

    static bool decodeFile(const std::string& inputFile, const std::string& outputFile) {
        // Read input file
        std::ifstream inFile(inputFile);
        if (!inFile) {
            std::cerr << "Failed to open input file: " << inputFile << std::endl;
            return false;
        }

        std::string encoded((std::istreambuf_iterator<char>(inFile)),
                           std::istreambuf_iterator<char>());
        inFile.close();

        // Decode
        std::vector<BYTE> fileData = Base64Decoder::decode(encoded);

        if (fileData.empty()) {
            std::cerr << "Failed to decode file data" << std::endl;
            return false;
        }

        // Write output file
        std::ofstream outFile(outputFile, std::ios::binary);
        if (!outFile) {
            std::cerr << "Failed to open output file: " << outputFile << std::endl;
            return false;
        }

        outFile.write(reinterpret_cast<const char*>(fileData.data()), fileData.size());
        outFile.close();

        std::cout << "File decoded: " << inputFile << " -> " << outputFile << std::endl;
        std::cout << "Encoded size: " << encoded.size() << " characters" << std::endl;
        std::cout << "Decoded size: " << fileData.size() << " bytes" << std::endl;

        return true;
    }
};

// 5. Base64 Hash Encoding
class Base64Hash {
public:
    // Encode SHA256 hash as base64
    static std::string encodeSHA256(const std::string& input) {
        // Calculate SHA256 hash
        std::vector<BYTE> hash = calculateSHA256(input);

        // Encode as base64
        return Base64Encoder::encode(hash);
    }

    // Common for JWT (JSON Web Tokens)
    static std::string encodeForJWT(const std::string& data) {
        std::vector<BYTE> bytes(data.begin(), data.end());

        // Use URL-safe base64 encoding
        return Base64URLEncoder::encode(bytes);
    }

private:
    static std::vector<BYTE> calculateSHA256(const std::string& input) {
        // This would normally use Windows Cryptography API
        // For demonstration, returning a placeholder
        std::vector<BYTE> hash(32, 0x42);
        return hash;
    }
};

// 6. Base64 Image Encoding
class Base64Image {
public:
    static std::string encodeImageWithHeader(const std::string& imageFile, const std::string& mimeType) {
        // Read image file
        std::ifstream inFile(imageFile, std::ios::binary);
        if (!inFile) {
            return "";
        }

        std::vector<BYTE> imageData((std::istreambuf_iterator<char>(inFile)),
                                    std::istreambuf_iterator<char>());
        inFile.close();

        // Encode as base64
        std::string base64Data = Base64Encoder::encode(imageData);

        // Add data URI header
        return "data:" + mimeType + ";base64," + base64Data;
    }

    static bool saveImageFromDataURI(const std::string& dataURI, const std::string& outputFile) {
        // Check for data URI header
        const std::string prefix = "data:image/";
        size_t startPos = dataURI.find(prefix);

        if (startPos == std::string::npos) {
            std::cerr << "Invalid data URI" << std::endl;
            return false;
        }

        startPos = dataURI.find(";base64,") + 8; // Skip ";base64,"
        std::string base64Data = dataURI.substr(startPos);

        // Decode base64
        std::vector<BYTE> imageData = Base64Decoder::decode(base64Data);

        if (imageData.empty()) {
            return false;
        }

        // Write to file
        std::ofstream outFile(outputFile, std::ios::binary);
        if (!outFile) {
            return false;
        }

        outFile.write(reinterpret_cast<const char*>(imageData.data()), imageData.size());
        outFile.close();

        return true;
    }
};

// 7. Base64 Chunked Encoding
class Base64Chunked {
public:
    // Split data into chunks and encode each chunk separately
    static std::vector<std::string> encodeChunked(const std::vector<BYTE>& data, int chunkSize = 1024) {
        std::vector<std::string> chunks;

        for (size_t i = 0; i < data.size(); i += chunkSize) {
            size_t end = std::min(i + chunkSize, data.size());
            std::vector<BYTE> chunk(data.begin() + i, data.begin() + end);

            std::string encoded = Base64Encoder::encode(chunk);
            chunks.push_back(encoded);
        }

        return chunks;
    }

    // Decode chunks and combine
    static std::vector<BYTE> decodeChunked(const std::vector<std::string>& chunks) {
        std::vector<BYTE> result;

        for (const auto& chunk : chunks) {
            std::vector<BYTE> decoded = Base64Decoder::decode(chunk);

            if (decoded.empty()) {
                return {}; // Error
            }

            result.insert(result.end(), decoded.begin(), decoded.end());
        }

        return result;
    }
};

// 8. Base64 Validation
class Base64Validator {
public:
    static bool isValidBase64(const std::string& str) {
        if (str.empty()) {
            return false;
        }

        // Check each character
        for (char c : str) {
            bool valid = (c >= 'A' && c <= 'Z') ||
                       (c >= 'a' && c <= 'z') ||
                       (c >= '0' && c <= '9') ||
                       c == '+' || c == '/' || c == '=' ||
                       c == '\r' || c == '\n';

            if (!valid) {
                return false;
            }
        }

        // Check length is multiple of 4 (ignoring whitespace)
        int contentLength = 0;
        for (char c : str) {
            if (c != '\r' && c != '\n' && c != ' ') {
                contentLength++;
            }
        }

        return contentLength % 4 == 0;
    }

    static std::string cleanBase64String(const std::string& str) {
        std::string cleaned;

        for (char c : str) {
            if (c != '\r' && c != '\n' && c != ' ' && c != '\t') {
                cleaned += c;
            }
        }

        return cleaned;
    }
};

// Main demonstration
int main() {
    std::cout << "=== Windows C++ Base64 Encoding/Decoding Examples ===" << std::endl;

    // 1. Basic encoding
    std::cout << "\n--- Basic Encoding ---" << std::endl;
    std::string text = "Hello, World!";
    std::vector<BYTE> data(text.begin(), text.end());

    std::string encoded = Base64Encoder::encode(data);
    std::cout << "Original: " << text << std::endl;
    std::cout << "Encoded: " << encoded << std::endl;

    // 2. Basic decoding
    std::cout << "\n--- Basic Decoding ---" << std::endl;
    std::vector<BYTE> decoded = Base64Decoder::decode(encoded);
    std::string decodedText(decoded.begin(), decoded.end());
    std::cout << "Decoded: " << decodedText << std::endl;

    // 3. String encoding/decoding
    std::cout << "\n--- String Operations ---" << std::endl;
    std::string message = "This is a secret message.";
    std::string encodedMessage = Base64Encoder::encodeString(message);
    std::string decodedMessage = Base64Decoder::decodeToString(encodedMessage);

    std::cout << "Original: " << message << std::endl;
    std::cout << "Encoded: " << encodedMessage << std::endl;
    std::cout << "Decoded: " << decodedMessage << std::endl;

    // 4. URL-safe encoding
    std::cout << "\n--- URL-Safe Encoding ---" << std::endl;
    std::vector<BYTE> urlData = {0x00, 0x01, 0x02, 0x03, 0xFF, 0xFE, 0xFD};
    std::string urlEncoded = Base64URLEncoder::encode(urlData);
    std::cout << "URL-safe encoded: " << urlEncoded << std::endl;

    std::vector<BYTE> urlDecoded = Base64URLEncoder::decode(urlEncoded);
    std::cout << "URL-safe decoded: " << (urlDecoded == urlData ? "SUCCESS" : "FAILED") << std::endl;

    // 5. File encoding/decoding
    std::cout << "\n--- File Operations ---" << std::endl;
    std::ofstream testFile("test_data.txt");
    testFile << "This is test data for Base64 encoding.";
    testFile.close();

    Base64File::encodeFile("test_data.txt", "test_data.b64");
    Base64File::decodeFile("test_data.b64", "test_data_decoded.txt");

    // 6. Image encoding
    std::cout << "\n--- Image Data URI ---" << std::endl;
    std::ofstream testImage("test_image.png");
    // Write minimal PNG data (1x1 pixel transparent PNG)
    unsigned char pngData[] = {0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A};
    testImage.write(reinterpret_cast<const char*>(pngData), 8);
    testImage.close();

    std::string dataUri = Base64Image::encodeImageWithHeader("test_image.png", "image/png");
    std::cout << "Data URI (truncated): " << dataUri.substr(0, 100) << "..." << std::endl;

    // 7. Chunked encoding
    std::cout << "\n--- Chunked Encoding ---" << std::endl;
    std::vector<BYTE> largeData(3000);
    for (size_t i = 0; i < largeData.size(); i++) {
        largeData[i] = static_cast<BYTE>(i % 256);
    }

    std::vector<std::string> chunks = Base64Chunked::encodeChunked(largeData, 1000);
    std::cout << "Split into " << chunks.size() << " chunks" << std::endl;

    std::vector<BYTE> reassembled = Base64Chunked::decodeChunked(chunks);
    std::cout << "Reassembly: " << (reassembled == largeData ? "SUCCESS" : "FAILED") << std::endl;

    // 8. Validation
    std::cout << "\n--- Validation ---" << std::endl;
    std::string validBase64 = "SGVsbG8sIFdvcmxkIQ==";
    std::string invalidBase64 = "Invalid@Base64!";

    std::cout << "'" << validBase64 << "' is valid: "
              << (Base64Validator::isValidBase64(validBase64) ? "YES" : "NO") << std::endl;
    std::cout << "'" << invalidBase64 << "' is valid: "
              << (Base64Validator::isValidBase64(invalidBase64) ? "YES" : "NO") << std::endl;

    std::cout << "\n=== All Base64 Examples Completed ===" << std::endl;
    return 0;
}

Herramientas

Herramientas que suelen usarse con este ejemplo

Relacionado

Colecciones de ejemplos relacionadas

Relacionado

Temas relacionados

Guía de uso de la herramienta

Contexto para entender esta colección de ejemplos

Resumen

Use «Codificación/Decodificación Base64» para los ejercicios. Siga solo el bloque indicado; otras tarjetas tratan funciones y dependencias diferentes.

Elegir el ejemplo mínimo

Solo texto y límites. Base64 codifica, no cifra. Sin API, hash, claves ni autenticación; validador no apto como control de seguridad.

Cómo funciona

  1. 1Lea la tarjeta y siga los valores declarados. La página muestra código; no ejecuta este lenguaje.

Casos límite y errores frecuentes

  • Alcance del resultado

    Son expectativas derivadas del código, no salidas registradas de compilación o ejecución. Entrada interactiva, reloj, archivos, hilos, servicios y frameworks quedan fuera.

Ejemplos

1. Seguir Base64URLEncoder: text normalization

Cómo usarlo

En «Codificación/Decodificación Base64», localice Base64URLEncoder: text normalization. Use synthetic preexisting text="+/8=" y siga únicamente las operaciones indicadas en el mismo orden.

Resultado

Esperado: "-_8". Solo sustituciones y corte del relleno; sin API ni criptografía.

2. Seguir Base64Validator::isValidBase64

Cómo usarlo

En «Codificación/Decodificación Base64», localice Base64Validator::isValidBase64. Use synthetic text="";"====" y siga únicamente las operaciones indicadas en el mismo orden.

Resultado

Esperado: false;true. Caracteres/longitud, no posición del relleno:==== pasa, sin probar validez.

3. Seguir Base64Validator::cleanBase64String

Cómo usarlo

En «Codificación/Decodificación Base64», localice Base64Validator::cleanBase64String. Use synthetic text with space,tab,CR,LF around A and B y siga únicamente las operaciones indicadas en el mismo orden.

Resultado

Esperado: "AB". Quita cuatro espacios sin validar resto; sin descifrar.

Tema

Herramientas de cifrado clásico y moderno

Practica cifrados seguros para educación o CTF comparando transformaciones clásicas reversibles con cifrado moderno y sus límites de seguridad.

Tema

Flujo de cifrado y permisos para documentos

Protege archivos y campos seleccionados, abre PDF cifrados con credenciales autorizadas, configura o elimina permisos y comprueba la reversibilidad y la compatibilidad.

Tema

Paquetización AGEX y aceptación de entrega segura

Crea un paquete AGEX, revisa sus metadatos visibles, elige la protección de contraseña y firma, y verifica la entrega antes de compartirlo.

Tema

Revisión e integración de un componente de sorteos autoalojado

Revisa la configuración, las respuestas de ejemplo, la legibilidad y el diseño adaptable antes de entregar los archivos y las pruebas de integración del sorteo.

Inicio
Herramientas3216
Ejemplos593
Visualizaciones473
Visores de archivos103
Temas239
Etiquetas333
Imágenes, audio y video657

Herramientas de procesamiento, conversión y optimización de imágenes, audio y video

Matemáticas, fechas y finanzas647

Calculadoras, herramientas numéricas, lógica de fechas, estadística y finanzas

Diseño y color298

Herramientas de color, maquetación, gráficos, estilo visual y apoyo al diseño

Texto y redacción209

Herramientas de redacción, limpieza, formato, extracción y análisis de texto

Desarrollo y Web194

Utilidades para desarrolladores, red, depuración web y automatización

Conversión y codificación175

Herramientas de conversión de formatos, archivos, unidades, archivos comprimidos y codificación

Lanzamiento de un cuestionario de captación y entrega de contactos
Textos conmemorativos: revisión familiar y entrega para impresión
Hábitos privados: copia, restauración y revisión antes de compartir
De artículo a borradores de X y LinkedIn: revisión factual y entrega
Revisión de una lección Markdown interactiva y entrega sin conexión

© 2026 Elysia Tools. Todos los derechos reservados.

DocumentaciónPreciosAcerca deTérminos de servicioPolítica de privacidadCookiesContacto

Cryptography

Comparador de algoritmos hash

Aplica MD5, SHA-1, SHA-256, SHA-512, BLAKE2b y BLAKE3 a la misma entrada y compáralos lado a lado: longitud de salida, digest hex/Base64, estado de seguridad (roto/moderno) y benchmark de velocidad relativa. Útil para enseñanza, elegir algoritmo o verificar checksums.

Format Conversion

Cifrador/Descifrador de Texto

Cifrado y descifrado simple de texto usando varios algoritmos

Development

Inspector de cabeceras binarias Windows PE / Linux ELF / Mach-O (secciones, importaciones/exportaciones)

Sube un binario PE-COFF (.exe/.dll), ELF o Mach-O: analiza cabeceras DOS/PE/COFF/opcional, e_ident y tablas de ELF, y la cabecera y órdenes de carga de Mach-O; muestra punto de entrada/base de imagen, tabla de secciones con mapeo RVA↔offset, importaciones/exportaciones, símbolos dinámicos y reubicaciones, anotaciones multiarquitectura (x86/x86-64/ARM/AArch64/RISC-V), insignias ASLR/NX/PIE y un volcado hexadecimal inicial.

Security

Herramienta de cifrado AES

Cifra o descifra texto y archivos con AES-256-GCM y exporta paquetes basados en IV

Security

Cifrado / Descifrado RSA

Cifra texto con una clave pública RSA o descifra el texto cifrado con la clave privada correspondiente, usando padding OAEP (SHA-1 o SHA-256). Maneja mensajes largos por bloques. Las claves y datos se quedan en local. No se ofrece PKCS#1 v1.5 (Node lo deshabilita para descifrar por ataques Bleichenbacher).

Format Conversion

Cifrado Atbash

Codifica y decodifica texto usando el cifrado de sustitución Atbash (invierte el alfabeto)

Security

Generador de Hash

Generar valores hash (MD5, SHA1, SHA256, SHA512)

Format Conversion

Cifrado de Flujo RC4

Cifra y descifra texto usando el cifrado de flujo RC4 con una clave

C++

Ejemplos de Características de Escritorio Windows C++

Características específicas de escritorio Windows C++ incluyendo cuadros de diálogo de archivos, cuadros de mensaje y bandeja del sistema

C++

Operaciones de Archivos Windows - Ejemplos C++

Ejemplos completos de operaciones de archivos en C++ para plataforma Windows incluyendo E/S de texto, copia de archivos y recorrido de directorios

C++

Ejemplos de Serialización C++ Windows

Ejemplos de serialización de datos Windows C++ incluyendo serialización/deserialización JSON y análisis XML

C++

Procesamiento de Cadenas Windows - Ejemplos C++

Ejemplos completos de procesamiento de cadenas en C++ para plataforma Windows incluyendo concatenación, expresiones regulares y operaciones de reemplazo

C++

Ejemplos de Funciones Web C++ Windows

Ejemplos de desarrollo de servidor web C++ Windows incluyendo enrutamiento URL, middleware y servicio de archivos estáticos

Java

Ejemplos de Criptografía Android Java

Ejemplos de criptografía Android Java incluyendo cálculo de hash, encriptación AES y codificación Base64