C++ AES/Base64加密算法

4 篇文章 1 订阅

AES/Base64算法

😊💕😁👍🙌🤦‍♀️🤦‍♂️🤷‍♀️🤷‍♂️😜💖😢🎶😎🤞✌👏💋🌹🎉🎂🤳🐱‍👤🐱‍🏍✨😃👀🐱‍🚀🐱‍🚀🐱‍🐉

base64编码&解码
std::string base64_encode(const std::string &str);
std::string base64_decode(const std::string &encoded_str);
std::string base64_encode(const std::string &stri,int begin, int end);
std::string base64_decode(const std::string &encoded_str, int begin, int end);

#include <string>
static const std::string base64_alphabet =
    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static inline bool is_base64(unsigned char c) { return (isalnum(c) || (c == '+') || (c == '/')); }
std::string base64_encode(const std::string &str, int begin, int end){
    std::string encoded_str;
    int i = 0;
    int j = 0;
    unsigned char char_arr3[3];
    unsigned char idx_arr4[4];
    for (int k = begin; k < end; k++) {
        char_arr3[i++] = str[k];
        if (i == 3) {
            idx_arr4[0] = (char_arr3[0] & 0xFC) >> 2;
            idx_arr4[1] = ((char_arr3[0] & 0x03) << 4) + ((char_arr3[1] & 0xF0) >> 4);
            idx_arr4[2] = ((char_arr3[1] & 0x0F) << 2) + ((char_arr3[2] & 0xC0) >> 6);
            idx_arr4[3] = char_arr3[2] & 0x3F;
            for (i = 0; i < 4; i++) {
                encoded_str += base64_alphabet[idx_arr4[i]];
            }
            i = 0;
        }
    }
    if (i) {
        for (j = i; j < 3; j++) {
            char_arr3[j] = '\0';
        }
        idx_arr4[0] = (char_arr3[0] & 0xFC) >> 2;
        idx_arr4[1] = ((char_arr3[0] & 0x03) << 4) + ((char_arr3[1] & 0xF0) >> 4);
        idx_arr4[2] = ((char_arr3[1] & 0x0F) << 2) + ((char_arr3[2] & 0xC0) >> 6);
        for (j = 0; j < i + 1; j++) {
            encoded_str += base64_alphabet[idx_arr4[j]];
        }
        while (i++ < 3) {
            encoded_str += '=';
        }
    }
    return encoded_str;
}

std::string base64_encode(const std::string &str) {
    return base64_encode(str, 0, str.size());
}
std::string base64_decode(const std::string &encoded_str, int begin, int end) {
    int len = end;
    int i = 0;
    int j = 0;
    int idx = 0;
    unsigned char idx_array_4[4], char_array_3[3];
    std::string str;
    while ((end-- > begin) && (encoded_str[idx] != '=') && is_base64(encoded_str[idx])) {
        idx_array_4[i++] = encoded_str[idx];
        idx++;
        if (i == 4) {
            for (i = 0; i < 4; i++) {
                idx_array_4[i] = base64_alphabet.find(idx_array_4[i]);
            }
            char_array_3[0] = (idx_array_4[0] << 2) + ((idx_array_4[1] & 0x30) >> 4);
            char_array_3[1] = ((idx_array_4[1] & 0xF) << 4) + ((idx_array_4[2] & 0x3C) >> 2);
            char_array_3[2] = ((idx_array_4[2] & 0x3) << 6) + idx_array_4[3];
            for (i = 0; (i < 3); i++) {
                str += char_array_3[i];
            }
            i = 0;
        }
    }
    if (i) {
        for (j = 0; j < i; j++) {
            idx_array_4[j] = base64_alphabet.find(idx_array_4[j]);
        }
        char_array_3[0] = (idx_array_4[0] << 2) + ((idx_array_4[1] & 0x30) >> 4);
        char_array_3[1] = ((idx_array_4[1] & 0xF) << 4) + ((idx_array_4[2] & 0x3C) >> 2);
        for (j = 0; (j < i - 1); j++) {
            str += char_array_3[j];
        }
    }
    return str;
}
std::string base64_decode(const std::string &encoded_str) {
    return base64_decode(encoded_str, 0, encoded_str.size());
}

AES-256bit加密揭秘&base64编码
std::string Encode(const std::string &str);
std::string Decode(const std::string &encode_str);
int Encode(const std::string &str, const std::string &pkey, char v, std::string *encode_str);
int Decode(const std::string &encode_str, const std::string &pkey, std::string *decode_str);

#include <iostream>
#include <string>
#include <hex.h>
#include <aes.h>
#include <modes.h>
#include <exception>
#include <stdexcept>
#include <string.h>
//设置32位字符串,可填充256bit,AES-256 最高级别的对称加密算法
static const std::string kDefaultPrivateKey = "Ws6d2nx12dfearohlQQrw^J#skRz1KI";
static void FillKey(byte * key, int size, const std::string& pkey){
	int length = size <= pkey.size() ? size : pkey.size();
	memcpy(key, pkey.data(), length);
}

int Encode(const std::string &str, const std::string &pkey, std::string *encode_str) {
  std::string strCipher = "";
  try{
      byte key[32] = {0};
      FillKey(key, 32, pkey);;
      CryptoPP::ECB_Mode<CryptoPP::AES>::Encryption e;
      e.SetKey(key, sizeof(key));
      CryptoPP::StringSource(str, true,
               new CryptoPP::StreamTransformationFilter(e, new CryptoPP::StringSink(strCipher)));
      if(strCipher.empty()) return -1;
      std::string tmp = std::move(base64_encode(strCipher));
      encode_str->swap(tmp);
  }catch(const std::exception& ex){
      spdlog::error("Encode error:{}", ex.what());
      return -1;
  }
  return 0;
}
int Decode(const std::string &encode_str,
                      const std::string &pkey,
                      std::string *decode_str) {
  try{
    byte key[32] = {0};
    FillKey(key, 32, pkey);
    CryptoPP::ECB_Mode<CryptoPP::AES>::Decryption d;
    d.SetKey(key, sizeof(key));
    CryptoPP::StringSource s(encode_str, true, new CryptoPP::StreamTransformationFilter(
                                            d, new CryptoPP::StringSink(*decode_str)));
  }catch(const std::exception& ex){
     spdlog::error("Decode error:{}", ex.what());
     return -1;
  }
  return 0;
}

std::string Encode(const std::string &str) {
  std::string strCipher;
  if(0 == Encode(str, kDefaultPrivateKey, &strCipher)){
      return std::move(strCipher);
  }
  return "";
}

std::string Decode(const std::string &encode_str) {
  std::string strRecovered;
  if(0 == Decode(base64_decode(encode_str), kDefaultPrivateKey, &strRecovered)){
     return std::move(strRecovered);
   }
  return "";
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值