Large arithmetic and RSA algorithm

zhaozj2021-02-16  50

(To test the email of the code to WWB206@163.com)

I don't busy a few days ago, so I remembered the encryption algorithm, but RSA encryption was relying on a large number of operations, and the mainstream RSA algorithm was established at 512 bits to 1024. The most existing computer data type is 64 (INT64), so I have composed a large number of CXWORD to implement 1024-bit large numbers. Basic thinking is to store the array of DWORD [32], which is implemented as follows. (To speed up the running speed, all functions are inline) / ****************************************** ****************************** / file: // WORD number of long header file: CXWord.hfile: // author: wwb206 @ 163.comfile: // Version: 1.0 (2004.2.17) File: // Description: Will the large number as an N-based array, for the current 32-bit system, N can take the file: // value 2 32 times, 0x00000000 <> 0xfffffffff / *********************************************** ************************* / file: // # if defined WWB_XWORD2004021711025632100 # ifndef WWB_XWORD2004021711025632100 # define WWB_XWORD2004021711025632100file:! // # define WWB_XWORDDEBUGfile: // The number of groups, indicating that the maximum number of representations is 2 ^ (32 * 32) = 2 ^ 1024, that is, 1024 digits const DWORD XWORDLEN = 32; class cxword {private: DWORD DATA [xwordlen]; // Reserved [] other public: inline int getlen () const; inline CXWord LOffset (int value); inline CXWord operator (const CXWord value) const; inline CXWord operator (const DWORD value) const; inline CXWord operator- (const CXWord value) const Inline Cxword Operator- (const DWord value) const; inline cxword settor * (const dword value) const; inline cxword operator / (const cxword value) Con st; inline CXWord operator / (const DWORD value) const; inline CXWord operator% (const CXWord value) const; inline DWORD operator% (const DWORD value) const; inline CXWord operator = (const CXWord value); inline CXWord operator = ( Const dword value); inline cxword operator << (const dword bitcount); inline cxword operator >> (Const DWORD BITCOUNT);

inline bool operator> (const DWORD value) const; inline bool operator> (const CXWord value) const; inline bool operator <(const DWORD value) const; inline bool operator <(const CXWord value) const; inline bool operator> = ( const DWORD value) const; inline bool operator> = (const CXWord value) const; inline bool operator <= (const DWORD value) const; inline bool operator <= (const CXWord value) const; inline bool operator == (const CXWord value) const; inline bool operator == (const DWORD value) const; inline CXWord sqrt () const; inline CString Format16 () const; inline CString Format10 () const; inline void operator (); inline void operator - () Inline cxword (); inline cxword (cstring value); inline ~ cxword () {;}}; file: // dword chartonum (char C);

Inline DWord Chartonum (CAAR C) {Case '0': Case '1': Case '2': Case '3': Case '4': Case '5': Case '6': Case ' 7 ': Case' 8 ': Case' 9 ': Return 0 C-'0'; Case 'A': Case 'B': Case 'C': Case 'D': Case 'E': Case 'f ': Return 10 c-'a';} cstring str1 = C; MessageBox (0, "incorrect characters", str1, mb_ok | mb_iconstop); return 0;} cxword :: cxword () {for (DWORD D) = 0; D data [d] = 0;} inline cxword :: cxword (cstring value) {cxword tmp; * this = 0; file: // remove the empty color of the middle CSTRING STR1 = Value; value = ""; for (DWORD D = 0; D

Value.makeupper (); if (value.left (2) == "0x") {value.delete (0, 2); for (DWORD D = 0; D d; d2--) TMP = TMP * 16; * this = * this TMP;}} else {for (INT D = Value.getLength () - 1; D> = 0; D -) {TMP = Chartonum (Value.getat (D)); for (DWORD D2 = 0; D2 <(Value.getLength () - 1-d); D2 ) TMP = TMP * 10;

* this = * this TMP;}}} cstring cxword :: format16 () const {cstring str1, str2; int dlen = this-> getlen (); for (int D = 0; D data [d]); str2 = str1 str2;} str2 = "0x" str2; return str2;}

CString cxword :: format10 () const {cstring str1, str2; cxword residue, quotient, dividend = (* this), TMP = (* this); // 数, commercial, divisor, temporary I = 0; while (DivideD) > = 10) {i ; quotient = divident / 10; residue = dividend - quotient * 10; // <10 str1.format ("% 01d", Residue.Data [0]); if (i% 10 == 0 ) str1 = "," str1; str2 = str1 str2; dividend = quotient;} str1.format ("% 01d", dividend; str2 = str1 str2; return str2;}

Inline Bool Cxword :: Operator == (const cxword value) const {for (DWORD D = 0; D data [d]) Return False; Return True;} inline book cxword :: operator == (const dword value) const {for (DWord D = 1; D Data [D]! = 0) Return False; if (this -> DATA [0] == value) Return true; Else Return false;} inline cxword cxword :: operator = (const cxword value) {for (DWORD D = 0; D data [d ] = value.data [d]; return * this;} inline cxword cxword :: operator = (const dword value) {data [0] = value; for (DWORD D = 1; D getlen (); maxresult.data [(rlen 1) / 2] = 1; File: // Possible maximum IF (rlen> = 1) minResult.data [(RLEN-1) / 2] = 1;

While (true) {result = (maxResult minResult) / 2; if (Result * Result> (* this)) {maxResult = result;} else if (Result * Result <= (* this)) {x x = result 1; IF (XTMP * XTMP <= * this) {minResult = Result;} else break; // Return Result;}

Return Result;}; inline cxword cxword :: Operator / (const cxword value) const {cxword result, maxresult, minResult; if ((* this)

Int rlen = this-> getlen () - value.getlen (); file: // for (DWord D = 0; D = 1) minResult.data [rlen-1] = 1; while {result = (maxResult minResult) / 2; if (Result * Value> (* this)) {MaxResult = Result;} else if (result * value <= (* this)) {if (* this) - result * value> = value) {minResult = result;} else break; // Return Result;}

} Return Result;} inline cxword cxword :: operator% (const cxword value) const {cxword result, tmp; tmp = (* this) / value; result = ((* this) - tmp * value); Return Result;} Inline cxword cxword :: Operator / (const dword value) const {// xyzk / w /// Int Datalen; for (DATALEN = xwordlen; datalen> 0; datalenten ---) IF (Data [DataLen-1]! = 0) Break; unsigned __INT64 DIVIDEND; // Demodes DWORD Residue, quotient; //, remainder, business, divisor

DWORD DQWS = DATALEN-1; // Current Bits CXWORD XQUOTIENT, XTEMP = * this;

While (TRUE) {if (xtemp.data [dqws]

Xtemp.Data [DQWS] = 0; xTemp.data [DQWS-1] = Residue; xquotient.data [DQWS-1] = quotient; dqws--;}} else // xtemp.data [dqws]> = value { DividEnd = xtemp.data [dqws]; residue = divident% value; quotient = Dividend / value;

XQUOTIENT.DATA [DQWS] = quotient xquotient.data [dqws]; xtemp.data [dqws] = Residue;}} Return xquotient;

Inline dword cxword :: operator% (const dword value) const {cxword w1; w1 = * this / value; w1 = * this - (w1 * value); return w1.data [0];

} // a b // b C d //

Inline cxword cxword :: Operator (const cxword value) const {cxword result; unsigned __INT64 x, carry = 0; // carry flag for (DWORD D = 0; D DATA [D] Carry; Carry = x >> 32; Result.data [D] = (x << 32) >> 32;} if (carry! = 0) {# # IFDEF WWB_XWORDDEBUG THROW ("exceeds xword range"); #ndif for (DWord D = 0; D

x = (unsigned __INT64) Value this-> Data [0]; carry = x >> 32; Result.Data [0] = (x << 32) >> 32; for (DWORD D = 1; D data [d] carry; carry = x >> 32; result.data [d] = (x << 32) >> 32;} if (carry! = 0) {#ifDef wwb_xworddebug throw ("exceeds xword range"); #ndif for (DWord D = 0; D > 31); if ((Data [Digit] << (31-D2)) >> 31) == 0) // Is 0 {DATA [Digit] = (Data [Digit] | (1L << D2)); // turn this to 1 Data [Digit] = (Data [Digit] & (0xffffffff << (D2))); // Change the previous one to 0 for (DWORD D3 = 0 D3

Inline Bool CXWORD :: Operator> Const {for (DWORD D = 1; D Data [D]> 0) Return True; Return (Data [0]> Value );} Inline Bool Cxword :: Operator> Const {for (int D = xwordlen-1; D> = 0; D -) {IF (Data [D]> Value.Data [D]) Return true; Else IF (Data [D] Data [D]> 0) Return False; Return (Data [0] = 0; D -) {IF (Data [D] Value.Data [D]) Return False;} Return False } Inline Bool CXWORD :: Operator> = (const dword value) const {for (DWORD D = 1; D Data [D]> 0) Return True; Return (Data [0 ]> = value);} Inline Bool CXWORD :: Operator> = (const cxword value) const {for (int D = xwordlen-1; d> = 0; D -) {IF (Data [D]> Value. Data [D]) Return True; Else IF (Data [D] Data [D]> 0) Return False Return (Data [0] <= Value);} Inline Bool CXWORD :: Operator <= (const cxword value) const {for (int D = xwordlen-1; D> = 0; D -) {IF (data [D]> Value.Data [D]) Return False; Else IF (Data [D]

TMP = Value * Data [D]; // for (DWORD D2 = 0; D2 xwordlen- 1-i; i--) IF (Tmp.Data [i]> 0) throw ("exceeds xword range"); #ndiffor (DWORD D2 = 0; (D2

Tmp.Data [xWordlen-1-D2] = tmp.data [xwordlen-1-d-d2];} for (DWORD D3 = 0; D3

Result = result TMP;

}}} Inline cxword cxword :: operator * (const dword value) const {cxword result; unsigned __INT64 x, carry = 0; // carry flag for (DWORD D = 0; D > 32); carry = (x >> 32);} IF (carry! = 0) { #ifdef wwb_xworddebug throw ("Beyond XWORD Range"); #ndif for (DWORD D = 0; D = Value.Data [D]) Result.Data [D] = TMP.DATA [ D] -Value.Data [D]; Else {file: // Borrow Minuend = 1; minuend = (minuend << 32) TMP.Data [D]; // not minuend << 31 Result.data [D ] = Minuend - value.data [d]; for (DWORD D2 = D 1; D2

}}}}}} Inline cxword cxword :: Operator- (const dword value) const {cxword result; if (* this = value) Result.data [0] = data [0] -Value; Else {Unsigned __INT64 TMP; TMP = 1; TMP = (TMP << 32); / / 32 Not 31 TMP = TMP DATA [0] - Value; Result.Data [0] = TMP; for (DWORD D = 1; D 0) { For (DWORD D2 = 1; D2 0; i -) {IF (Data [i-1]! = 0) Break;} return i;} inline cxword cxword :: Loffset (int value) {ix> = xwordlen-1) {for (int i = 0; i 0) {for (int I = xwordlen -1; i> = value; i - data [i] = data [i-value]; for (i = 0; i

#ENDIF

转载请注明原文地址:https://www.9cbs.com/read-24360.html

New Post(0)