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

📄 flintpp.cpp

📁 rsa算法的c++实现,该程序实现rsa密钥对的生成.此密钥用来加密和解密
💻 CPP
📖 第 1 页 / 共 5 页
字号:


#include "flintpp.h"

#if defined FLINTPP_ANSI
#define NOTHROW (nothrow)
#else
#define NOTHROW
#endif

#define NO_ASSERTS

#ifdef FLINT_DEBUG
#undef NO_ASSERTS
#define ASSERT_LOG_AND_QUIT
#include "_assert.h"
#endif

#ifdef NO_ASSERTS
#define Assert(a) (void)0
#endif

// Version control

//#define FLINTCPPVMAJ  2
//#define FLINTCPPVMIN  3

//#if ((FLINTCPPVMIN != FLINTCPPHVMIN) || (FLINTCPPVMAJ != FLINTCPPHVMAJ))
//#error Version error: FLINTPP.CPP not compatibel to FLINTPP.H
//#endif

//lint -wlib(0)
//lint -e537 (Don't complain about "repeated include files")

#include <stdlib.h>
#include <string.h>

//lint -wlib(4)


////////////////////////////////////////////////////////////////////////////////
//           Constructors                                                     //
////////////////////////////////////////////////////////////////////////////////

// Constructor 1
// Default-constructor without assigment
LINT::LINT (void)
{
  n_l = new NOTHROW CLINT;

  if (NULL == n_l)
    {
      panic (E_LINT_NHP, "Default constructor", 0, __LINE__);
    }

  status = E_LINT_INV;
}


// Constructor 2
// LINT is constructed from character string 
LINT::LINT (const char* const str, const int base)
{
  int error;

  n_l = new NOTHROW CLINT;
  if (NULL == n_l)
    {
      panic (E_LINT_NHP, "constructor 2", 0, __LINE__);
    }

  error = str2clint_l (n_l , (char*)str, base);

  switch (error)
   {
      case E_CLINT_OK:
        status = E_LINT_OK;
        break;
      case E_CLINT_NPT:
        status = E_LINT_INV;
        panic (E_LINT_NPT, "constructor 2", 1, __LINE__);
        break;
      case E_CLINT_OFL:
        status = E_LINT_OFL;
        panic (E_LINT_OFL, "constructor 2", 1, __LINE__);
        break;
      case E_CLINT_BOR:
        status = E_LINT_BOR;
        panic (E_LINT_BOR, "constructor 2", 2, __LINE__);
        break;
      default:
        status = E_LINT_INV;
        panic (E_LINT_ERR, "constructor 2", error, __LINE__);
   }
} //lint !e1541


// Constructor 3
// LINT is constructed from byte array with digits to base 2^8
// according to IEEE P1363, significance of bytes increasing from left to right 
LINT::LINT (const UCHAR* const bytevector, const int length)
{
  int error;

  if (NULL == bytevector)
    {
      panic (E_LINT_INV, "constructor 3", 1, __LINE__);
    }

  n_l = new NOTHROW CLINT;
  if (NULL == n_l)
    {
      panic (E_LINT_NHP, "constructor 3", 0, __LINE__);
    }

  error = byte2clint_l (n_l , (UCHAR*)bytevector, length);

  switch (error)
   {
      case E_CLINT_OK:
        status = E_LINT_OK;
        break;
      case E_CLINT_NPT:
        status = E_LINT_INV;
        panic (E_LINT_NPT, "constructor 3", 1, __LINE__);
        break;
      case E_CLINT_OFL:
        status = E_LINT_OFL;
        panic (E_LINT_OFL, "constructor 3", 1, __LINE__);
        break;
      default:
        status = E_LINT_INV;
        panic (E_LINT_ERR, "constructor 3", error, __LINE__);
   }
} //lint !e1541


// Constructor 4
// LINT is constructed from ASCII-string with C syntax.
// Syntax:
//   "0[x|X]{0123456789abcdefABCDEF}" : HEX number
//   "0[b|B]{01}"                     : Binary number
//   "{0123456789}" : Characters are interpreted as decimal digits
LINT::LINT (const char* const str)
{
  int error;

  n_l = new NOTHROW CLINT;
  if (NULL == n_l)
    {
      panic (E_LINT_NHP, "constructor 5", 0, __LINE__);
    }

  //lint -e668 -e613 (str is not NULL)
  if (strncmp (str, "0x", 2) == 0 || strncmp (str, "0X", 2) == 0)
    {
      error = str2clint_l (n_l, (char*)str+2, 16);
    }
  else
    {
      if (strncmp (str, "0b", 2) == 0 || strncmp (str, "0B", 2) == 0)
        {
          error = str2clint_l (n_l, (char*)str+2, 2);
        }
      else
        {
          error = str2clint_l (n_l, (char*)str, 10);
        }
    }

  switch (error)
    {
      case E_CLINT_OK:
        status = E_LINT_OK;
        break;
      case E_CLINT_NPT:
        status = E_LINT_INV;
        panic (E_LINT_NPT, "constructor 4", 1, __LINE__);
        break;
      case E_CLINT_OFL:
        status = E_LINT_OFL;
        panic (E_LINT_OFL, "constructor 4", 1, __LINE__);
        break;
      default:
        status = E_LINT_INV;
        panic (E_LINT_ERR, "constructor 4", error, __LINE__);
    }
} //lint !e1541 +e668


// Constructor 5
// LINT is constructed from LINT
LINT::LINT (const LINT& ln)
{
  if (ln.status == E_LINT_INV) panic (E_LINT_INV, "constructor 5", 1, __LINE__);

  n_l = new NOTHROW CLINT;
  if (NULL == n_l)
    {
      panic (E_LINT_NHP, "constructor 5", 0, __LINE__);
    }
  cpy_l (n_l, ln.n_l);
  status = ln.status;
} //lint !e1541


// Constructor 6
// LINT is constructed from int (w/o sign extension)
LINT::LINT (const signed int i)
{
  unsigned long ul;
  n_l = new NOTHROW CLINT;
  if (NULL == n_l)
    {
      panic (E_LINT_NHP, "constructor 8", 0, __LINE__);
    }
  status = E_LINT_OK;
  ul = (unsigned)abs(i);
  ul2clint_l (n_l, ul);
}


// Constructor 7
// LINT is constructed from long (w/o sign extension)
LINT::LINT (const signed long l)
{
  unsigned long ul;
  n_l = new NOTHROW CLINT;
  if (NULL == n_l)
    {
      panic (E_LINT_NHP, "constructor 9", 0, __LINE__);
    }
  status = E_LINT_OK;
  ul = (ULONG)abs(l);
  ul2clint_l (n_l, ul);
}


// Constructor 8
// LINT is constructed from unsigned char 
LINT::LINT (const unsigned char uc)
{
  unsigned long ul;
  n_l = new NOTHROW CLINT;
  if (NULL == n_l)
    {
      panic (E_LINT_NHP, "constructor 10", 0, __LINE__);
    }
  status = E_LINT_OK;
  ul = uc;
  ul2clint_l (n_l, ul);
}


// Constructor 9
// LINT is constructed from unsigned short
LINT::LINT (const unsigned short us)
{
  n_l = new NOTHROW CLINT;
  if (NULL == n_l)
    {
      panic (E_LINT_NHP, "constructor 11", 0, __LINE__);
    }
  status = E_LINT_OK;
  u2clint_l (n_l, us);
}


// Constructor 10
// LINT is constructed from unsigned int
LINT::LINT (const unsigned int ui)
{
  unsigned long ul;
  n_l = new NOTHROW CLINT;
  if (NULL == n_l)
    {
      panic (E_LINT_NHP, "constructor 12", 0, __LINE__);
    }
  status = E_LINT_OK;
  ul = ui;
  ul2clint_l (n_l, ul);
}


// Constructor 11
// LINT is constructed from unsigned long
LINT::LINT (const unsigned long ul)
{
  n_l = new NOTHROW CLINT;
  if (NULL == n_l)
    {
      panic (E_LINT_NHP, "constructor 13", 0, __LINE__);
    }
  status = E_LINT_OK;
  ul2clint_l (n_l, ul);
}


// Constructor 12
// LINT is constructed from type CLINT
LINT::LINT (const CLINT m_l)
{
  if (vcheck_l ((clint*)(m_l)) < 0)
    {
      panic (E_LINT_INV, "constructor 14", 1, __LINE__);
    }

  n_l = new NOTHROW CLINT;
  if (NULL == n_l)
    {
      panic (E_LINT_NHP, "constructor 14", 0, __LINE__);
    }

  cpy_l (n_l, (clint*)m_l);             //lint !e613 (m_l is not NULL)
  status = E_LINT_OK;
}                                       //lint !e1541


////////////////////////////////////////////////////////////////////////////////
//           Overloaded operators                                             //
////////////////////////////////////////////////////////////////////////////////

// Assignment

const LINT& LINT::operator= (const LINT& ln)
{
  if (ln.status == E_LINT_INV) panic (E_LINT_INV, "=", 1, __LINE__);

  if (&ln != this)                      // Don't copy object to itself
    {
      cpy_l (n_l, ln.n_l);
      status = ln.status;
    }
  return *this;
} /*lint !e1539*/


// Arithmetic

const LINT operator+ (const LINT& lm, const LINT& ln)
{
  LINT sum;
  int err;
  if (lm.status == E_LINT_INV) LINT::panic (E_LINT_INV, "+", 1, __LINE__);
  if (ln.status == E_LINT_INV) LINT::panic (E_LINT_INV, "+", 2, __LINE__);
  err = add_l (ln.n_l, lm.n_l, sum.n_l);
  switch (err)
    {
      case E_CLINT_OK:
        sum.status = E_LINT_OK;
        break;
      case E_CLINT_OFL:
        sum.status = E_LINT_OFL;
        break;
      default:
        LINT::panic (E_LINT_ERR, "+", err, __LINE__);
    }
  return sum;
}


const LINT operator- (const LINT& lm, const LINT& ln)
{
  LINT diff;
  int err;
  if (lm.status == E_LINT_INV) LINT::panic (E_LINT_INV, "-", 1, __LINE__);
  if (ln.status == E_LINT_INV) LINT::panic (E_LINT_INV, "-", 2, __LINE__);
  err = sub_l (lm.n_l, ln.n_l, diff.n_l);
  switch (err)
    {
      case E_CLINT_OK:
        diff.status = E_LINT_OK;
        break;
      case E_CLINT_UFL:
        diff.status = E_LINT_UFL;
        break;
      default:
        LINT::panic (E_LINT_ERR, "-", err, __LINE__);
    }

  return diff;
}


const LINT operator* (const LINT& lm, const LINT& ln)
{
  LINT prd;
  int err;
  if (lm.status == E_LINT_INV) LINT::panic (E_LINT_INV, "*", 1, __LINE__);
  if (ln.status == E_LINT_INV) LINT::panic (E_LINT_INV, "*", 2, __LINE__);

  if (&lm == &ln)     //lint !e506
      err = sqr_l (lm.n_l, prd.n_l);    // Use squaring function sqr_l for lm*lm
  else
      err = mul_l (lm.n_l, ln.n_l, prd.n_l);

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

  return prd;
}


const LINT operator/ (const LINT& lm, const LINT& ln)
{
  LINT quot;
  CLINT junk_l;
  int err;
  if (lm.status == E_LINT_INV) LINT::panic (E_LINT_INV, "/", 1, __LINE__);
  if (ln.status == E_LINT_INV) LINT::panic (E_LINT_INV, "/", 2, __LINE__);

  err = div_l (lm.n_l, ln.n_l, quot.n_l, junk_l);

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

  ZEROCLINT_L (junk_l);
  return quot;
}


const LINT operator% (const LINT& lm, const LINT& ln)
{
  LINT rem;
  CLINT junk_l;
  int err;
  if (lm.status == E_LINT_INV) LINT::panic (E_LINT_INV, "%", 1, __LINE__);
  if (ln.status == E_LINT_INV) LINT::panic (E_LINT_INV, "%", 2, __LINE__);

  err = div_l (lm.n_l, ln.n_l, junk_l, rem.n_l);

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

  ZEROCLINT_L (junk_l);
  return rem;
}


const LINT& LINT::operator++ (void) // Prefix operation
{
  int err;
  if (status == E_LINT_INV) panic (E_LINT_INV, "++", 0, __LINE__);

  err = inc_l (n_l);

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

  return *this;
}


const LINT LINT::operator++ (int i) // Postfix operation
{
  LINT tmp = *this;
  int err;
  if (status == E_LINT_INV) panic (E_LINT_INV, "++", 0, __LINE__);

  err = inc_l (n_l);

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

  return tmp;
} //lint !e715


const LINT& LINT::operator-- (void) // Prefix operation
{
  int err;
  if (status == E_LINT_INV) panic (E_LINT_INV, "--", 0, __LINE__);

  err = dec_l (n_l);

  switch (err)
    {
      case E_CLINT_OK:
        status = E_LINT_OK;
        break;
      case E_CLINT_UFL:
        status = E_LINT_UFL;
        break;
      default:
        panic (E_LINT_ERR, "--", err, __LINE__);
    }

  return *this;
}


const LINT LINT::operator-- (int i) // Postfix operation
{
  LINT tmp = *this;
  int err;
  if (status == E_LINT_INV) panic (E_LINT_INV, "--", 0, __LINE__);

  err = dec_l (n_l);

  switch (err)
    {
      case E_CLINT_OK:
        status = E_LINT_OK;
        break;
      case E_CLINT_OFL:
        status = E_LINT_UFL;
        break;
      default:
        panic (E_LINT_ERR, "--", err, __LINE__);
    }

  return tmp;
} //lint !e715


const LINT& LINT::operator+= (const LINT& ln)
{
  int err;
  if (status == E_LINT_INV) panic (E_LINT_INV, "+=", 0, __LINE__);
  if (ln.status == E_LINT_INV) panic (E_LINT_INV, "+=", 1, __LINE__);

⌨️ 快捷键说明

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