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

📄 flintpp.cpp

📁 rsa加密算法的c++实现,此程序实现利用公钥解密
💻 CPP
📖 第 1 页 / 共 5 页
字号:



const int mequ (const LINT& a, const LINT& b, const LINT& m)
{
  int err;
  if (a.status == E_LINT_INV) LINT::panic (E_LINT_INV, "mequ", 1, __LINE__);
  if (b.status == E_LINT_INV) LINT::panic (E_LINT_INV, "mequ", 2, __LINE__);
  if (m.status == E_LINT_INV) LINT::panic (E_LINT_INV, "mequ", 3, __LINE__);

  err = mequ_l (a.n_l, b.n_l, m.n_l);

  switch (err)
    {
      case 0:
      case 1:
        break;
      case E_CLINT_DBZ:
        LINT::panic (E_LINT_DBZ, "mequ", 3, __LINE__);
        break;
      default:
        LINT::panic (E_LINT_ERR, "mequ", err, __LINE__);
    }

  return err;
}


////////////////////////////////////////////////////////////////////////////////
//                    Pseudorandom number generators                          //
////////////////////////////////////////////////////////////////////////////////


// Linear congruential generator

void seedl (const LINT& seed)
{
  if (seed.status == E_LINT_INV) seed64_l (seed.n_l);
  else LINT::panic (E_LINT_INV, "seed64", 0, __LINE__);
}


LINT randl (const int l)
{
  LINT random;
  rand_l (random.n_l, MIN (l, (int)CLINTMAXBIT));
  random.status = E_LINT_OK;
  return random;
}


LINT randl (const LINT& rmin, const LINT& rmax)
{
  if (rmin.status == E_LINT_INV) LINT::panic (E_LINT_INV, "randl", 1, __LINE__);
  if (rmax.status == E_LINT_INV) LINT::panic (E_LINT_INV, "randl", 2, __LINE__);
  if (rmax < rmin) LINT::panic (E_LINT_INV, "randl", 1, __LINE__);

  LINT random;
  LINT t = rmax - rmin;
  USHORT l = (ld (rmin) + ld (rmax)) >> 1;

  rand_l (random.n_l, MIN (l, (int)CLINTMAXBIT));
  random.status = E_LINT_OK;

  if (random < rmin)
    {
      random += rmin;
    }

  if (random > rmax)
    {
      random = rmin + random % (t + 1);
    }

  Assert ((random >= rmin) && (random <= rmax));

  return random;
}




// Blum-Blum-Shub Generator

int seedBBS (const LINT& seed)
{
  if (seed.status == E_LINT_INV) LINT::panic (E_LINT_INV, "seedBBS", 0, __LINE__);
  return (seedBBS_l (seed.n_l));
}


LINT randBBS (const int l)
{
  LINT random;
  randBBS_l (random.n_l, MIN (l, (int)CLINTMAXBIT));
  random.status = E_LINT_OK;
  return random;
}


LINT randBBS (const LINT& rmin, const LINT& rmax)
{
  if (rmin.status == E_LINT_INV) LINT::panic (E_LINT_INV, "randBBS", 1, __LINE__);
  if (rmax.status == E_LINT_INV) LINT::panic (E_LINT_INV, "randBBS", 2, __LINE__);
  if (rmax < rmin) LINT::panic (E_LINT_INV, "randBBS", 1, __LINE__);

  LINT random;
  LINT t = rmax - rmin;
  USHORT l = (ld (rmin) + ld (rmax)) >> 1;

  randBBS_l (random.n_l, MIN (l, (int)CLINTMAXBIT));
  random.status = E_LINT_OK;

  if (random < rmin)
    {
      random += rmin;
    }

  if (random > rmax)
    {
      random = rmin + random % (t + 1);
    }

  Assert ((random >= rmin) && (random <= rmax));

  return random;
}

////////////////////////////////////////////////////////////////////////////////
//                         Input/Output                                       //
////////////////////////////////////////////////////////////////////////////////


long LINT::flagsindex;

// Initialization of class member setup (class LintStatus == E_LINT_INV)
// The initialization of LINT::setup causes a call to the constructor 
// LintInit(), which in turn initializes the static member flagsindex as 
// a pointer to ios-internal memory: 

LintInit LINT::setup; /*lint !e1502*/


// Constructor LintInit() sets the ios-internal variable ios::iword (flagsindex)
// to the LINT default values:

LintInit::LintInit (void)
{
  // Get index to status variable of type long in class ios
  LINT::flagsindex = ios::xalloc ();

  // Set default status in cout and cerr
  cout.iword (LINT::flagsindex) = cerr.iword (LINT::flagsindex) =
           LINT::lintshowlength | LINT::linthex | LINT::lintshowbase;
}


// Read static LINT status variable in ios for standard output stream cout

long LINT::flags (void)
{
  return cout.iword (flagsindex);
}


// Read static LINT status variable in ios for output stream ostream s

long LINT::flags (ostream& s)
{
  return s.iword (flagsindex);
}


// Set static LINT status variable in ios for output stream ostream s

long LINT::setf (ostream& s, long flag)
{
  long t = s.iword (flagsindex);

  // Flags lintdec, linthex, lintbin, lintoct are mutually exclusive:
  if (flag & LINT::lintdec)
    {
      s.iword (flagsindex) = 
        (t & ~LINT::linthex & ~LINT::lintoct & ~LINT::lintbin) | LINT::lintdec;
      flag ^= LINT::lintdec;
    }

  if (flag & LINT::linthex)
    {
      s.iword (flagsindex) =
        (t & ~LINT::lintdec & ~LINT::lintoct & ~LINT::lintbin) | LINT::linthex;
      flag ^= LINT::linthex;
    }

  if (flag & LINT::lintoct)
    {
      s.iword (flagsindex) = 
        (t & ~LINT::lintdec & ~LINT::linthex & ~LINT::lintbin) | LINT::lintoct;
      flag ^= LINT::lintoct;
    }
  
  if (flag & LINT::lintbin)
    {
      s.iword (flagsindex) = 
        (t & ~LINT::lintdec & ~LINT::lintoct & ~LINT::linthex) | LINT::lintbin;
      flag ^= LINT::lintbin;
    } 

  // All remaining flags are mutually compatible:
  s.iword (flagsindex) |= flag;

  return t;
}


// Set static LINT status variable in ios for standard output stream cout

long LINT::setf (long flag)
{
  return LINT::setf (cout, flag);
}


// Switch off status bits in ostream s

long LINT::unsetf (ostream& s, long flag)
{
  int t = s.iword (flagsindex);
  s.iword (flagsindex) = (t | flag) ^ flag;
  return t;
}


// Switch off status bits in stream cout

long LINT::unsetf (long flag)
{
  return LINT::unsetf (cout, flag);
}


// Reset of LINT status variable for stream ostream s

long LINT::restoref (ostream& s, long flag)
{
  int t = s.iword (flagsindex);
  LINT::unsetf (s, t);
  s.iword (flagsindex) = flag;
  return t;
}


// Reset of LINT status variable for standard stream ostream cout

long LINT::restoref (long flag)
{
  return LINT::restoref (cout, flag);
}


// Manipulators

ostream& LintHex (ostream& s)
{
  LINT::setf (s, LINT::linthex);
  return s;
}

ostream& LintDec (ostream& s)
{
  LINT::setf (s, LINT::lintdec);
  return s;
}

ostream& LintOct (ostream& s)
{
  LINT::setf (s, LINT::lintoct);
  return s;
}

ostream& LintBin (ostream& s)
{
  LINT::setf (s, LINT::lintbin);
  return s;
}

ostream& LintUpr (ostream& s)
{
  LINT::setf (s, LINT::lintuppercase);
  return s;
}

ostream& LintLwr (ostream& s)
{
  LINT::unsetf (s, LINT::lintuppercase);
  return s;
}

ostream& LintShowbase (ostream& s)
{
  LINT::setf (s, LINT::lintshowbase);
  return s;
}

ostream& LintNobase (ostream& s)
{
  LINT::unsetf (s, LINT::lintshowbase);
  return s;
}

ostream& LintShowlength (ostream& s)
{
  LINT::setf (s, LINT::lintshowlength);
  return s;
}

ostream& LintNolength (ostream& s)
{
  LINT::unsetf (s, LINT::lintshowlength);
  return s;
}


// Manipulators SetLintFlags, ResetLintFlags

ostream& _SetLintFlags (ostream& s, int flag)
{
  LINT::setf (s, flag);
  return s;
}

ostream& _ResetLintFlags (ostream& s, int flag)
{
  LINT::unsetf (s, flag);
  return s;
}

LINT_omanip <int> SetLintFlags (int flag)
{
  return LINT_omanip <int> (&_SetLintFlags, flag);
}

LINT_omanip <int> ResetLintFlags (int flag)
{
  return LINT_omanip <int> (&_ResetLintFlags, flag);
}


// Overloading of ostream operator << for ouput of LINT objects

ostream& operator << (ostream& s, const LINT& ln)
{
  unsigned short base = 16;
  long flags = LINT::flags (s);
  char* formatted_lint;

  if (ln.status == E_LINT_INV) LINT::panic (E_LINT_INV, "ostream operator<<", 0, __LINE__);

  if (flags & LINT::linthex)
    {
      base = 16;
    }
  else
    {
      if (flags & LINT::lintdec)
        {
          base = 10;
        }
      else
        {
          if (flags & LINT::lintoct)
            {
              base = 8;
            }
          else
            {
              if (flags & LINT::lintbin)
                {
                  base = 2;
                }
            }
        }
    }
 
  if (flags & LINT::lintshowbase)
    {
      formatted_lint = lint2str (ln, base, 1);
    }
  else
    {
      formatted_lint = lint2str (ln, base, 0);
    }

  if (flags & LINT::lintuppercase)
    {
      strupr_l (formatted_lint);
    }

  s << formatted_lint << flush;

  if (flags & LINT::lintshowlength)
    {
      long _flags = s.flags ();  //get current flag settings
      s.setf (ios::dec);         //lint !e641
      s << endl << ld (ln) << " bit\n" << endl;
      s.setf (_flags);           //restore flags
    }

  return s;
}


// Overloading of ifstream/ofstream operators >>/<< for file input/ouput 
// of LINT objects

ofstream& operator<< (ofstream& s, const LINT& ln)
{
  if (ln.status == E_LINT_INV) LINT::panic (E_LINT_INV, "ofstream operator <<", 0, __LINE__);
  for (int i = 0; i <= (int) DIGITS_L (ln.n_l); i++)
    {
      if (write_ind_ushort (s, ln.n_l[i]))
        {
          LINT::panic (E_LINT_EOF, "ofstream operator <<", 0, __LINE__);
        }
    }
  return s;
}


fstream& operator<< (fstream& s, const LINT& ln)
{
  if (ln.status == E_LINT_INV) LINT::panic (E_LINT_INV, "fstream operator <<", 0, __LINE__);
  for (int i = 0; i <= (int) DIGITS_L (ln.n_l); i++)
    {
      if (write_ind_ushort (s, ln.n_l[i]))
        {
          LINT::panic (E_LINT_EOF, "fstream operator <<", 0, __LINE__);
        }
    }
  return s;
}


ifstream& operator>> (ifstream& s, LINT& ln)
{
  if (read_ind_ushort (s, ln.n_l))
    {
      LINT::panic (E_LINT_EOF, "ifstream operator >>", 0, __LINE__);
    }

  if (DIGITS_L (ln.n_l) < CLINTMAXSHORT)
    {
      for (int i = 1; i <= (int) DIGITS_L (ln.n_l); i++)
        {
          if (read_ind_ushort (s, &ln.n_l[i]))
            {
              LINT::panic (E_LINT_EOF, "ifstream operator >>", 0, __LINE__);
            }
        }
    }
  if (vcheck_l (ln.n_l) == 0)    // be paranoid - check imported values!
    {
      ln.status = E_LINT_OK;
    }
  else
    {
      ln.status = E_LINT_INV;
    }
  return s;
}


fstream& operator>> (fstream&s, LINT& ln)
{
  if (read_ind_ushort (s, ln.n_l))
    {
      LINT::panic (E_LINT_EOF, "fstream operator >>", 0, __LINE__);
    }

  if (DIGITS_L (ln.n_l) < CLINTMAXSHORT)
    {
      for (int i = 1; i <= (int) DIGITS_L (ln.n_l); i++)
        {
          if (read_ind_ushort (s, &ln.n_l[i]))
            {
              LINT::panic (E_LINT_EOF, "fstream operator >>", 0, __LINE__);
            }
        }
    }
  if (vcheck_l (ln.n_l) == 0)
    { // Be paranoid ...
      ln.status = E_LINT_OK;
    }
  else
    {
      ln.status = E_LINT_INV;
    }
  return s;
}


// Conversion of type LINT to character string by lint2str

// lint2str as member function:

char* LINT::lint2str (const USHORT base, const int showbase) const
{
  if (status == E_LINT_INV) LINT::panic (E_LINT_INV, "lint2str", 0, __LINE__);
  return xclint2str_l (n_l, base, showbase);
}


// lint2str as friend function

char* lint2str (const LINT& ln, const USHORT  base, const int showbase)
{
  if (ln.status == E_LINT_INV) LINT::panic (E_LINT_INV, "lint2str", 0, __LINE__);
  return xclint2str_l (ln.n_l, base, showbase);
}


// Conversion of type LINT to byte array by lint2byte

// lint2byte as member function:

UCHAR* LINT::lint2byte (int* len) const
{
  if (status == E_LINT_INV) LINT::panic (E_LINT_INV, "lint2byte", 0, __LINE__);
  return clint2byte_l (n_l, len);
}


// lint2byte as friend function

UCHAR* lint2byte (const LINT& ln, int* len)
{
  if (ln.status == E_LINT_INV) LINT::panic (E_LINT_INV, "lint2byte", 0, __LINE__);
  return clint2byte_l (ln.n_l, len);
}


// LINT to CLINT export

// lint2clint as member function:

clint* LINT::lin

⌨️ 快捷键说明

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