⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 big_code.cpp

📁 含有多种公开密钥算法、多种块加密、多种数据流加密、多种HASH函数、多种CheckSum校验、多种MAC校验等几十种加密算法的程序
💻 CPP
字号:
/************************************************** BigInt Encoding/Decoding Source File           ** (C) 1999-2002 The Botan Project                **************************************************/#include <botan/bigint.h>#include <botan/numthry.h>#include <botan/hex.h>#include <cctype>namespace Botan {u32bit encoded_size(const BigInt& n, Encoding code)   {   static const double LOG_2_BASE_10 = 0.30102999566;   if(code == Binary)      return ((n.bits() + 7) / 8);   else if(code == Hexadecimal)      return 2 * ((n.bits() + 7) / 8);   else if(code == Octal)      return ((n.bits() + 2) / 3);   else if(code == Decimal)      return (u32bit)((n.bits() * LOG_2_BASE_10) + 1);   else throw Invalid_Argument("Cannot encode BigInt with unknown base");   }void encode(byte output[], const BigInt& n, Encoding code)   {   if(code == Binary)      n.binary_encode(output);   else if(code == Hexadecimal)      {      SecureVector<byte> binary = n.binary_encode();      for(u32bit j = 0; j != binary.size(); j++)         Hex_Encoder::encode(binary[j], output + 2*j);      }   else if(code == Octal)      {      BigInt copy = n;      const u32bit output_size = encoded_size(n, Octal);      for(u32bit j = 0; j != output_size; j++)         {         output[output_size - 1 - j] = (copy % 8) + '0';         copy >>= 3;         }      }   else if(code == Decimal)      {      BigInt copy = n;      BigInt remainder;      copy.set_sign(Positive);      const u32bit output_size = encoded_size(n, code);      for(u32bit j = 0; j != output_size; j++)         {         divide(copy, 10, copy, remainder);         output[output_size - 1 - j] = remainder.word_at(0) + '0';         if(copy.is_zero())            break;         }      }   else      throw Invalid_Argument("Cannot encode BigInt with unknown base");   }SecureVector<byte> encode(const BigInt& n, Encoding code)   {   SecureVector<byte> output(encoded_size(n, code));   encode(output, n, code);   if(code != Binary)      for(u32bit j = 0; j != output.size(); j++)         if(output[j] == 0)            output[j] = '0';   return output;   }BigInt decode(const SecureVector<byte>& buf, Encoding code)   {   return decode(buf, buf.size(), code);   }BigInt decode(const byte buf[], u32bit length, Encoding code)   {   BigInt r;   if(code == Binary)      r.binary_decode(buf, length);   else if(code == Hexadecimal)      {      SecureVector<byte> hex;      for(u32bit j = 0; j != length; j++)         if(Hex_Decoder::is_valid(buf[j]))            hex.append(buf[j]);      u32bit offset = (hex.size() % 2);      SecureVector<byte> binary(hex.size() / 2 + offset);      if(offset)         {         byte temp[2] = { '0', hex[0] };         binary[0] = Hex_Decoder::decode(temp);         }      for(u32bit j = offset; j != binary.size(); j++)         binary[j] = Hex_Decoder::decode(hex+2*j-offset);      r.binary_decode(binary);      }   else if(code == Decimal || code == Octal)      {      const u32bit RADIX = ((code == Decimal) ? 10 : 8);      const BigInt multiply(RADIX);      for(u32bit j = 0; j != length; j++)         {         byte x = buf[j] - '0';         if(x >= RADIX)            {            if(RADIX == 10)               throw Invalid_Argument("BigInt: Invalid decimal string");            else               throw Invalid_Argument("BigInt: Invalid octal string");            }         r = multiply * r + x;         }      }   else      throw Invalid_Argument("Cannot decode BigInt with unknown base");   return r;   }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -