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

📄 flintpp.h

📁 rsa的c++实现,此程序实现使用公钥的加密
💻 H
📖 第 1 页 / 共 2 页
字号:
//*****************************************************************************/
//                                                                            */
//  Functions for arithmetic and number theory with large integers in C       */
//  Software supplement to the book "Cryptography in C and C++"               */
//  by Michael Welschenbach, published by Apress Berkeley CA, 2001            */
//                                                                            */
//  Module flintpp.h        Revision: 27.01.2002                              */
//                                                                            */
//  Copyright (C) 1998-2003 by Michael Welschenbach                           */
//  Copyright (C) 1998-2003 by Springer-Verlag Berlin, Heidelberg             */
//  Copyright (C) 2001-2003 by Apress L.P., Berkeley, CA                      */
//  Copyright (C) 2002-2003 by Wydawnictwa MIKOM, Poland                      */
//  Copyright (C) 2002-2003 by PHEI, P.R.China                                */
//  Copyright (C) 2002-2003 by InfoBook, Korea                                */
//  Copyright (C) 2002-2003 by Triumph Publishing, Russia                     */
//                                                                            */
//  All Rights Reserved                                                       */
//                                                                            */
//  The software may be used for noncommercial purposes and may be altered,   */
//  as long as the following conditions are accepted without any              */
//  qualification:                                                            */
//                                                                            */
//  (1) All changes to the sources must be identified in such a way that the  */
//      changed software cannot be misinterpreted as the original software.   */
//                                                                            */
//  (2) The statements of copyright may not removed or altered.               */
//                                                                            */
//  (3) The following DISCLAIMER is accepted:                                 */
//                                                                            */
//  DISCLAIMER:                                                               */
//                                                                            */
//  There is no warranty for the software contained on this CD-ROM, to the    */
//  extent permitted by applicable law. The copyright holders provide the     */
//  software `as is' without warranty of any kind, either expressed or        */
//  implied, including, but not limited to, the implied warranty of fitness   */
//  for a particular purpose. The entire risk as to the quality and           */
//  performance of the program is with you.                                   */
//                                                                            */
//  In no event unless required by applicable law or agreed to in writing     */
//  will the copyright holders, or any of the individual authors named in     */
//  the source files, be liable to you for damages, including any general,    */
//  special, incidental or consequential damages arising out of any use of    */
//  the software or out of inability to use the software (including but not   */
//  limited to any financial losses, loss of data or data being rendered      */
//  inaccurate or losses sustained by you or by third parties as a result of  */
//  a failure of the software to operate with any other programs), even if    */
//  such holder or other party has been advised of the possibility of such    */
//  damages.                                                                  */
//                                                                            */
//*****************************************************************************/
//
//  History
//
//  27.01.2002
//    Added member and friend functions lint2clint for export to type CLINT 
//
////////////////////////////////////////////////////////////////////////////////

#ifndef __FLINTPPH__
#define __FLINTPPH__            // flintpp.h is #included

//lint -wlib(1)

#if defined FLINTPP_ANSI
#include <limits>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <new>
#include <algorithm>
#if !defined __WATCOMC__
using namespace std;
#endif // #!defined __WATCOMC__
#else
#include <limits.h>
#include <iostream.h>
#include <fstream.h>
#include <iomanip.h>
#include <new.h>
#endif // #defined FLINTPP_ANSI

#ifndef __CPLUSPLUS__
#define __CPLUSPLUS__
#endif

#ifndef __cplusplus
#define __cplusplus
#endif

#define FLINTCPPHVMAJ   2 // Major version number of flintpp.cpp
#define FLINTCPPHVMIN   3 // Minor version number of flintpp.cpp
#define FLINTCOMPATMAJ  2 // Major version of flint.c required for flintpp.cpp
#define FLINTCOMPATMIN  1 // Minor version of flint.c required for flintpp.cpp

//lint -wlib(4)


// Include FLINT/C C-Library

#include "flint.h"


// Version control

#if ((FLINTCOMPATMIN > FLINT_VERMIN) || (FLINTCOMPATMAJ > FLINT_VERMAJ))
#error Versionsfehler: FLINTPP.H  not compatibel to FLINT.H
#endif


// LINT-Errors

enum LINT_ERRORS {
        E_LINT_OK     = 0,      // Everything O.K.
        E_LINT_EOF    = 0x0010, // File-IO-Error
        E_LINT_DBZ    = 0x0020, // Division by zero
        E_LINT_NHP    = 0x0040, // Heap-Error: new returned NULL-pointer
        E_LINT_OFL    = 0x0080, // Overflow in function/operator
        E_LINT_UFL    = 0x0100, // Underflow in function/operator
        E_LINT_VAL    = 0x0200, // Argument of function/operator not initialized
        E_LINT_INV    = 0x0200, // Argument of function/operator not initialized
        E_LINT_BOR    = 0x0400, // Base invalid
        E_LINT_MOD    = 0x0800, // Modulus even in mexp?m
        E_LINT_NPT    = 0x1000, // Argument is Null-pointer
        E_LINT_ERR    = 0x2000  // root or chinrem has no solution,
                                // else: unspecific error
};


// LINT-Exceptions

class LINT_Error                        // Abstract base class
{
 public:
  const char* function;
  int argno, lineno;
  virtual void debug_print (void) const = 0;  // Pure virtual function
  virtual ~LINT_Error() {function = 0;};
};

class LINT_DivByZero : public LINT_Error      // Division by Zero
{
 public:
  LINT_DivByZero (const char* const func, const int line);
  void debug_print (void) const;
};

class LINT_File : public LINT_Error     // File-IO error
{
 public:
  LINT_File (const char* const func, const int line);
  void debug_print (void) const;
};

class LINT_Init : public LINT_Error     // Argument not initialized
{
 public:
  LINT_Init (const char* const func, const int arg, const int line);
  void debug_print (void) const;
};

class LINT_Heap : public LINT_Error     // Heap-error in new
{
 public:
  LINT_Heap (const char* const func, const int line);
  void debug_print (void) const;
};

class LINT_OFL : public LINT_Error      // Overflow in function
{
 public:
  LINT_OFL (const char* const func, const int line);
  void debug_print (void) const;
};

class LINT_UFL : public LINT_Error      // Underflow in function
{
 public:
  LINT_UFL (const char* const func, const int line);
  void debug_print (void) const;
};

class LINT_Base : public LINT_Error     // Base invalid
{
 public:
  LINT_Base (const char* const func, const int line);
  void debug_print (void) const;
};

class LINT_Emod : public LINT_Error     // Modulus even in mexp?m
{
 public:
  LINT_Emod (const char* const func, const int line);
  void debug_print (void) const;
};

class LINT_Nullptr : public LINT_Error  // Argument is NULL-pointer
{
 public:
  LINT_Nullptr (const char* const func, const int arg, const int line);
  void debug_print (void) const;
};

class LINT_Mystic : public LINT_Error   // Unknown error ;-)
{
 public:
  LINT_Mystic (const char* const func, const int errcode, const int line);
  void debug_print (void) const;
};


class LintInit
{
 public:
  LintInit (void);
};

// The constructor LintInit() sets the ios-internal value
// ios::iword (flagsindex) (after initialization of LINT::flagsindex) to the
// default values of the LINT-package. Thus the default mode for stream
// output of LINT-objects is defined. A calling program can change the output
// mode by calling LINT manipulators (cf. manipulators like
// LINT::SetLintFlags (LINT::flags)).


// Macros for Internationalization of class LINT

#define ggT            gcd
#define xggT           xgcd
#define kgV            lcm
#define chinrest       chinrem
#define zweianteil     twofact


// Declaration of class LINT

class LINT
{
 public:

  // LINT-FRIENDS

  friend LintInit::LintInit (void);

  // Overloaded operators, implemented as friend functions

  friend const LINT operator+ (const LINT&, const LINT&);
  friend const LINT operator- (const LINT&, const LINT&);
  friend const LINT operator* (const LINT&, const LINT&);
  friend const LINT operator/ (const LINT&, const LINT&);
  friend const LINT operator% (const LINT&, const LINT&);
  friend const LINT operator<< (const LINT&, const int);
  friend const LINT operator>> (const LINT&, const int);

  // Logical functions

  friend const int operator== (const LINT&, const LINT&);
  friend const int operator!= (const LINT&, const LINT&);
  friend const int operator> (const LINT&, const LINT&);
  friend const int operator< (const LINT&, const LINT&);
  friend const int operator<= (const LINT&, const LINT&);
  friend const int operator>= (const LINT&, const LINT&);

  // Boolean functions

  friend const LINT operator^ (const LINT&, const LINT&);
  friend const LINT operator| (const LINT&, const LINT&);
  friend const LINT operator& (const LINT&, const LINT&);

  // Arithmetic

  friend const LINT add (const LINT&, const LINT&);
  friend const LINT sub (const LINT&, const LINT&);
  friend const LINT mul (const LINT&, const LINT&);
  friend const LINT sqr (const LINT&);
  friend const LINT divr (const LINT&, const LINT&, LINT&);
  friend const LINT mod (const LINT&, const LINT&);
  //friend const LINT mod2 (const LINT&, const USHORT);

  // Swapping

  friend void fswap (LINT&, LINT&);

  // Purging of LINT, overwriting with 0

  friend void purge (LINT&);

  // Modular arithmetic

  friend const LINT madd (const LINT&, const LINT&, const LINT&);
  friend const LINT msub (const LINT&, const LINT&, const LINT&);
  friend const LINT mmul (const LINT&, const LINT&, const LINT&);
  friend const LINT msqr (const LINT&, const LINT&);
  friend const LINT mexp (const LINT&, const LINT&, const LINT&);
  friend const LINT mexp (const USHORT, const LINT&, const LINT&);
  friend const LINT mexp (const LINT&, const USHORT, const LINT&);
  friend const LINT mexpkm (const LINT&, const LINT&, const LINT&);
  friend const LINT shift (const LINT&, const int);

  // Number theoretic friend functions

  friend const int isprime (const LINT&, const int noofsmallprimes = 302, const int iterations = 0);
  friend const unsigned int ld (const LINT&);
  friend const LINT gcd (const LINT&, const LINT&);
  friend const LINT xgcd (const LINT&, const LINT&, LINT&, int&, LINT&, int&);
  friend const LINT inv (const LINT&, const LINT&);
   friend const int twofact (const LINT&, LINT&);
  friend const LINT findprime (const USHORT);
  friend const LINT findprime (const USHORT, const LINT&);
  friend const LINT findprime (const LINT&, const LINT&, const LINT&);
  friend const int iseven (const LINT&);
  friend const int isodd (const LINT&);
  friend const int mequ (const LINT&, const LINT&, const LINT&);

  // Pseudorandom number generators

  friend LINT randl (const int);
  friend LINT randl (const LINT&, const LINT&);
  friend void seedl (const LINT&);
  friend LINT randBBS (const int);
  friend LINT randBBS (const LINT&, const LINT&);
  friend int seedBBS (const LINT&);

  // Conversion

  friend char* lint2str (const LINT&, const USHORT, const int = 0);
  friend UCHAR* lint2byte (const LINT&, int*);

⌨️ 快捷键说明

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