我正在尝试使用C程序实现rsa算法。它在大多数情况下都能正常工作。但是在某些情况下,它不会进行加密,而是再次解密回到同一阶段。例如:加密和解密后的“-”更改为“ƒƒ”
这是加密和解密的代码。帮助将不胜感激。提前致谢

void encrypt(uChar state[16])
{
    long int pt,ct,key=e[0],k;
    i=0;
    while(i<=16)
    {
        pt=state[i];
        pt=pt-96;
        k=1;
        for (j=0;j<key;j++)
        {
            k=k*pt;
            k=k%n;
        }
    ct=k+96;
    state[i]=ct;
    i++;
    }
}

void decrypt(uChar state[16])
{
    long int pt,ct,key=d[0],k;
    i=0;
    while(i<=16)
    {
        ct=state[i]-96;
        k=1;
        for (j=0;j<key;j++)
        {
            k=k*ct;
            k=k%n;
        }
        pt=k+96;
        state[i]=pt;
        i++;
    }
}

最佳答案

您正在使用数组索引超出范围

while(i<=16)


您最多只能为state[16]编制索引,因此(在两个函数中)应为

while(i < 16)

关于c - C中的RSA加密/解密,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36382514/

10-11 23:23
查看更多