#include<cstdio>
#include<cmath>
#include<iostream>
#include<time.h>
using namespace std;
struct zuobiao
{
int x;
int y;
};
int EX_gcd(int a, int b, int &s,int &t)//无问题
{
if (a == 0 && b == 0) {
return -1;
}
if (b == 0) {
s = 1;
t = 0;
return a;
}
int d = EX_gcd(b, a%b, t,s);
t -= a / b*s;
return d;
}
int f1(zuobiao P,zuobiao q,int a,int p) //求λ的值 这里的除法a/b=a*b^-1(modp)
{ int k,c,b,e,t;
if ((P.x==q.x)&&(P.y==q.y)) { //此处只有加法规则的第三步,无(1)(2)
c = ((3 * q.x*q.x + a)%p+p)%p;
b= (2 *P.y);
EX_gcd(b, p, e,t);
while (e < 0) {
e = (e + p)% p;
}
k = (c*e+p)%p;
//cout << k << endl;
}
else {
c = ((q.y - P.y) % p + p ) % p;
b = ((q.x - P.x) % p + p) % p;
EX_gcd(b, p, e,t);
while (e < 0) {
e = (e + p) % p;
}
k = (c*e+p)%p;
}
/*cout << "输出k的值:" << endl;
cout << k << endl;*/
return k;
}
zuobiao add(zuobiao p,zuobiao g,int a,int n,int b) //abel群内的加法运算 a为曲线方程a,n为乘数,b为素数p
{
int k;
int e;
for (int i = 0; i < n-1; i++){
k = f1(p, g, a,b);
e = p.x;
p.x = (k*k - p.x - g.x+b)%b;
while (p.x < 0) {
p.x = p.x + b;
}
p.y = (k*(e - p.x) - p.y + b) % b;
while (p.y < 0) {
p.y = p.y + b;
}
}
return p;
}
int qiujie(zuobiao g,int a,int b) //g为生成元,n为g的阶,a为椭圆曲线方程y^2=x^3+a*x+b中的a,b为大素数
{
int n;
zuobiao p;
p = g;
int k;
int e;
int i;
for ( i = 1; i < 10000; i++) {
k = f1(p, g, a, b);
e = p.x;
p.x = (k*k - p.x - g.x + b) % b;
while (p.x < 0) {
p.x = p.x + b;
}
p.y = (k*(e - p.x) - p.y + b) % b;
while (p.y < 0) {
p.y = p.y + b;
}
if (p.x == g.x&&p.y == g.y) {
n = i; //此时n即为生成元的阶
break;
}
}
if (i == 10000) {
cout << "g不为该椭圆曲线的一个生成元,请重新举例!" << endl;
}
return n;
}
void encrypt(zuobiao g,zuobiao P,zuobiao m,int a,int r,int p)
{
zuobiao c1, c2;
c1 = g;
c1 = add(c1, g, a, r, p);
cout << "输出密文c1的值:" << endl;
cout << c1.x << " " << c1.y << endl;
c2 = P;
c2 = add(c2, P, a, r, p);
c2 = add(m, c2, a, 2, p);
cout << "输出密文c2的值:" << endl;
cout << c2.x << " " << c2.y << endl;
}
void decipher(zuobiao c2,zuobiao c1,int a,int d,int b) //g为生成元,c1 c2为密文,d为私钥,r为随机数,b为大素数
{
zuobiao p,m;
p = c1;
p = add(p, c1, a, d, b);
p.y = -p.y;
int k,e;
k = f1(c2, p, a, b);
e = c2.x;
m.x = ((k*k - c2.x - p.x)%b+ b) % b;
m.y = ((k*(e - m.x) - c2.y)%b + b) % b;
cout << "解密得到的明文为:" << endl;
cout << m.x << " " << m.y << endl;
}
int main()
{
int a, b; //椭圆曲线方程为y^2=x^3+a*x+b
cout << "请输入椭圆曲线方程a和b的值:" << endl;
cin >> a >> b;
int p;
cout << "请给定一个素数p:" << endl;
cin >> p;
if ((4 * a ^ 3 + 27 * b ^ 2) % p == 0) {
cout << "输入的p不满足条件!" << endl;
}
srand((unsigned)time(NULL));
int d; // 随机选择一个整数d;
//d = rand() % (n- 2)+2;
int n;//n为g的阶
cout << "请输入d:" << endl;
cin >> d; //d为私钥
zuobiao g;
cout << "请给定一个生成元G" << endl;
cin >> g.x >> g.y;
//n = qiujie(g, a, p);
zuobiao P = g;
P=add(P, g, a, d,p);
cout<<"输出P的值:"<<endl;
cout << P.x<<" "<<P.y << endl;
/*zuobiao P;
cout << "请输入公钥p的值:" << endl;
cin >> P.x >> P.y;
//cout << P.x << P.y << endl;*/
zuobiao m;//明文m
cout << "请输入明文m:" << endl;
cin >> m.x >> m.y;
int r;//随机数r
//r=rand()%n;
cout << "请输入r:" << endl;
cin >> r;
encrypt(g, P, m, a, r, p);
cout << "*************************" << endl;
cout << "下面为解密过程" << endl;
zuobiao c1, c2;
cout << "请输入密文c1:" << endl;
cin >> c1.x >> c1.y;
cout << "请输入密文c2:" << endl;
cin >> c2.x >> c2.y;
decipher(c2, c1, a, d, p);
return 0;
}
#include<cmath>
#include<iostream>
#include<time.h>
using namespace std;
struct zuobiao
{
int x;
int y;
};
int EX_gcd(int a, int b, int &s,int &t)//无问题
{
if (a == 0 && b == 0) {
return -1;
}
if (b == 0) {
s = 1;
t = 0;
return a;
}
int d = EX_gcd(b, a%b, t,s);
t -= a / b*s;
return d;
}
int f1(zuobiao P,zuobiao q,int a,int p) //求λ的值 这里的除法a/b=a*b^-1(modp)
{ int k,c,b,e,t;
if ((P.x==q.x)&&(P.y==q.y)) { //此处只有加法规则的第三步,无(1)(2)
c = ((3 * q.x*q.x + a)%p+p)%p;
b= (2 *P.y);
EX_gcd(b, p, e,t);
while (e < 0) {
e = (e + p)% p;
}
k = (c*e+p)%p;
//cout << k << endl;
}
else {
c = ((q.y - P.y) % p + p ) % p;
b = ((q.x - P.x) % p + p) % p;
EX_gcd(b, p, e,t);
while (e < 0) {
e = (e + p) % p;
}
k = (c*e+p)%p;
}
/*cout << "输出k的值:" << endl;
cout << k << endl;*/
return k;
}
zuobiao add(zuobiao p,zuobiao g,int a,int n,int b) //abel群内的加法运算 a为曲线方程a,n为乘数,b为素数p
{
int k;
int e;
for (int i = 0; i < n-1; i++){
k = f1(p, g, a,b);
e = p.x;
p.x = (k*k - p.x - g.x+b)%b;
while (p.x < 0) {
p.x = p.x + b;
}
p.y = (k*(e - p.x) - p.y + b) % b;
while (p.y < 0) {
p.y = p.y + b;
}
}
return p;
}
int qiujie(zuobiao g,int a,int b) //g为生成元,n为g的阶,a为椭圆曲线方程y^2=x^3+a*x+b中的a,b为大素数
{
int n;
zuobiao p;
p = g;
int k;
int e;
int i;
for ( i = 1; i < 10000; i++) {
k = f1(p, g, a, b);
e = p.x;
p.x = (k*k - p.x - g.x + b) % b;
while (p.x < 0) {
p.x = p.x + b;
}
p.y = (k*(e - p.x) - p.y + b) % b;
while (p.y < 0) {
p.y = p.y + b;
}
if (p.x == g.x&&p.y == g.y) {
n = i; //此时n即为生成元的阶
break;
}
}
if (i == 10000) {
cout << "g不为该椭圆曲线的一个生成元,请重新举例!" << endl;
}
return n;
}
void encrypt(zuobiao g,zuobiao P,zuobiao m,int a,int r,int p)
{
zuobiao c1, c2;
c1 = g;
c1 = add(c1, g, a, r, p);
cout << "输出密文c1的值:" << endl;
cout << c1.x << " " << c1.y << endl;
c2 = P;
c2 = add(c2, P, a, r, p);
c2 = add(m, c2, a, 2, p);
cout << "输出密文c2的值:" << endl;
cout << c2.x << " " << c2.y << endl;
}
void decipher(zuobiao c2,zuobiao c1,int a,int d,int b) //g为生成元,c1 c2为密文,d为私钥,r为随机数,b为大素数
{
zuobiao p,m;
p = c1;
p = add(p, c1, a, d, b);
p.y = -p.y;
int k,e;
k = f1(c2, p, a, b);
e = c2.x;
m.x = ((k*k - c2.x - p.x)%b+ b) % b;
m.y = ((k*(e - m.x) - c2.y)%b + b) % b;
cout << "解密得到的明文为:" << endl;
cout << m.x << " " << m.y << endl;
}
int main()
{
int a, b; //椭圆曲线方程为y^2=x^3+a*x+b
cout << "请输入椭圆曲线方程a和b的值:" << endl;
cin >> a >> b;
int p;
cout << "请给定一个素数p:" << endl;
cin >> p;
if ((4 * a ^ 3 + 27 * b ^ 2) % p == 0) {
cout << "输入的p不满足条件!" << endl;
}
srand((unsigned)time(NULL));
int d; // 随机选择一个整数d;
//d = rand() % (n- 2)+2;
int n;//n为g的阶
cout << "请输入d:" << endl;
cin >> d; //d为私钥
zuobiao g;
cout << "请给定一个生成元G" << endl;
cin >> g.x >> g.y;
//n = qiujie(g, a, p);
zuobiao P = g;
P=add(P, g, a, d,p);
cout<<"输出P的值:"<<endl;
cout << P.x<<" "<<P.y << endl;
/*zuobiao P;
cout << "请输入公钥p的值:" << endl;
cin >> P.x >> P.y;
//cout << P.x << P.y << endl;*/
zuobiao m;//明文m
cout << "请输入明文m:" << endl;
cin >> m.x >> m.y;
int r;//随机数r
//r=rand()%n;
cout << "请输入r:" << endl;
cin >> r;
encrypt(g, P, m, a, r, p);
cout << "*************************" << endl;
cout << "下面为解密过程" << endl;
zuobiao c1, c2;
cout << "请输入密文c1:" << endl;
cin >> c1.x >> c1.y;
cout << "请输入密文c2:" << endl;
cin >> c2.x >> c2.y;
decipher(c2, c1, a, d, p);
return 0;
}