Openssl 1024bit RSA算法---公私钥获取和处理(一)

news2025/3/1 1:10:59

1.简介

使用OpenSSL生成公私钥文件,然后再将文件中的信息读出的操作。

由于要对设备升级,需要用到RSA算法对一部分验证信息进行加密.

2.使用OpenSSL获取公私钥

我在window系统尝试安装OpenSSL,但是安装不上,我们可以使用linux自带的OpenSSL自动生成公私钥(我这里使用的Ubuntu)

打开终端,依次输入一下命令就可以生成我们想要的文件(生成文本文件,是为了便于查看文件内容;生成bin文件是为了接下来的文件操作)

//生成1024位的私钥
openssl genrsa -out private.pem 1024
 
//将生成的文件转换为文本
openssl rsa -in private.pem -text -out private.txt
 
//将BASE64编码的文件转换为bin文件
openssl   base64  -d  -in private.pem -out private.bin

详细关于私钥(PEM格式)参考:OPENSSL中RSA私钥文件(PEM格式)解析【一】 - 走看看

3.文件处理

3.1秘钥信息获取

通过查看生成的文本文件发现,不同的文件生成的文件长度不一样,有些会在头部自动添加一个“00”

为了解决不同文件的差异,消除“00”的影响,文件处理流程如下:

 上面的步骤还有uint32_t bits的位还没有设定,由于我们生成的1024bit的秘钥,所以我们将bits的值设为1024.如下所示的结构体,就是RSA私钥断开

//上面的流程主要是将生成的秘钥按格式填入
typedef struct
{
    uint32_t bits;                   /* length in bits of modulus */
    uint8_t  modulus[256];           /* modulus */
    uint8_t  publicExponent[4];      /* public exponent */
    uint8_t  exponent[256];          /* private exponent */
    uint8_t  prime[2][128];          /* prime factors */
    uint8_t  primeExponent[2][128];  /* exponents for CRT */
    uint8_t  coefficient[128];       /* CRT coefficient */ 
} R_RSA_PRIVATE_KEY;

3.2公钥信息获取 

typedef struct
{
    uint32_t bits;                   /* length in bits of modulus */
    uint8_t  modulus[256];           /* modulus */
    uint8_t  exponent[4];            /* public exponent */ 
} R_RSA_PUBLIC_KEY;

4.读取文件公私钥信息

#ifndef READ_PEM_FILE_H
#define READ_PEM_FILE_H
#include "file.h"
#include "s_rsa.h"
 
 
void generate_private_and_public_key(R_RSA_PRIVATE_KEY* privat_key, R_RSA_PUBLIC_KEY* public_key);
 
#endif
#include "read_pem_file.h"
 
 
 
static int is_private_pem_exist(void)//判断当前文件下有没有private.pem
{
    if(fopen("private.bin", "r"))
    {
        return 1;
    }
    else
    {
        printf("\n当前文件未找到private.bin这个文件,请用openssl工具生成\n\n");
        return 0;
    }
}
 
//从文件中读取私钥信息
static void read_private_pem(R_RSA_PRIVATE_KEY* privat_key)
{
    int i = 0;
    long long file_size = 0;
    char* flie_buffer;
    if(is_private_pem_exist() == 1)
    {
        flie_buffer = copyFile("private.bin", &file_size);
    }
    else
    {
        system("pause");
        exit(0);
    }
 
    //printf("%02x\n\n", file_size);
 
    //for(i = 1; i<= file_size; i++)
    //{
    //  printf("%02x ", (unsigned char)flie_buffer[i - 1]);
    //  if(i % 16 == 0)
    //  {
    //      printf("\n");
    //  }
    //}
 
 
 
    flie_buffer += 10;
    if(*flie_buffer == 0)
    {
        flie_buffer += 1;
    }
    for(i = 0; i < 128; i++)
    {
        privat_key->modulus[i] = flie_buffer[i];
    }
    flie_buffer += 127;
 
    flie_buffer += 3;
    if(*flie_buffer == 0)
    {
        flie_buffer += 1;
    }
    for(i = 0; i < 3; i++)
    {
        privat_key->publicExponent[i + 1] = flie_buffer[i];
    }
    flie_buffer += 2;
 
    flie_buffer += 4;
    if(*flie_buffer == 0)
    {
        flie_buffer += 1;
    }
    for(i = 0; i < 128; i++)
    {
        privat_key->exponent[i] = flie_buffer[i];
    }
    flie_buffer += 127;
 
    flie_buffer += 3;
    if(*flie_buffer == 0)
    {
        flie_buffer += 1;
    }
    for(i = 0; i < 64; i++)
    {
        privat_key->prime[0][i] = flie_buffer[i];
    }
    flie_buffer += 63;
 
    flie_buffer += 3;
    if(*flie_buffer == 0)
    {
        flie_buffer += 1;
    }
    for(i = 0; i < 64; i++)
    {
        privat_key->prime[1][i] = flie_buffer[i];
    }
    flie_buffer += 63;
 
    flie_buffer += 3;
    if(*flie_buffer == 0)
    {
        flie_buffer += 1;
    }
    for(i = 0; i < 64; i++)
    {
        privat_key->primeExponent[0][i] = flie_buffer[i];
    }
    flie_buffer += 63;
 
    flie_buffer += 3;
    if(*flie_buffer == 0)
    {
        flie_buffer += 1;
    }
    for(i = 0; i < 64; i++)
    {
        privat_key->primeExponent[1][i] = flie_buffer[i];
    }
    flie_buffer += 63;
 
    flie_buffer += 3;
    if(*flie_buffer == 0)
    {
        flie_buffer += 1;
    }
    for(i = 0; i < 64; i++)
    {
        privat_key->coefficient[i] = flie_buffer[i];
    }
}
 
//打印私钥信息
static void prinf_privat_key(R_RSA_PRIVATE_KEY* privat_key)
{
    int i = 0;
    printf("\nmoduls:\n");
    for(i = 1; i <= 128; i++)
    {
        printf("%02x ",privat_key->modulus[i - 1]);
        if(i % 16 == 0)
        {
            printf("\n");
        }
    }
 
    printf("\npubicExpinent:\n");
    for(i = 0; i < 4; i++)
    {
        printf("%02x ",privat_key->publicExponent[i]);
    }
 
    printf("\nprivateExponent:\n");
    for(i = 1; i <= 128; i++)
    {
        printf("%02x ",privat_key->exponent[i - 1]);
        if(i % 16 == 0)
        {
            printf("\n");
        }
    }
 
    printf("\nprime1:\n");
    for(i = 1; i <= 64; i++)
    {
        printf("%02x ",privat_key->prime[0][i - 1]);
        if(i % 16 == 0)
        {
            printf("\n");
        }
    }
 
    printf("\nprime2:\n");
    for(i = 1; i <= 64; i++)
    {
        printf("%02x ",privat_key->prime[1][i - 1]);
        if(i % 16 == 0)
        {
            printf("\n");
        }
    }
 
    printf("\nexponent1:\n");
    for(i = 1; i <= 64; i++)
    {
        printf("%02x ",privat_key->primeExponent[0][i - 1]);
        if(i % 16 == 0)
        {
            printf("\n");
        }
    }
 
    printf("\nexponent2:\n");
    for(i = 1; i <= 64; i++)
    {
        printf("%02x ",privat_key->primeExponent[1][i - 1]);
        if(i % 16 == 0)
        {
            printf("\n");
        }
    }
 
    printf("\ncoefficient:\n");
    for(i = 1; i <= 64; i++)
    {
        printf("%02x ",privat_key->coefficient[i - 1]);
        if(i % 16 == 0)
        {
            printf("\n");
        }
    }
     
}
 
//填充bits以及给公钥赋值
//生成公钥,私钥
void generate_private_and_public_key(R_RSA_PRIVATE_KEY* privat_key, R_RSA_PUBLIC_KEY* public_key)
{
    public_key->bits = 1024;
    privat_key->bits = 1024;
 
    read_private_pem(privat_key);
    prinf_privat_key(privat_key);//读出私钥信息
 
    for(int i = 0; i < 4; i++)
    {
        public_key->exponent[i] = privat_key->publicExponent[i];
    }
    for(int i = 0; i < 256; i++)
    {
        public_key->modulus[i] = privat_key->modulus[i];
    }
}

 5. RSA标准算法

#ifndef S_RSA_H_
#define S_RSA_H_
 
typedef unsigned char uint8_t;
typedef unsigned int  uint32_t;
// ����ֵ����
#define RE_DATA 0x0401
#define RE_LEN  0x0406
 
/* RSA key lengths.
 */
#define MIN_RSA_MODULUS_BITS 508
/* #define MAX_RSA_MODULUS_BITS 1024 ** linq modify ->>2048 ***/
#define MAX_RSA_MODULUS_BITS 2048
#define MAX_RSA_MODULUS_LEN ((MAX_RSA_MODULUS_BITS + 7) / 8)
#define MAX_RSA_PRIME_BITS ((MAX_RSA_MODULUS_BITS + 1) / 2)
#define MAX_RSA_PRIME_LEN ((MAX_RSA_PRIME_BITS + 7) / 8)
 
/* Length of digit in bits */
#define NN_DIGIT_BITS 32
#define NN_HALF_DIGIT_BITS 16
/* Length of digit in bytes */
#define NN_DIGIT_LEN (NN_DIGIT_BITS / 8)
/* Maximum length in digits */
#define MAX_NN_DIGITS \
  ((MAX_RSA_MODULUS_LEN + NN_DIGIT_LEN - 1) / NN_DIGIT_LEN + 1)
/* Maximum digits */
#define MAX_NN_DIGIT 0xffffffff
#define MAX_NN_HALF_DIGIT 0xffff
 
/* Macros.
 */
#define LOW_HALF(x) ((x) & MAX_NN_HALF_DIGIT)
#define HIGH_HALF(x) (((x) >> NN_HALF_DIGIT_BITS) & MAX_NN_HALF_DIGIT)
#define TO_HIGH_HALF(x) (((NN_DIGIT)(x)) << NN_HALF_DIGIT_BITS)
#define DIGIT_MSB(x) (uint32_t)(((x) >> (NN_DIGIT_BITS - 1)) & 1)
#define DIGIT_2MSB(x) (uint32_t)(((x) >> (NN_DIGIT_BITS - 2)) & 3)
 
#define NN_ASSIGN_DIGIT(a, b, digits) {NN_AssignZero (a, digits); a[0] = b;}
#define NN_EQUAL(a, b, digits) (! NN_Cmp (a, b, digits))
#define NN_EVEN(a, digits) (((digits) == 0) || ! (a[0] & 1))
 
 
typedef struct     // ��Կ�ṹ
{
    uint32_t bits;                   /* length in bits of modulus */
    uint8_t  modulus[256];           /* modulus */
    uint8_t  exponent[4];            /* public exponent */ 
} R_RSA_PUBLIC_KEY;
 
typedef struct    // ˽Կ�ṹ
{
    uint32_t bits;                   /* length in bits of modulus */
    uint8_t  modulus[256];           /* modulus */
    uint8_t  publicExponent[4];      /* public exponent */
    uint8_t  exponent[256];          /* private exponent */
    uint8_t  prime[2][128];          /* prime factors */
    uint8_t  primeExponent[2][128];  /* exponents for CRT */
    uint8_t  coefficient[128];       /* CRT coefficient */ 
} R_RSA_PRIVATE_KEY;
 
 
int  rsa_public(void *outbuf, int *outlen, const void *inbuf, int inlen, const R_RSA_PUBLIC_KEY *pubkey);
int  rsa_private(void *outbuf, int *outlen, const void *inbuf, int inlen, const R_RSA_PRIVATE_KEY *prikey);
 
#endif
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "s_rsa.h"
 
typedef uint32_t NN_DIGIT;
typedef uint16_t NN_HALF_DIGIT;
 
/****************************************************************************
  ??????     :  void dmult(uint32_t a, uint32_t b, uint32_t *cHigh, uint32_t *cLow)
  ????       :  32λ???????????
****************************************************************************/
void dmult(uint32_t a, uint32_t b, uint32_t *cHigh, uint32_t *cLow)
{
    uint64_t temp1,temp2,temp3;
 
    temp1 = a;
    temp2 = b;
 
    temp3 = temp1*temp2;
 
    *cHigh = temp3>>32;
    *cLow = temp3;
}
 
/****************************************************************************
  ??????     :
  ????       :
  ???????   :
  ???????   :
  ?????     :
  ??????   :
      ?????     ??????    ???汾??   ??????
  1??
****************************************************************************/
NN_DIGIT subdigitmult(NN_DIGIT *a, NN_DIGIT *b, NN_DIGIT c, NN_DIGIT *d, NN_DIGIT digits)
{
    NN_DIGIT borrow, thigh, tlow;
    NN_DIGIT i;
 
    borrow = 0;
 
    if(c != 0)
    {
        for(i = 0; i < digits; i++)
        {
            dmult(c, d[i], &thigh, &tlow);
            if((a[i] = b[i] - borrow) > (MAX_NN_DIGIT - borrow))
                borrow = 1;
            else
                borrow = 0;
            if((a[i] -= tlow) > (MAX_NN_DIGIT - tlow))
                borrow++;
            borrow += thigh;
        }
    }
    return (borrow);
}
 
/* Decodes character string b into a, where character string is ordered
   from most to least significant.
 
   Lengths: a[digits], b[len].
   Assumes b[i] = 0 for i < len - digits * NN_DIGIT_LEN. (Otherwise most
   significant bytes are truncated.)
 */
void NN_Decode (NN_DIGIT *a, uint32_t digits, uint8_t *b, uint32_t len)
{
    NN_DIGIT t;
    int32_t j;
    uint32_t i, u;
 
    for (i = 0, j = len - 1; i < digits && j >= 0; i++)
    {
        t = 0;
        for (u = 0; j >= 0 && u < NN_DIGIT_BITS; j--, u += 8)
        {
            t |= ((NN_DIGIT)b[j]) << u;
        }
        a[i] = t;
    }
 
    for (; i < digits; i++)
        a[i] = 0;
}
 
/* Encodes b into character string a, where character string is ordered
   from most to least significant.
 
   Lengths: a[len], b[digits].
   Assumes NN_Bits (b, digits) <= 8 * len. (Otherwise most significant
   digits are truncated.)
 */
void NN_Encode (uint8_t *a, uint32_t len, NN_DIGIT *b, uint32_t digits)
{
    NN_DIGIT t;
    int32_t j;
    uint32_t i, u;
 
    for (i = 0, j = len - 1; i < digits && j >= 0; i++)
    {
        t = b[i];
        for (u = 0; j >= 0 && u < NN_DIGIT_BITS; j--, u += 8)
            a[j] = (uint8_t)(t >> u);
    }
 
    for (; j >= 0; j--)
        a[j] = 0;
}
 
/* Assigns a = b.
 
   Lengths: a[digits], b[digits].
 */
void NN_Assign (NN_DIGIT *a, NN_DIGIT *b, uint32_t digits)
{
    if(digits)
    {
        do
        {
            *a++ = *b++;
        }while(--digits);
    }
}
 
/* Assigns a = 0.
 
   Lengths: a[digits].
 */
void NN_AssignZero (NN_DIGIT *a, uint32_t digits)
{
    if(digits)
    {
        do
        {
            *a++ = 0;
        }while(--digits);
    }
}
 
/* Returns the significant length of a in bits, where a is a digit.
 */
static uint32_t NN_DigitBits (NN_DIGIT a)
{
    uint32_t i;
 
    for (i = 0; i < NN_DIGIT_BITS; i++, a >>= 1)
    {
        if (a == 0)
            break;
    }
 
    return (i);
}
 
/* Returns the significant length of a in digits.
 
   Lengths: a[digits].
 */
uint32_t NN_Digits (NN_DIGIT *a, uint32_t digits)
{
    int32_t i;
 
    for (i = digits - 1; i >= 0; i--)
    {
        if (a[i])
            break;
    }
 
    return (i + 1);
}
 
/* Returns sign of a - b.
   Lengths: a[digits], b[digits].
 */
int32_t NN_Cmp (NN_DIGIT *a, NN_DIGIT *b, uint32_t digits)
{
    int32_t i;
 
    for (i = digits - 1; i >= 0; i--)
    {
        if (a[i] > b[i])
            return (1);
        if (a[i] < b[i])
            return (-1);
    }
 
    return (0);
}
 
/* Computes a = b + c. Returns carry.
 
   Lengths: a[digits], b[digits], c[digits].
 */
NN_DIGIT NN_Add (NN_DIGIT *a, NN_DIGIT *b, NN_DIGIT *c, uint32_t digits)
{
    NN_DIGIT ai, carry;
    uint32_t i;
 
    carry = 0;
 
    for (i = 0; i < digits; i++)
    {
        if ((ai = b[i] + carry) < carry)
            ai = c[i];
        else if ((ai += c[i]) < c[i])
            carry = 1;
        else
            carry = 0;
        a[i] = ai;
    }
 
    return (carry);
}
 
 
/* Computes a = b - c. Returns borrow.
 
   Lengths: a[digits], b[digits], c[digits].
 */
NN_DIGIT NN_Sub (NN_DIGIT *a, NN_DIGIT *b, NN_DIGIT *c, uint32_t digits)
{
    NN_DIGIT ai, borrow;
    uint32_t i;
 
    borrow = 0;
 
    for (i = 0; i < digits; i++)
    {
        if ((ai = b[i] - borrow) > (MAX_NN_DIGIT - borrow))
            ai = MAX_NN_DIGIT - c[i];
        else if ((ai -= c[i]) > (MAX_NN_DIGIT - c[i]))
            borrow = 1;
        else
            borrow = 0;
        a[i] = ai;
    }
 
    return (borrow);
}
 
/* Computes a = b * 2^c (i.e., shifts left c bits), returning carry.
 
   Lengths: a[digits], b[digits].
   Requires c < NN_DIGIT_BITS.
 */
NN_DIGIT NN_LShift (NN_DIGIT *a, NN_DIGIT *b, uint32_t c, uint32_t digits)
{
    NN_DIGIT bi, carry;
    uint32_t i, t;
 
    if (c >= NN_DIGIT_BITS)
        return (0);
 
    t = NN_DIGIT_BITS - c;
 
    carry = 0;
 
    for (i = 0; i < digits; i++)
    {
        bi = b[i];
        a[i] = (bi << c) | carry;
        carry = c ? (bi >> t) : 0;
    }
 
    return (carry);
}
 
/* Computes a = c div 2^c (i.e., shifts right c bits), returning carry.
 
   Lengths: a[digits], b[digits].
   Requires: c < NN_DIGIT_BITS.
 */
NN_DIGIT NN_RShift (NN_DIGIT *a, NN_DIGIT *b, uint32_t c, uint32_t digits)
{
    NN_DIGIT bi, carry;
    int32_t i;
    uint32_t t;
 
    if (c >= NN_DIGIT_BITS)
        return (0);
 
    t = NN_DIGIT_BITS - c;
 
    carry = 0;
 
    for (i = digits - 1; i >= 0; i--)
    {
        bi = b[i];
        a[i] = (bi >> c) | carry;
        carry = c ? (bi << t) : 0;
    }
 
    return (carry);
}
 
 
/* Computes a = b * c.
 
   Lengths: a[2*digits], b[digits], c[digits].
   Assumes digits < MAX_NN_DIGITS.
 */
void NN_Mult (NN_DIGIT *a, NN_DIGIT *b, NN_DIGIT *c, uint32_t digits)
{
    NN_DIGIT dhigh, dlow, carry;
    NN_DIGIT bDigits, cDigits, i, j;
    NN_DIGIT mt[2*MAX_NN_DIGITS];
 
    NN_AssignZero (mt, 2 * digits);
 
    bDigits = NN_Digits (b, digits);
    cDigits = NN_Digits (c, digits);
 
    for (i = 0; i < bDigits; i++)
    {
        carry = 0;
        if(*(b+i) != 0)
        {
            for(j = 0; j < cDigits; j++)
            {              
                dmult(*(b+i), *(c+j), &dhigh, &dlow);              
                if((*(mt+(i+j)) = *(mt+(i+j)) + carry) < carry)
                    carry = 1;
                else
                    carry = 0;
                if((*(mt+(i+j)) += dlow) < dlow)
                    carry++;
                carry += dhigh;
            }
        }
        *(mt+(i+cDigits)) += carry;
    }
     
    NN_Assign (a, mt, 2 * digits); 
 
    /* Zeroize potentially sensitive information.
    */
    memset ((uint8_t *)mt, 0, sizeof (mt));
}
 
/* Computes a = c div d and b = c mod d.
 
   Lengths: a[cDigits], b[dDigits], c[cDigits], d[dDigits].
   Assumes d > 0, cDigits < 2 * MAX_NN_DIGITS,
           dDigits < MAX_NN_DIGITS.
 */
void NN_Div (NN_DIGIT *a, NN_DIGIT *b, NN_DIGIT *c, uint32_t cDigits, NN_DIGIT *d, uint32_t dDigits)
{
    NN_DIGIT ai;
    int32_t i;
    uint32_t ddDigits, shift;
    NN_DIGIT div_cc[2*MAX_NN_DIGITS+1], div_dd[MAX_NN_DIGITS]; // ?????????м????
 
    NN_DIGIT s;
    NN_DIGIT t[2], u, v, *ccptr;
    NN_HALF_DIGIT aHigh, aLow, cHigh, cLow;
 
    ddDigits = NN_Digits (d, dDigits);
    if (ddDigits == 0)
        return;
 
    /* Normalize operands.
    */
    shift = NN_DIGIT_BITS - NN_DigitBits (d[ddDigits-1]);
    NN_AssignZero (div_cc, ddDigits);
    div_cc[cDigits] = NN_LShift (div_cc, c, shift, cDigits);
    NN_LShift (div_dd, d, shift, ddDigits);
 
    s = div_dd[ddDigits-1];
 
    NN_AssignZero (a, cDigits);
 
    for (i = cDigits-ddDigits; i >= 0; i--)
    {
        if (s == MAX_NN_DIGIT)
        {
            ai = div_cc[i+ddDigits];
        }
        else
        {
            ccptr = &div_cc[i+ddDigits-1];
 
            s++;
            cHigh = (NN_HALF_DIGIT)HIGH_HALF(s);
            cLow = (NN_HALF_DIGIT)LOW_HALF(s);
 
            *t = *ccptr;
            *(t+1) = *(ccptr+1);
 
            if (cHigh == MAX_NN_HALF_DIGIT)
            {
                aHigh = (NN_HALF_DIGIT)HIGH_HALF(*(t+1));
            }
            else
            {
                aHigh = (NN_HALF_DIGIT)(*(t+1) / (cHigh + 1));
            }
            u = (NN_DIGIT)aHigh * (NN_DIGIT)cLow;
            v = (NN_DIGIT)aHigh * (NN_DIGIT)cHigh;
            if ((*t -= TO_HIGH_HALF(u)) > (MAX_NN_DIGIT - TO_HIGH_HALF(u)))
            {
                t[1]--;
            }
            *(t+1) -= HIGH_HALF(u);
            *(t+1) -= v;
 
            while ((*(t+1) > cHigh) ||   ((*(t+1) == cHigh) && (*t >= TO_HIGH_HALF(cLow))))
            {
                if ((*t -= TO_HIGH_HALF(cLow)) > MAX_NN_DIGIT - TO_HIGH_HALF(cLow))
                    t[1]--;
                *(t+1) -= cHigh;
                aHigh++;
            }
 
            if (cHigh == MAX_NN_HALF_DIGIT)
            {
                aLow = (NN_HALF_DIGIT)LOW_HALF(*(t+1));
            }
            else
            {
                aLow = (NN_HALF_DIGIT)((TO_HIGH_HALF(*(t+1)) + HIGH_HALF(*t)) / (cHigh + 1));
            }
            u = (NN_DIGIT)aLow * (NN_DIGIT)cLow;
            v = (NN_DIGIT)aLow * (NN_DIGIT)cHigh;
            if ((*t -= u) > (MAX_NN_DIGIT - u))
                t[1]--;
            if ((*t -= TO_HIGH_HALF(v)) > (MAX_NN_DIGIT - TO_HIGH_HALF(v)))
                t[1]--;
            *(t+1) -= HIGH_HALF(v);
 
            while ((*(t+1) > 0) || ((*(t+1) == 0) && *t >= s))
            {
                if ((*t -= s) > (MAX_NN_DIGIT - s))
                    t[1]--;
                aLow++;
            }
 
            ai = TO_HIGH_HALF(aHigh) + aLow;
            s--;
        }
 
        div_cc[i+ddDigits] -= subdigitmult(&div_cc[i], &div_cc[i], ai, div_dd, ddDigits);
 
        while (div_cc[i+ddDigits] || (NN_Cmp(&div_cc[i], div_dd, ddDigits) >= 0))
        {
            ai++;
            div_cc[i+ddDigits] -= NN_Sub(&div_cc[i], &div_cc[i], div_dd, ddDigits);
        }
 
        a[i] = ai;
    }
 
    /* Restore result.
    */
    NN_AssignZero (b, dDigits);
    NN_RShift (b, div_cc, shift, ddDigits);
 
    /* Zeroize potentially sensitive information.
    */
    memset ((uint8_t *)div_cc, 0, sizeof (div_cc));
    memset ((uint8_t *)div_dd, 0, sizeof (div_dd));
}
 
/* Computes a = b mod c.
 
   Lengths: a[cDigits], b[bDigits], c[cDigits].
   Assumes c > 0, bDigits < 2 * MAX_NN_DIGITS, cDigits < MAX_NN_DIGITS.
 */
void NN_Mod (NN_DIGIT *a, NN_DIGIT *b, uint32_t bDigits, NN_DIGIT *c, uint32_t cDigits)
{
    NN_DIGIT mod_t[2 * MAX_NN_DIGITS];    // ??????м????
 
    NN_Div (mod_t, a, b, bDigits, c, cDigits);
 
    /* Zeroize potentially sensitive information.
    */
    memset ((uint8_t *)mod_t, 0, sizeof (mod_t));
}
 
/* Computes a = b * c mod d.
 
   Lengths: a[digits], b[digits], c[digits], d[digits].
   Assumes d > 0, digits < MAX_NN_DIGITS.
 */
void NN_ModMult (NN_DIGIT *a, NN_DIGIT *b, NN_DIGIT *c, NN_DIGIT *d, uint32_t digits)
{
    NN_DIGIT mod_t[2 * MAX_NN_DIGITS];    // ??????м????
 
    NN_Mult (mod_t, b, c, digits);
    NN_Mod (a, mod_t, 2 * digits, d, digits);
 
    /* Zeroize potentially sensitive information.
    */
    memset ((uint8_t *)mod_t, 0, sizeof (mod_t));
}
 
/* Computes a = b^c mod d.
 
   Lengths: a[dDigits], b[dDigits], c[cDigits], d[dDigits].
   Assumes d > 0, cDigits > 0, dDigits < MAX_NN_DIGITS.
 */
void NN_ModExp (NN_DIGIT *a, NN_DIGIT *b, NN_DIGIT *c, uint32_t cDigits, NN_DIGIT *d, uint32_t dDigits)
{
    int32_t i;
    uint32_t ciBits, j, s;
    NN_DIGIT ci;
    NN_DIGIT bPower[3][MAX_NN_DIGITS], Exp_t[MAX_NN_DIGITS];   // ????????м????
 
    /* Store b, b^2 mod d, and b^3 mod d.
    */
    NN_Assign (bPower[0], b, dDigits);
    NN_ModMult (bPower[1], bPower[0], b, d, dDigits);
    NN_ModMult (bPower[2], bPower[1], b, d, dDigits);
 
    NN_ASSIGN_DIGIT (Exp_t, 1, dDigits);
 
    cDigits = NN_Digits (c, cDigits);
    for (i = cDigits - 1; i >= 0; i--)
    {
        ci = c[i];
        ciBits = NN_DIGIT_BITS;
 
        /* Scan past leading zero bits of most significant digit.
        */
        if (i == (int)(cDigits - 1))
        {
            while (! DIGIT_2MSB (ci))
            {
                ci <<= 2;
                ciBits -= 2;
            }
        }
 
        for (j = 0; j < ciBits; j += 2, ci <<= 2)
        {
            /* Compute t = t^4 * b^s mod d, where s = two MSB's of ci.
            */
            NN_ModMult (Exp_t, Exp_t, Exp_t, d, dDigits);
            NN_ModMult (Exp_t, Exp_t, Exp_t, d, dDigits);
            if ((s = DIGIT_2MSB (ci)) != 0)
                NN_ModMult (Exp_t, Exp_t, bPower[s-1], d, dDigits);
        }
    }
 
    NN_Assign (a, Exp_t, dDigits);
 
    /* Zeroize potentially sensitive information.
    */
    memset ((uint8_t *)bPower, 0, sizeof (bPower));
    memset ((uint8_t *)Exp_t, 0, sizeof (Exp_t));
}
 
static int32_t RSAPublicBlock (uint8_t *output,                  /* output block */
                                uint32_t *outputLen,              /* length of output block */
                                uint8_t *input,                   /* input block */
                                uint32_t inputLen,                /* length of input block */
                                R_RSA_PUBLIC_KEY *publicKey)    /* RSA public key */
{
    uint32_t eDigits, nDigits;
    NN_DIGIT pc[MAX_NN_DIGITS];           // ???????????????
    NN_DIGIT pe[MAX_NN_DIGITS];           // ??????
    NN_DIGIT pm[MAX_NN_DIGITS];           // ????????
    NN_DIGIT pn[MAX_NN_DIGITS];           // ????
 
    // ??????:??????????????RSA???????????????
    // ???????????????????256????
    NN_Decode (pm, MAX_NN_DIGITS, input, inputLen);
    //NN_Decode (pn, MAX_NN_DIGITS, publicKey->modulus, MAX_RSA_MODULUS_LEN);
    NN_Decode (pn, MAX_NN_DIGITS, publicKey->modulus, publicKey->bits/8);
    NN_Decode (pe, MAX_NN_DIGITS, publicKey->exponent, 4);
    nDigits = NN_Digits (pn, MAX_NN_DIGITS);
    eDigits = NN_Digits (pe, MAX_NN_DIGITS);
 
    if (NN_Cmp (pm, pn, nDigits) >= 0)
        return (RE_DATA);
 
    /* Compute pc = pm^pe mod pn.
    */
    NN_ModExp (pc, pm, pe, eDigits, pn, nDigits);
 
    *outputLen = (publicKey->bits + 7) / 8;
    NN_Encode (output, *outputLen, pc, nDigits);
 
    /* Zeroize sensitive information.
    */
    memset ((uint8_t *)pc, 0, sizeof (pc));
    memset ((uint8_t *)pm, 0, sizeof (pm));
 
    return (0);
}
 
/* Raw RSA private-key operation. Output has same length as modulus.
 
   Assumes inputLen < length of modulus.
   Requires input < modulus.
 */
static int32_t RSAPrivateBlock (uint8_t *output,            /* output block */
                                 uint32_t *outputLen,           /* length of output block */
                                 uint8_t *input,              /* input block */
                                 uint32_t inputLen,             /* length of input block */
                                 R_RSA_PRIVATE_KEY *privateKey)     /* RSA private key */
{
    uint32_t cDigits, nDigits, pDigits, OutLen;
    NN_DIGIT c[MAX_NN_DIGITS];            // ?????????????????????
    NN_DIGIT cP[MAX_NN_DIGITS];           // c?p???????
    NN_DIGIT cQ[MAX_NN_DIGITS];           // c?q???????
    NN_DIGIT dP[MAX_NN_DIGITS];           // ?????p????
    NN_DIGIT dQ[MAX_NN_DIGITS];           // ?????q????
    NN_DIGIT mP[MAX_NN_DIGITS];           // mP = cP^dP mod p
    NN_DIGIT mQ[MAX_NN_DIGITS];           // mQ = cQ^dQ mod q
    NN_DIGIT n[MAX_NN_DIGITS];            // ???
    NN_DIGIT p[MAX_NN_DIGITS];            // ??p????
    NN_DIGIT q[MAX_NN_DIGITS];            // ??q????
    NN_DIGIT qInv[MAX_NN_DIGITS];         // ??p??q????????
    NN_DIGIT t[MAX_NN_DIGITS];            // ??????????????
 
    NN_Decode (c, MAX_NN_DIGITS, input, inputLen);
 
    // ??????:??????????????RSA???????????????
    // ???????????????????256????
    /*NN_Decode(n, MAX_NN_DIGITS, privateKey->modulus, MAX_RSA_MODULUS_LEN);
    NN_Decode(p, MAX_NN_DIGITS, privateKey->prime[0], MAX_RSA_PRIME_LEN);
    NN_Decode(q, MAX_NN_DIGITS, privateKey->prime[1], MAX_RSA_PRIME_LEN);
    NN_Decode(dP, MAX_NN_DIGITS, privateKey->primeExponent[0], MAX_RSA_PRIME_LEN);
    NN_Decode(dQ, MAX_NN_DIGITS, privateKey->primeExponent[1], MAX_RSA_PRIME_LEN);
    NN_Decode(qInv, MAX_NN_DIGITS, privateKey->coefficient, MAX_RSA_PRIME_LEN);*/
 
    NN_Decode(n, MAX_NN_DIGITS, privateKey->modulus, privateKey->bits/8);
    NN_Decode(p, MAX_NN_DIGITS, privateKey->prime[0], privateKey->bits/16);
    NN_Decode(q, MAX_NN_DIGITS, privateKey->prime[1], privateKey->bits/16);
    NN_Decode(dP, MAX_NN_DIGITS, privateKey->primeExponent[0], privateKey->bits/16);
    NN_Decode(dQ, MAX_NN_DIGITS, privateKey->primeExponent[1], privateKey->bits/16);
    NN_Decode(qInv, MAX_NN_DIGITS, privateKey->coefficient, privateKey->bits/16);
 
    cDigits = NN_Digits (c, MAX_NN_DIGITS);
    nDigits = NN_Digits (n, MAX_NN_DIGITS);
    pDigits = NN_Digits (p, MAX_NN_DIGITS);
 
    if (NN_Cmp (c, n, nDigits) >= 0)
        return (RE_DATA);
 
    /* Compute mP = cP^dP mod p  and  mQ = cQ^dQ mod q. (Assumes q has
    length at most pDigits, i.e., p > q.)
    */
    NN_Mod (cP, c, cDigits, p, pDigits);
    NN_Mod (cQ, c, cDigits, q, pDigits);
    NN_ModExp (mP, cP, dP, pDigits, p, pDigits);
    NN_AssignZero (mQ, nDigits);
    NN_ModExp (mQ, cQ, dQ, pDigits, q, pDigits);
 
    /* Chinese Remainder Theorem:
    m = ((((mP - mQ) mod p) * qInv) mod p) * q + mQ.
    */
    if (NN_Cmp (mP, mQ, pDigits) >= 0)
        NN_Sub (t, mP, mQ, pDigits);
    else
    {
        NN_Sub (t, mQ, mP, pDigits);
        NN_Sub (t, p, t, pDigits);
    }
    NN_ModMult (t, t, qInv, p, pDigits);
    NN_Mult (t, t, q, pDigits);
    NN_Add (t, t, mQ, nDigits);
 
    OutLen = (privateKey->bits + 7) / 8;
    if(outputLen != NULL)
    {
        *outputLen = OutLen;
    }
    if(output != NULL)
    {
        NN_Encode (output, OutLen, t, nDigits);
    }
 
    /* Zeroize sensitive information.
    */
    memset ((uint8_t *)c, 0, sizeof (c));
    memset ((uint8_t *)cP, 0, sizeof (cP));
    memset ((uint8_t *)cQ, 0, sizeof (cQ));
    memset ((uint8_t *)dP, 0, sizeof (dP));
    memset ((uint8_t *)dQ, 0, sizeof (dQ));
    memset ((uint8_t *)mP, 0, sizeof (mP));
    memset ((uint8_t *)mQ, 0, sizeof (mQ));
    memset ((uint8_t *)p, 0, sizeof (p));
    memset ((uint8_t *)q, 0, sizeof (q));
    memset ((uint8_t *)qInv, 0, sizeof (qInv));
    memset ((uint8_t *)t, 0, sizeof (t));
 
    return (0);
}
 
 
int  rsa_public(void *outbuf, int *outlen, const void *inbuf, int inlen, const R_RSA_PUBLIC_KEY *pubkey)
{
    int status;
    uint32_t modulusLen, pkcsBlockLen=0;
    uint8_t pkcsBlock[MAX_RSA_MODULUS_LEN]; // ????????????????????????
 
    if((outbuf == NULL) || (pubkey == NULL)) {
        return(-1);
    }
    modulusLen = (pubkey->bits + 7) / 8;
    if (inlen > modulusLen){
        return (RE_LEN);
    }
 
    status = RSAPublicBlock(pkcsBlock, &pkcsBlockLen, (uint8_t *)inbuf, inlen, (R_RSA_PUBLIC_KEY *)pubkey);
    if(status){
        return (status);
    }
 
    if(pkcsBlockLen != modulusLen){
        return (RE_LEN);
    }
     
    if(outlen != NULL){
        *outlen = modulusLen;
    }
 
    if(outbuf != NULL){
        memcpy(outbuf, pkcsBlock, modulusLen);
    }
 
    /* Zeroize potentially sensitive information.
    */
    memset(pkcsBlock, 0, sizeof (pkcsBlock));
 
    return (0);
}
 
 
int  rsa_private(void *outbuf, int *outlen, const void *inbuf, int inlen, const R_RSA_PRIVATE_KEY *prikey)
{
    int status;
    uint32_t i, modulusLen;
    uint8_t pkcsBlock[MAX_RSA_MODULUS_LEN]; // ??????????????????????
 
    if((inbuf == NULL) || (prikey == NULL)){
        return(-1);
    }
    modulusLen = (prikey->bits + 7) / 8;
 
    if (inlen  > modulusLen){
        return (RE_LEN);
    }
 
    i = 0;
    memcpy (pkcsBlock+i, inbuf, inlen);
 
    *outlen = 0;
    status = RSAPrivateBlock((uint8_t*)outbuf, (uint32_t *)outlen, pkcsBlock, modulusLen, (R_RSA_PRIVATE_KEY *)prikey);
 
    /* Zeroize potentially sensitive information.
    */
    memset (pkcsBlock, 0, sizeof (pkcsBlock));
 
    return (status);
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/94555.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

模式识别 第7、8章 特征的选择和提取

基本概念 问题的提出 特征→ 特征空间&#xff1a; 每一个特征对应特征空间的一个维度 &#xff1b;特征越多&#xff0c;特征空间的维度越高原则&#xff1a;在保证分类效果的前提下用尽量少的特征来完成分类基本概念 &#xff08;1&#xff09;特征形成&#xff1a;由仪器…

如何去除图片雾化?给你推荐图片去雾怎么去除的方法

小伙伴们会不会和我一样喜欢外出爬山呢&#xff1f;为了留住美好记忆&#xff0c;我们会在途中拍照记录。但是山上很经常会有雾气&#xff0c;会容易导致我们拍出来的图片模糊不清。那应该怎么办呢&#xff1f;其实&#xff0c;我们只要对图片进行去雾处理就可以很好解决这个问…

[附源码]Nodejs计算机毕业设计基于的校园疫情防控管理Express(程序+LW)

该项目含有源码、文档、程序、数据库、配套开发软件、软件安装教程。欢迎交流 项目运行 环境配置&#xff1a; Node.js Vscode Mysql5.7 HBuilderXNavicat11VueExpress。 项目技术&#xff1a; Express框架 Node.js Vue 等等组成&#xff0c;B/S模式 Vscode管理前后端分…

Web(十一)JavaScript知识训练-数学对象

1、Math.ceil(-3.14)的结果是&#xff08; B&#xff09;。 A、 -3.14 B、 -3 C、 -4 D、 3.14 2、Math.floor(-3.14)的结果是&#xff08; C&#xff09;。 A、 -3.14 B、 -3 C、 -4 D、 3.14 3、Math.round(-3.14)的结果是&#xff08; B&#xff09;。 A、 -3.14 B、 -3 C…

Spring Cloud Openfeign微服务接口调用与Hystrix集成实战

关于openfeign 可以认为OpenFeign是Feign的增强版&#xff0c;不同的是OpenFeign支持Spring MVC注解。OpenFeign和Feign底层都内置了Ribbon负载均衡组件&#xff0c;在导入OpenFeign依赖后无需专门导入Ribbon依赖&#xff0c;用做客户端负载均衡&#xff0c;去调用注册中心服务…

dataFactory向mysql批量插入测试数据

目录 第一步&#xff1a;准备阶段&#xff1a;datafactory已连接app_user的表 第二步&#xff1a; 点击原表app_user,其中属性界面properties中配置含义如下&#xff1a; 第三步&#xff1a;根据需要设置插入字段 第四步&#xff1a;设置每个字段的规则后&#xff0c;点击se…

病毒之Worm.Win32.AutoRun

题外话&#xff1a;在被奥密克戎包围的我(两个室友和我&#xff0c;一个低烧、一个咳嗽、就差我了&#xff0c;这属实是真被包围了丫)在和Worm.Win32.AutoRun决一死战… 本次Worm.Win32.AutoRun的来源&#xff1a; windows电脑上重装vscode&#xff0c;然后没有 mingw-get-setu…

浏览器兼容模式如何设置?只需要跟着下面的步骤设置

许多考生在报考教师资格证或者其他的考试报名&#xff0c;会遇到浏览器兼容设置的问题。与其到时急急忙忙来设置浏览器的兼容模式&#xff0c;不如提前设置好&#xff0c;免了后顾之忧。浏览器兼容模式怎么设置&#xff1f;一起来看看关于浏览器兼容模式的含义以及设置方式吧&a…

《Python代码审计》(1)一款超好用的代码扫描工具

1.前言 从本文开始&#xff0c;我将开始介绍Python源代码审计&#xff0c;代码审计是检查源代码中的安全缺陷&#xff0c;检查源代码是否存在安全隐患&#xff0c;或者编码不规范的地方。通常使用自动化工具或者人工审查的方式&#xff0c;自动化工具效率高&#xff0c;但是误…

【浅学Java】Linux系统中的硬连接和软连接

Linux系统中的软连接和硬连接1. 前置知识1.1 文件的存储1.2 inode——索引节点1.3 Linux系统查找文件的过程2. Linux系统中的硬连接2.1 硬连接的实现原理2.2 实现硬连接的指令3. Linux系统中的软连接3.1 软连接的实现原理3.2 实现硬连接的指令4. 软连接和硬连接的区别1. 前置知…

Java 19虚拟线程实战与性能分析

Java 19推出了新特性“虚拟线程”&#xff0c;类似于Go语言中的协程。它是传统线程的不同之处在于&#xff0c;它是一种用户模式&#xff08;user-mode&#xff09;的线程。 虚拟线程是由 JDK 而非操作系统提供的线程的轻量级实现&#xff1a; 虚拟线程是没有绑定到特定操作系…

mock.js的使用

初始化 //main.js中 import "/network/fake-message/index.js" //全局文件中引入虚假数据///network/fake-message/index.js //创建伪数据 import Mock from mockjs import { Random } from mockjs//获取商品列表 Mock.mock(/api\/goodlist/,get,{status:200,mess…

基于java+springmvc+mybatis+vue+mysql的高校课程评价系统

项目介绍 在还没有计算机的时代&#xff0c;对于教学评价的各方面的管理都是靠纸、笔为主要的工具进行的。随着科技的发展&#xff0c;大量信息增长&#xff0c;利用传统的方式已经无法快速、准确的跟随学校各项目的管理工作了。当人们意识到对于大量信息管理的重要性的时候&a…

第二十二章 染色法与匈牙利算法

第二十二章 染色法与匈牙利算法一、使用场景——二分图二、染色法1、算法原理2、代码模板&#xff08;1&#xff09;问题&#xff1a;&#xff08;2&#xff09;代码&#xff1a;&#xff08;3&#xff09;分析&#xff1a;三、匈牙利算法1、算法用途2、算法思路3、算法模板&am…

五个月学完软件测试,现在分享以前自学的测试笔记

以前学习手抄的linux命令哈哈哈 定义 在规定的条件下对程序进行操作&#xff0c;以发现程序错误&#xff0c;衡量软件质量&#xff0c;并对其是否能满足设计要求进行评估的过程。 测试就是发现错误而执行程序的过程。 原则 保证测试的覆盖度&#xff0c;但是穷举测试是不可能…

LVI-SAM坐标系外参分析与代码修改,以适配各种数据集

文章目录0.前言1.原作者传感器件坐标系定义与外参修改1.1.博客作者的讲解&#xff08;仅供参考&#xff09;1.2.LIO-SAM的README中作者对其传感器配置的解释1.3.IMU坐标系详解1.4.params_lidar.yaml中LIO外参修改1.4.1.作者给的参数注释问题1.4.2.自己修改代码2.LVI-SAM中的坐标…

Redis常见面试题(一)

目录 1、Redis是什么? 2、Redis有哪些应用场景? 3、Redis有什么优势? 4、Redis为什么这么快? 5、Redis主要消耗什么物理资源? 6、Redis为什么把所有数据放到内存中? 7、Redis命令是原子性的吗? 8、Redis磁盘快照操作是原子的吗? 9、Redis怎么测试连通性? 10、…

【图像去噪】均值+中值+空间+高斯滤波图像去噪【含GUI Matlab源码 763期】

⛄一、图像去噪及滤波简介 1 图像去噪 1.1 图像噪声定义 噪声是干扰图像视觉效果的重要因素&#xff0c;图像去噪是指减少图像中噪声的过程。噪声分类有三种&#xff1a;加性噪声&#xff0c;乘性噪声和量化噪声。我们用f(x,y&#xff09;表示图像&#xff0c;g(x,y&#xff0…

四十六——

四十六、JavaScript——对象 一、对象 数据类型&#xff1a;原始值&#xff1a; 1. 数值 Number 2. 大整数 BigInt 3. 字符串 String 4. 布尔值 Boolean 5. 空值 Null 6. 未定义 Undefinded 7. 符号 Symbol 除了七种原始值之外&#xff0c;后面所用到的数据类型&#xff0c;都…

jsp+ssm计算机毕业设计茶园文化交流平台论文【附源码】

项目运行 环境配置&#xff1a; Jdk1.8 Tomcat7.0 Mysql HBuilderX&#xff08;Webstorm也行&#xff09; Eclispe&#xff08;IntelliJ IDEA,Eclispe,MyEclispe,Sts都支持&#xff09;。 项目技术&#xff1a; JSPSSM mybatis Maven等等组成&#xff0c;B/S模式 Mave…