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

📄 flintpp.cpp

📁 rsa加密算法的c++实现,此程序实现利用公钥解密
💻 CPP
📖 第 1 页 / 共 5 页
字号:
      case E_CLINT_OK:
        dif.status = E_LINT_OK;
        break;
      case E_CLINT_UFL:
        dif.status = E_LINT_UFL;
        break;
      default:
        LINT::panic (E_LINT_ERR, "sub", err, __LINE__);
    }

  return dif;
}


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

  if (&a == &b) //lint !e506
      err = sqr_l (a.n_l, p.n_l);
  else
      err = mul_l (a.n_l, b.n_l, p.n_l);

  switch (err)
    {
      case E_CLINT_OK:
        p.status = E_LINT_OK;
        break;
      case E_CLINT_OFL:
        p.status = E_LINT_OFL;
        break;
      default:
        LINT::panic (E_LINT_ERR, "mul", err, __LINE__);
    }

  return p;
}


const LINT sqr (const LINT& a)
{
  LINT p;
  int err;
  if (a.status == E_LINT_INV) LINT::panic (E_LINT_INV, "sqr", 1, __LINE__);

  err = sqr_l (a.n_l, p.n_l);

  switch (err)
    {
      case E_CLINT_OK:
        p.status = E_LINT_OK;
        break;
      case E_CLINT_OFL:
        p.status = E_LINT_OFL;
        break;
      default:
        LINT::panic (E_LINT_ERR, "sqr", err, __LINE__);
    }

  return p;
}


const LINT divr (const LINT& a, const LINT& b, LINT& r)
{
  LINT q;
  int err;
  if (a.status == E_LINT_INV) LINT::panic (E_LINT_INV, "divr", 1, __LINE__);
  if (b.status == E_LINT_INV) LINT::panic (E_LINT_INV, "divr", 2, __LINE__);

  err = div_l (a.n_l, b.n_l, q.n_l, r.n_l);

  switch (err)
    {
      case E_CLINT_OK:
        q.status = E_LINT_OK;
        r.status = E_LINT_OK;
        break;
      case E_CLINT_DBZ:
        LINT::panic (E_LINT_DBZ, "divr", 2, __LINE__);
        break;
      default:
        LINT::panic (E_LINT_ERR, "div", err, __LINE__);
    }

  return q;
}


const LINT mod (const LINT& a, const LINT& n)
{
  LINT r, junk;
  int err;
  if (a.status == E_LINT_INV)  LINT::panic (E_LINT_INV, "mod", 1, __LINE__);
  if (n.status == E_LINT_INV) LINT::panic (E_LINT_INV, "mod", 2, __LINE__);

  err = div_l (a.n_l, n.n_l, junk.n_l, r.n_l);

  switch (err)
    {
      case E_CLINT_OK:
        r.status = E_LINT_OK;
        break;
      case E_CLINT_DBZ:
        LINT::panic (E_LINT_DBZ, "mod", 2, __LINE__);
        break;
      default:
        LINT::panic (E_LINT_ERR, "mod", err, __LINE__);
    }

  return r;
}


/*const LINT mod2 (const LINT& a, const USHORT k)
{
  LINT r;
  int err;
  if (a.status == E_LINT_INV)  LINT::panic (E_LINT_INV, "mod2", 1, __LINE__);

  err = mod2_l (a.n_l, k, r.n_l);

  switch (err)
    {
      case E_CLINT_OK:
        r.status = E_LINT_OK;
        break;
      default:
        LINT::panic (E_LINT_ERR, "mod2", err, __LINE__);
    }

  return r;
}
*/

const LINT madd (const LINT& lr, const LINT& ln, const LINT& m)
{
  LINT sum;
  int err;
  if (lr.status == E_LINT_INV) LINT::panic (E_LINT_INV, "madd", 1, __LINE__);
  if (ln.status == E_LINT_INV) LINT::panic (E_LINT_INV, "madd", 2, __LINE__);
  if (m.status == E_LINT_INV) LINT::panic (E_LINT_INV, "madd", 3, __LINE__);

  err = madd_l (lr.n_l, ln.n_l, sum.n_l, m.n_l);

  switch (err)
    {
      case E_CLINT_OK:
        sum.status = E_LINT_OK;
        break;
      case E_CLINT_DBZ:
        LINT::panic (E_LINT_DBZ, "madd", 3, __LINE__);
        break;
      default:
        LINT::panic (E_LINT_ERR, "madd", err, __LINE__);
    }

  return sum;
}


const LINT msub (const LINT& lr, const LINT& ln, const LINT& m)
{
  LINT dif;
  int err;
  if (lr.status == E_LINT_INV) LINT::panic (E_LINT_INV, "msub", 1, __LINE__);
  if (ln.status == E_LINT_INV) LINT::panic (E_LINT_INV, "msub", 2, __LINE__);
  if (m.status == E_LINT_INV) LINT::panic (E_LINT_INV, "msub", 3, __LINE__);

  err = msub_l (lr.n_l, ln.n_l, dif.n_l, m.n_l);

  switch (err)
    {
      case E_CLINT_OK:
        dif.status = E_LINT_OK;
        break;
      case E_CLINT_DBZ:
        LINT::panic (E_LINT_DBZ, "msub", 3, __LINE__);
        break;
      default:
        LINT::panic (E_LINT_ERR, "msub", err, __LINE__);
    }

  return dif;
}


const LINT mmul (const LINT& lr, const LINT& ln, const LINT& m)
{
  LINT p;
  int err;
  if (lr.status == E_LINT_INV) LINT::panic (E_LINT_INV, "mmul", 1, __LINE__);
  if (ln.status == E_LINT_INV) LINT::panic (E_LINT_INV, "mmul", 2, __LINE__);
  if (m.status == E_LINT_INV) LINT::panic (E_LINT_INV, "mmul", 3, __LINE__);

  if (&lr == &ln) //lint !e506
    {
      err = msqr_l (lr.n_l, p.n_l, m.n_l);
    }
  else
    {
      err = mmul_l (lr.n_l, ln.n_l, p.n_l, m.n_l);
    }

  switch (err)
    {
      case E_CLINT_OK:
        p.status = E_LINT_OK;
        break;
      case E_CLINT_DBZ:
        LINT::panic (E_LINT_DBZ, "mmul", 3, __LINE__);
        break;
      default:
        LINT::panic (E_LINT_ERR, "mmul", err, __LINE__);
    }

  return p;
}


const LINT msqr (const LINT& lr, const LINT& m)
{
  LINT p;
  int err;
  if (lr.status == E_LINT_INV) LINT::panic (E_LINT_INV, "msqr", 1, __LINE__);
  if (m.status == E_LINT_INV) LINT::panic (E_LINT_INV, "msqr", 2, __LINE__);

  err = msqr_l (lr.n_l, p.n_l, m.n_l);

  switch (err)
    {
      case E_CLINT_OK:
        p.status = E_LINT_OK;
        break;
      case E_CLINT_DBZ:
        LINT::panic (E_LINT_DBZ, "msqr", 2, __LINE__);
        break;
      default:
        LINT::panic (E_LINT_ERR, "msqr", err, __LINE__);
    }

  return p;
}


const LINT mexp (const LINT& lr, const LINT& ln, const LINT& m)
{
  LINT pot;
  int err;
  if (lr.status == E_LINT_INV) LINT::panic (E_LINT_INV, "mexp", 1, __LINE__);
  if (ln.status == E_LINT_INV) LINT::panic (E_LINT_INV, "mexp", 2, __LINE__);
  if (m.status == E_LINT_INV) LINT::panic (E_LINT_INV, "mexp", 3, __LINE__);

  err = mexp_l (lr.n_l, ln.n_l, pot.n_l, m.n_l);

  switch (err)
    {
      case E_CLINT_OK:
        pot.status = E_LINT_OK;
        break;
      case E_CLINT_DBZ:
        LINT::panic (E_LINT_DBZ, "mexp", 3, __LINE__);
        break;
      default:
        LINT::panic (E_LINT_ERR, "mexp", err, __LINE__);
    }

  return pot;
}


const LINT mexp (const USHORT b, const LINT& ln, const LINT& m)
{
  LINT pot;
  int err;
  if (ln.status == E_LINT_INV) LINT::panic (E_LINT_INV, "mexp", 2, __LINE__);
  if (m.status == E_LINT_INV) LINT::panic (E_LINT_INV, "mexp", 3, __LINE__);
  
  if (m.isodd ())
    {
      err = wmexpm_l (b, ln.n_l, pot.n_l, m.n_l);
    }
  else
    {
      err = wmexp_l (b, ln.n_l, pot.n_l, m.n_l);
    }

  switch (err)
    {
      case E_CLINT_OK:
        pot.status = E_LINT_OK;
        break;
      case E_CLINT_DBZ:
        LINT::panic (E_LINT_DBZ, "mexp", 3, __LINE__);
        break;
      default:
        LINT::panic (E_LINT_ERR, "mexp", err, __LINE__);
    }

  return pot;
}


const LINT mexp (const LINT& lr, const USHORT e, const LINT& m)
{
  LINT pot;
  int err;
  if (lr.status == E_LINT_INV) LINT::panic (E_LINT_INV, "mexp", 1, __LINE__);
  if (m.status == E_LINT_INV) LINT::panic (E_LINT_INV, "mexp", 3, __LINE__);

  if (m.isodd())
    {
      err = umexpm_l (lr.n_l, e, pot.n_l, m.n_l);
    }
  else
    {
      err = umexp_l (lr.n_l, e, pot.n_l, m.n_l);
    }

  switch (err)
    {
      case E_CLINT_OK:
        pot.status = E_LINT_OK;
        break;
      case E_CLINT_DBZ:
        LINT::panic (E_LINT_DBZ, "mexp", 3, __LINE__);
        break;
      default:
        LINT::panic (E_LINT_ERR, "mexp", err, __LINE__);
    }

  return pot;
}

const LINT mexpkm (const LINT& lr, const LINT& ln, const LINT& m)
{
  LINT pot;
  int err;
  if (lr.status == E_LINT_INV) LINT::panic (E_LINT_INV, "mexpkm", 1, __LINE__);
  if (ln.status == E_LINT_INV) LINT::panic (E_LINT_INV, "mexpkm", 2, __LINE__);
  if (m.status == E_LINT_INV) LINT::panic (E_LINT_INV, "mexpkm", 3, __LINE__);

  err = mexpkm_l (lr.n_l, ln.n_l, pot.n_l, m.n_l);

  switch (err)
    {
      case E_CLINT_OK:
        pot.status = E_LINT_OK;
        break;
      case E_CLINT_MOD:
        LINT::panic (E_LINT_MOD, "mexpkm", 3, __LINE__);
        break;
      case E_CLINT_DBZ:
        LINT::panic (E_LINT_DBZ, "mexpkm", 3, __LINE__);
        break;
      default:
        LINT::panic (E_LINT_ERR, "mexpkm", err, __LINE__);
    }

  return pot;
}


const LINT shift (const LINT& a, const int noofbits)
{
  int err;
  if (a.status == E_LINT_INV) LINT::panic (E_LINT_INV, "shift", 1, __LINE__);

  LINT shft = a;
  err = shift_l (shft.n_l, noofbits);

  switch (err)
    {
      case E_CLINT_OK:
        shft.status = E_LINT_OK; 
        break;
      case E_CLINT_OFL:
        shft.status = E_LINT_OFL;
        break;
      case E_CLINT_UFL:
        shft.status = E_LINT_UFL;
        break;
      default:
        LINT::panic (E_LINT_ERR, ">>", err, __LINE__);
    }

  return shft;
}


////////////////////////////////////////////////////////////////////////////////
//           Number-theoretic friend functions                                //
////////////////////////////////////////////////////////////////////////////////


const unsigned int ld (const LINT& a)
{
  if (a.status == E_LINT_INV) LINT::panic (E_LINT_INV, "ld", 1, __LINE__);
  return ld_l (a.n_l);
}


const int isprime (const LINT& p, const int noofsmallprimes, const int iterations)
{
  if (p.status == E_LINT_INV) LINT::panic (E_LINT_INV, "isprime", 1, __LINE__);
  return (prime_l (p.n_l, noofsmallprimes, iterations));
}


const int iseven (const LINT& p)
{
  if (p.status == E_LINT_INV) LINT::panic (E_LINT_INV, "iseven", 1, __LINE__);
  return (ISEVEN_L (p.n_l));
}


const int isodd (const LINT& p)
{
  if (p.status == E_LINT_INV) LINT::panic (E_LINT_INV, "isodd", 1, __LINE__);
  return (ISODD_L (p.n_l)); 
}


const LINT gcd (const LINT& a, const LINT& b)
{
  LINT gcdiv, hlp;
  if (a.status == E_LINT_INV) LINT::panic (E_LINT_INV, "gcd", 1, __LINE__);
  if (b.status == E_LINT_INV) LINT::panic (E_LINT_INV, "gcd", 2, __LINE__);

  gcd_l (a.n_l, b.n_l, gcdiv.n_l);
  gcdiv.status = E_LINT_OK;

  return gcdiv;
}

const LINT xgcd (const LINT& a, const LINT& b , LINT& u, int& sign_u, LINT& v, int& sign_v)
{
  LINT g, hlp;
  if (a.status == E_LINT_INV) LINT::panic (E_LINT_INV, "xgcd", 1, __LINE__);
  if (b.status == E_LINT_INV) LINT::panic (E_LINT_INV, "xgcd", 2, __LINE__);

  xgcd_l (a.n_l, b.n_l, g.n_l, u.n_l, &sign_u, v.n_l, &sign_v);

  g.status = E_LINT_OK;
  u.status = E_LINT_OK;
  v.status = E_LINT_OK;

  return g;
}


const LINT inv (const LINT& a, const LINT& b)
{
  LINT invers, hlp;
  if (a.status == E_LINT_INV) LINT::panic (E_LINT_INV, "inv", 1, __LINE__);
  if (b.status == E_LINT_INV) LINT::panic (E_LINT_INV, "inv", 2, __LINE__);

  inv_l (a.n_l, b.n_l, hlp.n_l, invers.n_l);
  invers.status = E_LINT_OK;

  return invers;
}

const int twofact (const LINT& even, LINT& odd)
{
  if (even.status == E_LINT_INV) LINT::panic (E_LINT_INV, "twofact", 1, __LINE__);
  odd.status = E_LINT_OK;
  return (twofact_l (even.n_l, odd.n_l));
}


// Generation of a prime number p with 2^(l-1) <= p < 2^l

const LINT findprime (const USHORT l)
{
  return findprime (l, 1);
}


// Generation of a prime number p with 2^(l-1) <= p < 2^l
// and gcd (p - 1, f) = 1

const LINT findprime (const USHORT l, const LINT& f)
{
  if (0 == l || l > CLINTMAXBIT) LINT::panic (E_LINT_OFL, "findprime", 1, __LINE__);

  LINT pmin (0);
  LINT pmax (0);
  pmin.setbit (l - 1);
  pmax.setbit (l);
  --pmax;

  return findprime (pmin, pmax, f);
}


// Generation of a prime number p pmin <= p <= pmax
// and gcd (p - 1, f) = 1

const LINT findprime (const LINT& pmin, const LINT& pmax, const LINT& f)
{
  if (pmin.status == E_LINT_INV) LINT::panic (E_LINT_INV, "findprime", 1, __LINE__);
  if (pmax.status == E_LINT_INV) LINT::panic (E_LINT_INV, "findprime", 2, __LINE__);
  if (f.status == E_LINT_INV) LINT::panic (E_LINT_INV, "findprime", 3, __LINE__);

  if (pmin > pmax) LINT::panic (E_LINT_INV, "findprime", 1, __LINE__);

  // 0 < f must be odd
  if (f.iseven ()) LINT::panic (E_LINT_INV, "findprime", 3, __LINE__);

  LINT p = randBBS (pmin, pmax);
  LINT t = pmax - pmin;

  if  (p < pmin)
    {
      p += pmin;
    }

  if (p.iseven ())
    {
      ++p;
    }

  if (p > pmax)
    {
      p = pmin + p % (t + 1);
    }

  while ((gcd (p - 1, f) != 1) || !p.isprime ())
    {
      ++p;
      ++p;

      while (p > pmax)
        {
          p = pmin + p % (t + 1);

          if (p.iseven ())
            {
              ++p;
            }
        }
    }

  return p;
}

⌨️ 快捷键说明

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