[密码学--RSA的加解密]

本文介绍了一种使用C语言实现的RSA加密算法。该程序包括密钥生成、加密和解密过程。用户可以输入素数p和q来生成密钥,并选择加密或解密操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

RSA算法详解及C语言实现

#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--;
    }
    printf( "%d\n", r );
    return(r);
}


void main()
{
    int p, q, e, d, m, n, t, c, r;
    char    s;
    printf( "please input the p,q: " );
    scanf( "%d%d", &p, &q );
    n = p * q;
    printf( "the n is %3d\n", n );
    t = (p - 1) * (q - 1);
    printf( "the t is %3d\n", t );
    printf( "please input the e: " );
    scanf( "%d", &e );
    if ( e < 1 || e > t )
    {
        printf( "e is error,please input again: " );
        scanf( "%d", &e );
    }
    d = 1;
    while ( ( (e * d) % t) != 1 )     
        d++;
    printf( "then caculate out that the d is %d\n", d );
    printf( "the cipher please input 1\n" );
    printf( "the plain please input 2\n" );
    scanf( "%d", &r );
    switch ( r )
    {
    case 1: printf( "input the m: " );      /*输入要加密的明文数字*/
        scanf( "%d", &m );
        c = candp( m, e, n );/*(加密的明文数字,加密指数,欧拉数)*/
        printf( "the cipher is %d\n", c ); break;
    case 2: printf( "input the c: " );      /*输入要解密的密文数字*/
        scanf( "%d", &c );
        m = candp( c, d, n );/*(需解密的密文,*/
        printf( "the cipher is %d\n", m ); break;
    }
    getchar();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值