实例四十:密码问题

实例四十:密码问题

问题描述:
某公司采用公共电话传递数据,每个数据都是四位的整数(类似电报编码),在传递过程是加密的,加密规则如下:
每位数字都加 5 ,除以 10 的余数代替该数字,再将第一位和第四位交换,第二位和第三位交换,试编写该加密数据的复原程序。
A company uses public telephones to transmit data. Each data is a four-digit integer (similar to telegraph coding). The transmission process is encrypted. The encryption rules are as follows:
Each digit is incremented by 5, divided by the remainder of 10 to replace the digit, and the first and fourth digits are exchanged, and the second and third digits are exchanged, and the recovery procedure of the encrypted data is tried.

算法思路:

  • 解密是加密的逆过程,经分析:将数据的第二位和第三位交换第一位和第四位交换,然后每位数字和 5 比较,大于等于 5 则减去 5, 小于 5 则加上 5.
  • Decryption is the inverse process of encryption. After analysis, the second and third bits of the data are exchanged for the first and fourth bits, and then each digit is compared with 5, and greater than or equal to 5 is subtracted by 5, less than 5 is added. Above 5.
/*实例四十:密码问题*/

#include<stdio.h>
int main()
{
    int n,w[4],i,m;
    printf("Please enter an encrypted four-digit integer:\n");
    scanf("%d",&n);             //将数据的第一位和第四位,第二位第三位交换,再依次存入w[]中
    w[0] = n % 10;                   //取第一位
    w[1] = n % 100 / 10;             //取得第二位
    w[2] = n /100 % 10;              //取得第三位
    w[3] = n / 1000;                 //取得第四位
    for(i=0;i<4;i++)            //复原数字
        if(w[i]<5)
            w[i] += 5;
    else
            w[i] -= 5;
    n = w[0] * 1000 + w[1] * 100 + w[2] * 10 + w[3];        //得到加密前数据
    printf("Before this data is encrypted, it is:   %d\n",n);
    return 0;
}

程序心得:

  • 加密和解密的算法很多,根据算法的复杂性,增加了破译的难度,也决定了数据安全性,但不管怎么样,加密的算法一定是可逆的,一般而言,解密的过程比加密过程复杂。
  • There are many algorithms for encryption and decryption. According to the complexity of the algorithm, the difficulty of deciphering is increased, and the data security is also determined. However, the encryption algorithm must be reversible anyway. In general, the decryption process is more complicated than the encryption process. .
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值