?? rsa.h
字號:
/***************"vlong.h"********************/
#ifndef __VLONG_H__
#define __VLONG_H__
#ifndef NULL
#define NULL 0
#endif
#ifndef DWORD
#define DWORD unsigned long
#endif
// Macros for doing double precision multiply
#define BPU ( 8*sizeof(DWORD) ) // 一個字節位 DWORD
#define lo(x) ( ((DWORD)(x)) & (DWORD)((((DWORD)1)<<(BPU/2))-((DWORD)1)) ) // lower half of DWORD
#define hi(x) ( ((DWORD)(x)) >> (BPU/2) ) // 生 1/2
#define lh(x) ( ((DWORD)(x)) << (BPU/2) ) // 實施
// Provides storage allocation and index checking
class flex_unit
{
public:
DWORD n; // used units (read-only)
flex_unit();
~flex_unit();
void clear(); // set n to zero
DWORD get( DWORD i ) const; // 獲取 ith DWORD
void set( DWORD i, DWORD x ); // 設置 ith DWORD
void reserve( DWORD x ); // storage hint
// Time critical routine
void fast_mul( flex_unit &x, flex_unit &y, DWORD n );
//private: //lchen modi
DWORD * a; // array of units
DWORD z; // units allocated
};
class vlong_value : public flex_unit
{
public:
DWORD share; // share count, used by vlong to delay physical copying
long is_zero() const;
DWORD bit( DWORD i ) const;
void setbit( DWORD i );
void clearbit( DWORD i );
DWORD bits() const;
long cf( vlong_value& x ) const;
long product( vlong_value &x ) const;
void shl();
long shr(); // result is carry
void shr( DWORD n );
void add( vlong_value& x );
void xor( vlong_value& x );
void and( vlong_value& x );
void subtract( vlong_value& x );
void init( DWORD x );
void copy( vlong_value& x );
DWORD to_unsigned(); // Unsafe conversion to DWORD
vlong_value();
void mul( vlong_value& x, vlong_value& y );
void divide( vlong_value& x, vlong_value& y, vlong_value& rem );
};
class vlong // very long integer - can be used like long
{
public:
// Standard arithmetic operators
friend vlong operator +( const vlong& x, const vlong& y );
friend vlong operator -( const vlong& x, const vlong& y );
friend vlong operator *( const vlong& x, const vlong& y );
friend vlong operator /( const vlong& x, const vlong& y );
friend vlong operator %( const vlong& x, const vlong& y );
friend vlong operator ^( const vlong& x, const vlong& y );
friend vlong pow2( DWORD n );
friend vlong operator &( const vlong& x, const vlong& y );
friend vlong operator <<( const vlong& x, DWORD n );
vlong& operator +=( const vlong& x );
vlong& operator -=( const vlong& x );
vlong& operator >>=( DWORD n );
// Standard comparison operators
friend long operator !=( const vlong& x, const vlong& y );
friend long operator ==( const vlong& x, const vlong& y );
friend long operator >=( const vlong& x, const vlong& y );
friend long operator <=( const vlong& x, const vlong& y );
friend long operator > ( const vlong& x, const vlong& y );
friend long operator < ( const vlong& x, const vlong& y );
// Absolute value
friend vlong abs( const vlong & x );
// Construction and conversion operations
vlong ( DWORD x=0 );
vlong ( const vlong& x );
~vlong();
friend DWORD to_unsigned( const vlong &x );
vlong& operator =(const vlong& x);
// Bit operations
DWORD bits() const;
DWORD bit(DWORD i) const;
void setbit(DWORD i);
void clearbit(DWORD i);
vlong& operator ^=( const vlong& x );
vlong& operator &=( const vlong& x );
vlong& ror( DWORD n ); // single bit rotate
vlong& rol( DWORD n ); // single bit rotate
friend long product( const vlong & x, const vlong & y ); // parity of x&y
void load( DWORD * a, DWORD n ); // 讀值, a[0]
void store( DWORD * a, DWORD n ) const; // low level save, a[0] is lsw
void load( char * a, DWORD n ); //自己添加
void store( char * a, DWORD n ) const; //自己添加
//private:
class vlong_value * value;
long negative;
long cf( const vlong & x ) const;
void docopy();
friend class monty;
};
vlong modexp( const vlong & x, const vlong & e, const vlong & m ); // m 必須已添加
vlong gcd( const vlong &X, const vlong &Y ); // greatest common denominator
vlong modinv( const vlong &a, const vlong &m ); // modular inverse
vlong monty_exp( const vlong & x, const vlong & e, const vlong & m );
vlong monty_exp( const vlong & x, const vlong & e, const vlong & m, const vlong &p, const vlong &q );
class rng
{
public:
virtual vlong next()=0;
};
class vlstr
{
public:
virtual void put( const vlong & x )=0;
virtual vlong get()=0;
};
vlong lucas ( vlong P, vlong Z, vlong k, vlong p ); // P^2 - 4Z != 0
vlong sqrt( vlong g, vlong p ); // 平方根的模 p
#endif// __VLONG_H__
/***************"vlong.h"********************/
/***************"rsa.h"********************/
#ifndef __RSA_H__
#define __RSA_H__
//#include "vlong.h"
#define LEVEL 64
//當LEVEL=32表示1024位,LEVEL=64表示2048,LEVEL=128表示4096位的RSA
#define VL LEVEL * 2
/*一次加密的數據塊大小應該和m的比特相同,即使比它小就生成的密文都是m的比特
加/解密的數據VL*char字節,它最大長度就是LEVEL的長度,
但是因為它要比m小,所以定LEVEL*DWORD比特既絕對安全又可以使可以構造的plain小于m*/
struct PK //公開密鑰
{
DWORD m[LEVEL]; //公開密鑰中要用大整數用DWORD表示
};
struct SK //私人密鑰
{
DWORD p[LEVEL / 2];
DWORD q[LEVEL / 2];
};
struct MessageDollop //要加密的數據塊,先用BPK加密,再用ASK加密。
{
char text[LEVEL * 2]; //要加密的消息內容,規定是LEVEL*2字節
DWORD digital_ID[4]; //數字簽名的標識(可以使他固定,用私鑰加密它時必須小于m),經過散列算法處理后...
DWORD messagePackage_ID[4]; //整條消息的標識(隨機生成),可以判斷收方是否收到這條消息
DWORD messageDollop_ID[4];//這個消息塊的ID(由message_ID隨機生成),把整個消息包的個塊的messageDollop_ID進行異或后必須等于message_ID
char time[20]; //記錄這條消息發出的時間,防止別人將我以前的消息發個收件人
char disuse[LEVEL * 2 - 22 * 4]; //無用的數據
char randCount[16]; //隨機數,當requires=false時就要重新進行加密,就用這個數的改變使得它requires=true
DWORD nil[1]; //必須為零,這樣就可以使得加密的大整數小于m了,但不能保證加密的數用另一個密鑰加密就可以小于m了
};
/*如何進行數字簽名呢?
《密聊》通過RSA實現的了消息通訊安全功能包括:
1. 身份驗證,使收件人確信發件人就是他或她就是公開密鑰所所對應的那個人;
2. 機密性,確保只有預期的收件人能夠閱讀郵件;
3. 完整性,確保消息在傳輸過程中沒有被更改;
4. 消息到達確認,發件人確認收件人收到了消息。
*/
/*
傳輸協議格式為:
[head(16bit)]:[ID(16bit)]:[n(16bit)]:[data(...)]
其中head為發送的消息類型,ID為消息接收者的標志,n為明文的實際字節數,data為談話內容。
消息類型包括:建立連接,退出連接,接收加密消息,發送公鑰,接受成功的消息回復
ID可以為重要的消息,一般的等等
*/
//head收到的消息類型
#define HEAD_TEXT 1 //收到的是正文
#define HEAD_REVERT_TEXT 2 //發出的正文就收到的是回復
#define HEAD_DISCONNECTION 4 //收到的是斷開提示
#define HEAD_CLAIM_PUBLIC_KEY 8 //對方的請求獲得公鑰
#define HEAD_REVERT_PUBLIC_KEY 16 //收到對方發過來的公鑰
#define HEAD_DIGITAL_SIGNATURE 32 //對方的數字簽名,看看我這里有沒有它的公鑰,可以在請求獲得它的公鑰
#define HEAD_ZAIXIANBIAOJI 64 //在線通知
#define HEAD_VERSION 128 //收到對方的版本通知,當前版本為1
#define HEAD_SENDFILE 256 //發送文件給對方
#define HEAD_SYSTEM_MESSAGE 1024 //系統消息
//HEAD_DIGITAL_SIGNATURE的子類型
#define HEAD_DIGITAL_SIGNATURE_YES 1 //合格的數字簽名
#define HEAD_DIGITAL_SIGNATURE_NO 2 //不合格的數字簽名
//HEAD_SENDFILE的子類型
#define HEAD_SENDFILE_ENQUIRY 1 //詢問對方需要文件嗎?
#define HEAD_SENDFILE_CONCENT 2 //允許
#define HEAD_SENDFILE_NO_CONCENT 4 //拒絕
#define HEAD_SENDFILE_CONCENT_RECEIVE 8 //拒絕
#define HEAD_SENDFILE_STOP 16 //中止發送或接收
#define HEAD_SENDFILE_VERSION_UPDATE 32 //文件傳送版本處理
#define HEAD_SENDFILE_SUCCEED 64 //發送成功
#define HEAD_SENDFILE_NO_SUCCEED 128 //發送失敗
//HEAD_DISCONNECTION的子類型
#define HEAD_DISCONNECTION_INFORM 1 //對方斷開連接通知,我也要斷開了
#define HEAD_DISCONNECTION_CLOSE 4 //對方關閉密聊
#define DATA_LENGTH 1024 * 4 //正文的最大長度,data是他的兩倍
struct MessagePackage //發送的消息數據包,它由許多消息塊和一些其他信息組成
{
int head; //發送消息的類型
int ID; //類型中的子類的標識
int n; //明文的實際字節數
char data[DATA_LENGTH * 2];//加密后的數據,一次最多發送4K字節,由許多消息塊組成
};
class public_key
{
public:
public_key(); //構造函數為了使得requires為1,派生類private_key也會調用這個基類中的構造函數的
void encrypt(MessagePackage &package);//加密消息包,正文的長度為package.n
void encrypt(MessageDollop &dollop); //加密消息塊
//要求plain必須小于m
vlong encrypt( const vlong& plain ); // Requires 0 <= plain < m
void PK_to_vlong(PK pk);
void vlong_to_PK(PK &pk);
public:
void set_requires(int req);
int get_requires();
int requires; //判斷加密的數是否小于m
vlong m, e;
};
class private_key : public public_key
{/*private_key是從public_key派生出來的類*/
public:
void create(); //生成m, e, p, q; 安全級別是2048位
void decrypt(MessagePackage &package);//解密消息包,正文的長度為package.n
void decrypt(MessageDollop &dollop); //解密消息塊
//要求plain必須小于m
vlong decrypt( const vlong& cipher );// Requires 0 <= cipher < m
void SK_to_vlong(SK sk);
void vlong_to_SK(SK &sk);
public:
vlong p, q;
// r1 and r2 should be null terminated random strings
// each of length around 35 characters (for a 500 bit modulus)
};
#endif
/***************"rsa.h"********************/
/***************"prime.h"********************/
#ifndef __PRIME_H__
#define __PRIME_H__
//#include "vlong.h"
class prime_factory
{
public:
DWORD np;
DWORD *pl;
prime_factory( DWORD MP = 2000 ); // sieve size
~prime_factory();
vlong find_prime( vlong & start );
long make_prime( vlong & r, vlong &k, const vlong & rmin );
};
long is_probable_prime( const vlong &p );
#endif
/***************"prime.h"********************/
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -