简易六位数字加密

两个端(如APP端与单片机端),以时间为加密起点,通过相同算法方式得到相同结果,用于校验输入端的密码是否正确。加密算法简易,可以再优化。

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
void Tnumber(int *a){//加密
    for(int i=0; i<6; i++){//每位的数字规则改变
    	a[i]=(a[i]+a[5]%10+a[4]%10)%10;
	}
    for (int i=0,T1; i<3; i++) {//位置互换
        T1=a[i];
        a[i]=a[abs(a[4]-i)%5];
        a[abs(a[4]-i)%5] = T1;
    }
}

int main(int argc, const char * argv[]) {
	
	while(1){
		
	time_t timep;
    struct tm *p;
    time (&timep);
    p=gmtime(&timep);
    printf("%d日 ",p->tm_mday);/*获取当前月份日数,范围是1-31*/
	printf("%d时 ",8+p->tm_hour);/*获取当前时,这里获取西方的时间,刚好相差八个小时*/
	printf("%d分 ",p->tm_min); /*获取当前分*/
	printf("\n");
    printf("\n");
	int a[6]={p->tm_mday/10,p->tm_mday%10,(8+p->tm_hour)/10,(8+p->tm_hour)%10,(p->tm_min)/10,(p->tm_min)%10};    //当前时间产生原密码 
    printf("时间加密之前:\n");
    for (int i=0; i<6; i++) {
        printf("%d ",a[i]);
    }
    Tnumber(a);
    printf("\n时间加密之后:\n");
    for (int i=0; i<6; i++) {
        printf("%d ",a[i]); 
    }
    
    printf("\n\n");
	int iup[6], iupNumber,statue=1;
	printf("请输入密码:\n");                     
	scanf("%d",&iupNumber);
	iup[0]=iupNumber/100000;
	iup[1]=iupNumber/10000%10;
	iup[2]=iupNumber/1000%10;
	iup[3]=iupNumber/100%10;
	iup[4]=iupNumber/10%10;
	iup[5]=iupNumber%10;
	printf("输入的密码:\n");
    for (int i=0; i<6; i++) {
        printf("%d ",iup[i]);
    }
    
	for (int i=0; i<6; i++) {
        if(a[i]!=iup[i]){
        	printf("\n密码错误…………\n");
        	statue = 0;
        	break; 
		} 
    }
    
    if(statue==1){
    	printf("\n密码正确…………\n");
	}
	printf("\n\n\n\n");		
	} 

    return 0;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
MATLAB可以用于实现数字图像加密解密系统。该系统可以对单通道和三通道的数字图像进行加密和解密,并支持多种主流图像格式。加密算法可以由用户自主设计,加密次数越多,像素越混乱,安全系数越高。解密时无需输入解密次数,可以一键解密图像。 以下是一个基于MATLAB的数字图像加密解密系统的示例代码: ```matlab % 加密函数 function encryptedImage = encryptImage(image, encryptionKey) % 在此处编写加密算法代码 % 返回加密后的图像 end % 解密函数 function decryptedImage = decryptImage(encryptedImage, decryptionKey) % 在此处编写解密算法代码 % 返回解密后的图像 end % 加载原始图像 originalImage = imread('original_image.jpg'); % 设置加密密钥 encryptionKey = 12345; % 加密图像 encryptedImage = encryptImage(originalImage, encryptionKey); % 保存加密后的图像 imwrite(encryptedImage, 'encrypted_image.jpg'); % 设置解密密钥 decryptionKey = 12345; % 解密图像 decryptedImage = decryptImage(encryptedImage, decryptionKey); % 保存解密后的图像 imwrite(decryptedImage, 'decrypted_image.jpg'); ``` 这是一个简单的示例,你可以根据自己的需求设计更复杂的加密算法。加密函数和解密函数分别用于对图像进行加密和解密操作。加密函数接受原始图像和加密密钥作为输入,并返回加密后的图像。解密函数接受加密后的图像和解密密钥作为输入,并返回解密后的图像。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值