RSA加密解密算法示例(C语言)

Python013

RSA加密解密算法示例(C语言),第1张

#include <stdlib.h>

#include <stdio.h>

#include <string.h>

#include <math.h>

#include <time.h>

#define PRIME_MAX 200   // 生成素数范围

#define EXPONENT_MAX 200 // 生成指数e范围

#define Element_Max 127    // 加密单元的最大值,这里为一个char, 即1Byte

char str_read[100]="hello world !"  // 待加密的原文

int str_encrypt[100]                // 加密后的内容

char str_decrypt[100]              // 解密出来的内容

int str_read_len                    // str_read 的长度

int prime1, prime2                  // 随机生成的两个质数

int mod, eular                      // 模数和欧拉数

int pubKey, priKey                  // 公钥指数和私钥指数

// 生成随机素数,实际应用中,这两个质数越大,就越难破解。

int randPrime()

{

int prime, prime2, i

next:

prime = rand() % PRIME_MAX   // 随机产生数

if (prime <= 1) goto next      // 不是质数,生成下一个随机数

if (prime == 2 || prime == 3) return prime

prime2 = prime / 2              // prime>=4, prime2 的平方必定大于 prime , 因此只检查小于等于prime2的数

for (i = 2i <= prime2i++)   // 判断是否为素数

{

if (i * i >prime) return prime

if (prime % i == 0) goto next  // 不是质数,生成下一个随机数

}

}

// 欧几里德算法,判断a,b互质

int gcd(int a, int b)

{

int temp

while (b != 0) {

temp = b

b = a % b

a = temp

}

return a

}

//生成公钥指数,条件是 1<e <欧拉数,且与欧拉数互质。

int randExponent()

{

int e

while (1)

{

e = rand() % eularif (e <EXPONENT_MAX) break

}

while (1)

{

if (gcd(e, eular) == 1) return ee = (e + 1) % eularif (e == 0 || e >EXPONENT_MAX) e = 2

}

}

//生成私钥指数

int inverse()

{

int d, x

while (1)

{

d = rand() % eular

x = pubKey * d % eular

if (x == 1)

{

return d

}

}

}

//加密函数

void jiami()           

{

str_read_len = strlen(str_read)      //从参数表示的地址往后找,找到第一个'\0',即串尾。计算'\0'至首地址的“距离”,即隔了几个字符,从而得出长度。

printf("密文是:")

for (int i = 0i <str_read_leni++)

{

int C = 1int a = str_read[i], b = a % mod

for (int j = 0j <pubKeyj++) //实现加密

{

C = (C*b) % mod

}

str_encrypt[i] = C

printf("%d ", str_encrypt[i])

}

printf("\n")

}

//解密函数

void jiemi()         

{

int i=0  for (i = 0i <str_read_leni++)

{

int C = 1int a = str_encrypt[i], b=a%mod

for (int j = 0j <priKeyj++)

{

C = (C * b) % mod

}

str_decrypt[i] = C

}

str_decrypt[i] = '\0'printf("解密文是:%s \n", str_decrypt)

}

int main()

{

srand(time(NULL))

while (1)

{

prime1 = randPrime()prime2 = randPrime()printf("随机产生两个素数:prime1 = %d , prime2 = %d ", prime1, prime2)

mod = prime1 * prime2printf("模数:mod = prime1 * prime2 = %d \n", mod)if (mod >Element_Max) break// 模数要大于每个加密单元的值

}

eular = (prime1 - 1) * (prime2 - 1)  printf("欧拉数:eular=(prime1-1)*(prime2-1) = %d \n", eular)

pubKey = randExponent()printf("公钥指数:pubKey = %d\n", pubKey)

priKey = inverse()printf("私钥指数:priKey = %d\n私钥为 (%d, %d)\n", priKey, priKey, mod)

jiami()jiemi()

return 0

}

文件分为文本文件和二进制文件。加密方法也略有不同。

1、文本文件

加密的主要是文本的内容,最简单的方法就是修改文档的内容,比如1.txt中的文件内容:

abcd

只要给每一个字符+1,就可以实现加密。文件内容即会变为

bcde

2、二进制文件加密

二进制文件加密也就是对应用程序加密,需要理解可执行文件格式,比如Windows平台的Exe文件它是PE结构,Linux上的可执行文件是ELF结构,要对这样的程序进行加密,实际上是开发一种叫做“壳”的程序,这种程序的开发,需要将扎实的底层基础,同时也需要对软件加密解密有细致的理解,比如流行的vmprotect、z壳以及早些年的upx壳、aspack等等。

3、无论哪种加密都牵涉到文件操作的问题,使用C语言进行文件操作时,极少使用C标准库中的I/O函数,大多数使用操作系统提供的内存文件映射相关的API函数,有兴趣,可以搜索相关的资料。

根据你的需要,修改了之前的代码。

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

#include <time.h>

const unsigned int MAX_KEY_LENGTH = 1000

int encode(char const *datafile, char const *keyfill)

int decode(char const *datafile, char const *keyfile)

int loadKey(char const *keyfile, int *keys, unsigned int size)

int saveKey(char const *keyfile, int *keys, unsigned int size)

int generateKey(int *keys, unsigned int size)

int main(int argc, char const *argv[])

{

    char datafile[] = "encrypted.txt"

    char keyfile[] = "key.txt"

    int retcode, choice, loop = 1

    char ch[5] = {'\0'}

    while(1)

    {

        printf("1. Encryption.\n")

        printf("2. Decryption.\n")

        printf("3. Exit.\n")

        printf("Selection (1,2,3):")        

        fgets(ch, sizeof(ch), stdin)

        sscanf(ch, "%d", &choice)        

        switch(choice)

        {

            case 1:                 

                retcode = encode(datafile, keyfile)

                if (retcode != 0) printf("error, %d\0", retcode)

                break

                

            case 2:                                

                retcode = decode(datafile, keyfile)

                if (retcode != 0) printf("error, %d\0", retcode)

                break

                

            case 3:

                loop = 0

                break

            default:

                

                break    

        }        

        if (0 == loop) break

    }    

    return 0

}

int generateKey(int *keys, unsigned int size) 

{

    char str[]="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz,./\"'<>?"    

    size_t str_len = sizeof(str)/sizeof(str[0])

    int i

    

    srand(time(NULL))

    for (i = 0 i < size ++i)

        keys[i] = str[rand() % str_len]

    return 0

}

int loadKey(char const *keyfile, int *keys, unsigned int size)

{

    int i = 0

    FILE *pfile    

    int retcode = 0

    pfile = fopen(keyfile, "r")

    if (pfile == NULL) return -1

    while ( !feof(pfile) ) {

        if (i < size)

            fscanf(pfile, "%d ", &keys[i++])

        else        

            break        

    }

    fclose(pfile)

    return i

}

int saveKey(char const *keyfile, int *keys, unsigned int size) 

{

    FILE *pfile

    int i

    pfile = fopen(keyfile, "w")

    if (pfile == NULL) return -1

    

    for(i = 0 i < size ++i) {

        fprintf(pfile, "%d ", keys[i])

    }

    fclose(pfile)

    return 0

}

int encode(char const *datafile, char const *keyfile) 

{

    char original[MAX_KEY_LENGTH] = {'\0'}

    char encrypted[MAX_KEY_LENGTH] = {'\0'}

    int i, size

    int keys[MAX_KEY_LENGTH]

    FILE *pdatafile, *pkeyfile

    

    pkeyfile = fopen(keyfile, "w")

    if (NULL == pkeyfile) return -1

    fclose(pkeyfile)    

    puts(" input message:")

    gets(original)

    size = strlen(original)

    if (0 != generateKey(keys, size)) return -2

    if (0 != saveKey(keyfile, keys, size) ) return -3

    

    pdatafile = fopen(datafile, "w")

    if (NULL == pdatafile) return -4

    for (i = 0 i < size ++i) {

        encrypted[i] = original[i] + keys[i]

        fputc(encrypted[i], pdatafile)

        fputc(encrypted[i], stdout)

    }

    printf("\n")

    fclose(pdatafile)

    return 0

}

int decode(char const *datafile, char const *keyfile)

{

    FILE *pdatafile, *pkeyfile

    int keys[MAX_KEY_LENGTH] = {0}

    char original[MAX_KEY_LENGTH] = {'\0'}

    char encrypted[MAX_KEY_LENGTH] = {'\0'}

    int i, size

    pkeyfile = fopen(keyfile, "r")

    if (NULL == pkeyfile) return -1

    fclose(pkeyfile)

    pdatafile = fopen(datafile, "r")

    if (NULL == pdatafile) return -2

    

    fscanf(pdatafile,"%s",encrypted)

    fclose(pdatafile)

    size = loadKey(keyfile, keys, MAX_KEY_LENGTH)

    if (size < 1) return -3

    

    for (i = 0 i < strlen(encrypted) ++i) {

        original[i] = encrypted[i]-keys[i]

        fputc(original[i], stdout)

    }

    printf("\n")

    return 0

}

运行结果:

1. Encryption.

2. Decryption.

3. Exit.

Selection (1,2,3):1

 input message:

this is A test!

╓┐»╞Lñ╗ù|t▄╬╢╒è

1. Encryption.

2. Decryption.

3. Exit.

Selection (1,2,3):2

this is A test!

1. Encryption.

2. Decryption.

3. Exit.

Selection (1,2,3):3