#include myint64g_n=0; myint64g_d=0; myint64g_w=0; intg_blocksize=0; intg_blocksize_src=1; myint64pow_i_old(myint64..." /> #include myint64g_n=0; myint64g_d=0; myint64g_w=0; intg_blocksize=0; intg_blocksize_src=1; myint64pow_i_old(myint64..." />

[原创]rsa的数学原理与实例 | 宜武汇-ag真人国际厅网站

#include "rsa.h"

#include

#include

 

myint64 g_n = 0;

 

myint64 g_d = 0;

 

myint64 g_w = 0;

 

int g_blocksize = 0;

 

int g_blocksize_src = 1;

 

myint64 pow_i_old(myint64 a, myint64 b, myint64 p) {

    myint64 res = 1;

    for (myint64 i = 0; i < b; i )

    {

        res = (res * a) % p;

        if (res == 0)            //caution here: avert to overlow

        {

            break;

        }

    }

 

    return res;

 

 

myint64 pow_i(myint64 x, myint64 n, myint64 p)

    if (n == 0)

    {

        return 1;

    }

    myint64 temp = pow_i((x * x) % p, n / 2, p); //递归计算(x*x)^[n/2]

    if ((n & 1) != 0) //判断n的奇偶性

    {

        temp = (temp * x) % p;

    }

    return temp;

 

 

myint64 pow_ii(myint64 a, myint64 b, myint64 g_n) {

    myint64 res = 1;

    for (myint64 i = 0; i < b; i )

    {

        res = (res * a) % g_n;

        if (res == 0)            //caution here: avert to overlow

        {

        }

    }

 

    return res;

 

 

int ismutualprime(myint64 n, myint64 m) {

    return 1;

 

int isprimenumber(myint64 n) {

    for (myint64 i = 2; i < n; i )

    {

        myint64 quotient = n % i;

        if (quotient == 0)

        {

            return false;

        }

    }

 

    return true;

 

 

 

int rsainit(myint64 p, myint64 q)

    if (p == q || p <= 2 || q <= 2 || isprimenumber(p) == 0 || isprimenumber(q) == 0)

    {

        return false;

    }

 

    g_n = p * q;

    if (g_n < 128)

    {

        return false;

    }

 

    myint64 mi64 = 0x8000000000000000;

    int bh = 63;

    for (int i = 63; i >= 0; i--)

    {

        if (g_n & mi64)

        {

            bh = i;

            break;

        }

        mi64 = mi64 >> 1;

    }

 

    if (bh >= 32)

    {

        g_blocksize = 8;

    }

    else if (bh >= 16)

    {

        g_blocksize = 4;

    }

    else if (bh >= 8)

    {

        g_blocksize = 2;

    }

    else

    {

        g_blocksize = 1;

    }

 

 

    myint64 fai_n = (p - 1) * (q - 1);

 

    myint64 w = 2;

    myint64 d = 2;

    for (w = 2; w < fai_n; w )

    {

        int result = isprimenumber(w);

        if (result)

        {

            for (d = 2; d < fai_n; d )

            {

                result = isprimenumber(w);

                if (result) {

                    myint64 m = w * d;

                    if ((d != w) && (m % fai_n == 1))

                    {

                        g_d = d;

                        g_w = w;

                        return true;

                    }

                }

            }

        }

    }

    return false;

 

//g_w is used to encrypt and g_d used to decrypt,and g_w must be primer to fai(n)

int rsa_encrypt(char* data, int size, char* dst) {

    if (g_n == 0 || g_w == 0 || g_d == 0)

    {

        return false;

    }

    int block_cnt = size / g_blocksize_src;

    int mod = size % g_blocksize_src;

 

    unsigned char* s = (unsigned char*)data;

    myint64* d = (myint64*)dst;

    for (int i = block_cnt; i > 0; i--)

    {

        myint64 tmp = 0;

        if (g_blocksize == 8)

        {

            tmp = *s;

            tmp = (tmp & 0xffffffffffffffff);

            tmp = (pow_i_old(tmp, g_w, g_n));

            *d = tmp;

        }

        else if (g_blocksize == 4)

        {

            tmp = *s;

            tmp = tmp & 0xffffffff;

            tmp = (pow_i_old(tmp, g_w, g_n));

            *(dword*)d = *(dword*)&tmp;

        }

        else if (g_blocksize == 2)

        {

            tmp = *s;

            tmp = tmp & 0xffff;

            tmp = (pow_i_old(tmp, g_w, g_n));

            *(word*)d = *(word*)&tmp;

        }

        else if (g_blocksize == 1)

        {

            tmp = *(unsigned char*)s;

            tmp = (tmp & 0xff);

            tmp = (pow_i_old(tmp, g_w, g_n));

            *(unsigned char*)d = *(unsigned char*)&tmp;

        }

 

        s = (unsigned char*)((unsigned char*)s g_blocksize_src);

        d = (myint64*)((unsigned char*)d g_blocksize);

    }

 

    if (mod)

    {

        myint64 tmp = 0;

        memcpy(&tmp, s, mod);

        if (g_blocksize_src == 8)

        {

            tmp = tmp & 0xffffffffffffffff;

        }

        else if (g_blocksize_src == 4)

        {

            tmp = tmp & 0xffffffff;

        }

        else if (g_blocksize_src == 2)

        {

            tmp = tmp & 0xffff;

        }

        else if (g_blocksize_src == 1)

        {

            tmp = tmp & 0xff;

        }

        tmp = pow_i_old(tmp, g_w, g_n);

        memcpy(d, &tmp, mod);

    }

    return size;

 

int rsa_decrypt(char* data, int size, char* dst) {

    if (g_n == 0 || g_w == 0 || g_d == 0)

    {

        return false;

    }

    int block_cnt = size / g_blocksize_src;

    int mod = size % g_blocksize_src;

 

    myint64* s = (myint64*)data;

    unsigned char* d = (unsigned char*)dst;

    for (int i = block_cnt; i > 0; i--)

    {

        myint64 tmp = 0;

        if (g_blocksize == 8)

        {

            tmp = *s;

            tmp = (tmp & 0xffffffffffffffff);

            tmp = (pow_i_old(tmp, g_d, g_n));

            *d = (unsigned char)tmp;

        }

        else if (g_blocksize == 4)

        {

            tmp = *(dword*)s;

            tmp = tmp & 0xffffffff;

            tmp = (pow_i_old(tmp, g_d, g_n));

            *d = (unsigned char)tmp;

        }

        else if (g_blocksize == 2)

        {

            tmp = *(word*)s;

            tmp = tmp & 0xffff;

            tmp = (pow_i_old(tmp, g_d, g_n));

            *d = (unsigned char)tmp;

        }

        else if (g_blocksize == 1)

        {

            tmp = *(unsigned char*)s;

            tmp = (tmp & 0xff);

            tmp = (pow_i_old(tmp, g_d, g_n));

            *d = (unsigned char)tmp;

        }

        s = (myint64*)((unsigned char*)s g_blocksize);

        d = (unsigned char*)((unsigned char*)d g_blocksize_src);

    }

 

    if (mod)

    {

        myint64 tmp = 0;

        memcpy(&tmp, s, mod);

        if (g_blocksize == 8)

        {

            tmp = tmp & 0xffffffffffffffff;

        }

        else if (g_blocksize == 4)

        {

            tmp = tmp & 0xffffffff;

        }

        else if (g_blocksize == 2)

        {

            tmp = tmp & 0xffff;

        }

        else if (g_blocksize == 1)

        {

            tmp = tmp & 0xff;

        }

        tmp = pow_i_old(tmp, g_d, g_n);

        memcpy(d, &tmp, mod);

    }

    return size;

原文链接:https://bbs.kanxue.com/thread-276899.htm

网络摘文,本文作者:15h,如若转载,请注明出处:https://www.15cov.cn/2023/08/27/原创rsa的数学原理与实例/

发表评论

邮箱地址不会被公开。 必填项已用*标注

网站地图