教学服务系统

 找回密码
 立即注册
搜索
查看: 595|回复: 1

信息计算2019级1班21号杨宇

[复制链接]

10

主题

19

帖子

134

积分

注册会员

Rank: 2

积分
134
发表于 2022-6-2 21:19:21 | 显示全部楼层 |阅读模式
RSA加密和签名
#include <stdio.h>  
int candp(int a,int b,int c)
{

    int r=1;  

b=b+1;  

while(b!=1)  

{  

  r=r*a;  

  r=r%c;  

  b--;  

}  

return r;  

}  



int main()  

{  

int p,q,m,n,e,d,x,y,r;  

printf("请输入 p,q: ");   

scanf("%d%d",&p,&q);  

n=p*q;  

printf("n为%d\n",n);  



m=(p-1)*(q-1);

printf("m为%d\n",m);  



printf("请输入e: ");  

scanf("%d",&e);  

if(e<1||e>m)  

{  

  printf("e输入错误,请重新输入: ");  

  scanf("%d",&e);  

}  

d=1;  

while(((e*d)%m)!=1) d++;  

printf("计算出d为:%d\n",d);  

printf("加密请输入1,解密请输入2,输入其他退出\n");   

scanf("%d",&r);  

while(1)

{



  if(r==1)

  {

printf("请输入x: ");  //计算密文

scanf("%d",&x);  

y=candp(x,e,n);  

printf("密文为:%d\n",y);

  }

  else if(r==2)

{

printf("请输入密文: ");  

scanf("%d",&y);  

x=candp(y,d,n);  

printf("明文为%d\n",x);

  }

  else break;

printf("加密请输入1,解密请输入2,输入其他退出\n");   

scanf("%d",&r);

}

         return 0;

}






回复

使用道具 举报

10

主题

19

帖子

134

积分

注册会员

Rank: 2

积分
134
 楼主| 发表于 2022-6-2 21:23:16 | 显示全部楼层
ECC椭圆曲线加密

#include <iostream>
#define Ea 20
#define Eb 4
#define Ep 29
using namespace std;
int fast_pow(int a, int b, int c)
{
    int ans = 1;   
    a = a % c;   
    while (b != 0)
    {
        if (b & 1)
        {
            ans = (ans * a) % c;
        }
        b >>= 1;   
        a = (a * a) % c;  
    }
    return ans;
}
struct point {
    int x;
    int y;
    point():x(0),y(0){}
    point(int a, int b):x(a),y(b) { }
    bool operator==(const point& b) {
        return this->x == b.x && this->y == b.y;
    }
    point operator+(const point& b) {
        point result;
        if (*this == b) {
            long aaa = 3 * this->x * this->x + Ea;
            long bbb = 2 * this->y;
            //int m = (3 * this->x * this->x + Ea) / (2 * this->y) mod Ep;
            long m;
            if (aaa % bbb != 0) {
                m = ((aaa % Ep) * fast_pow(bbb, (Ep - 2), Ep)) % Ep;
            }
            else {
                m = (aaa / bbb) % Ep;
            }
            result.x = (m * m - 2 * this->x) % Ep;
            result.y = (m * (this->x - result.x) - this->y) % Ep;  
        }
        else {
            long aaa = b.y - this->y;
            long bbb = b.x - this->x;
            if (bbb == 0) {
                return point(13, 23);
            }
            long m;
            if (aaa % bbb != 0) {
                m = ((aaa % Ep) * fast_pow(bbb, (Ep - 2), Ep)) % Ep;
            }
            else {
                m = (aaa / bbb) % Ep;
            }
            result.x = (m * m - this->x - b.x) % Ep;
            result.y = (m * (this->x - result.x) - this->y) % Ep;
        }
        if (result.x < 0) {
            result.x += Ep;
        }
        if (result.y < 0) {
            result.y += Ep;
        }
        return result;
    }
    point operator-() {
        this->y = Ep - this->y;
        return *this;
    }
};
void getG(point x,int& G) {
    point Gx = x;
    G = 1;
    int num = 0;
    do{
        cout<<G<<"x:" << Gx.x << " " << Gx.y << endl;
        Gx = Gx + x;
        G++;
    } while (Gx.x != x.x);
    G++;
    cout << G << "x:" << Gx.x << " " << Gx.y << endl;
}
point getNG(point x,int n) {
    point result = x;
    for (int i = 1; i < n; ++i) {
        result = result + x;
    }

    return result;
}
void print(point p) {
    cout << "[" << " " << p.x << "," << p.y << " " << "]" << endl;
}
int main()
{

    int G;
    point* x=new point(13, 23);
    getG(*x, G);
    cout << "阶数为:" << G << endl;
    //求得G=36
    //第二步,选择一个私有密钥p(p<G)
    int p =25;
    //第三步,公开密钥K=p*(13,23)
    point K = getNG(*x, p);
    //将 曲线 基点 密钥 公开
    cout << "公开密钥:"<< endl;
    print(K);

    int msg = 13;
    cout << "需要加密的信息:" << msg << endl;;
    point M(13,23);
    int r = 6;
    point c1 = M;
    for (int i = 0; i < r; ++i) {
        c1 = c1 + K;
    }
    point c2=*x;
    for (int i = 1; i < r; ++i) {
        c2 = c2 + *x;
    }
    cout << "密文为:" << endl;;
    print(c1);
    print(c2);

    point c3;
    point c4=*x;
    p = p*r % G;
    for (int i = 1; i < p; ++i) {
        c4 = c4 + *x;
    }
    c3 = c1 + (-c4);
    cout << "明文为:" << c3.x << endl;
    return 0;
}



回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

教学服务系统

GMT+8, 2025-4-30 01:50 , Processed in 0.018918 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表