密码比对问题

 

 

 


三次密码输入
#include<stdio.h>
#include<stdlib.h>
#include<Windows.h>
int main()
{
char arr[20] = { 0 };
int i;

for (i = 0; i < 3; i++)
{
printf("请输入密码:\n");
scanf("%s", arr);
if (strcmp(arr, "123456") == 0)
{
printf("密码正确!\n"); 
break;
}
else
{
printf("密码错误,请再输入一次!\n");
}
}
if (i == 3)
{
printf("退出系统\n");
}
else if (i < 3)
{
printf("登陆成功!\n");
}
system("pause");
return 0;
}










大小写字母输出相对的,数字不输出


#include<stdio.h>
#include<Windows.h>
int main()
{
char arr[100] = {0};
int i;
char j , enter;
printf("请输入键盘上字符:\n");
for (i = 0; i < 100; i++)
{
arr[i] = getchar();
enter = arr[i];
if (enter < 91 && enter > 64)
{
j = arr[i] + 32;
putchar(j);
putchar('\n');
continue;
}
else if (enter > 96 && enter < 123)
{
j = arr[i] - 32;
putchar(j);
putchar('\n');
continue;
}
else if (enter < 58 && enter >47)
{
j = NULL;
putchar(j);
putwchar('\n');
continue;
}
}
system("pause");
return 0;
}








计算1/1-1/2+1/3-1/4+1/5 …… + 1/99 - 1/100 的值
#include<stdio.h>
#include<Windows.h>
int main()
{
int i;
double value;
for (value = 0, i = 1; i < 101; i++)
{
if (i%2==1)
{
value += 1.0 / i;
}
else if (i % 2 == 0)
{
value -= 1.0 / i;
}
}
printf("VALUE = %lf", value);
system("pause");
return 0;
}






输出整数每一位
#include<stdio.h>
#include<Windows.h>
int main(void)
{
int n;
int a;
scanf("%d", &n);
while (n)
{
a = n % 10;
n /= 10;
printf("%d\n ", a);
}

system("pause");
return 0;
}






1到100之间9的次数
#include<stdio.h>
#include<Windows.h>
int main()
{
int i;
int count = 0;
for (i = 1; i < 101; i++)
{
if (i % 10 == 9)
{
count++;
}
else if (i / 10 == 9)
{
count++;
}

}
printf("%d\n", count);
system("pause");
return 0;
}

 

 

 

### Java中实现MD5密码比对 在Java中进行MD5密码比对的核心思路是:首先将原始密码通过`java.security.MessageDigest`类或其他库(如Apache Commons Codec、Guava等)生成其对应的MD5哈希值;然后将此哈希值与数据库中的已存储的MD5值进行比较。如果两者一致,则表示密码匹配。 以下是基于`java.security.MessageDigest`类的一个完整的MD5密码比对示例: #### 示例代码 ```java import java.nio.charset.StandardCharsets; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class MD5PasswordComparison { public static String getMD5(String input) { try { MessageDigest md = MessageDigest.getInstance("MD5"); byte[] messageDigest = md.digest(input.getBytes(StandardCharsets.UTF_8)); StringBuilder hexString = new StringBuilder(); for (byte b : messageDigest) { String hex = Integer.toHexString(0xff & b); if (hex.length() == 1) { hexString.append('0'); } hexString.append(hex); } return hexString.toString(); // 返回32位十六进制字符串 } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); // 处理异常情况 } } public static boolean comparePasswords(String password, String storedHashedPassword) { String hashedInputPassword = getMD5(password); return hashedInputPassword.equals(storedHashedPassword); // 比较两个哈希值是否相等 } public static void main(String[] args) { String userEnteredPassword = "testpassword"; String storedHashedPasswordInDatabase = "098f6bcd4621d373cade4e832627b4f6"; // 假设这是数据库中存储的MD5值 if (comparePasswords(userEnteredPassword, storedHashedPasswordInDatabase)) { System.out.println("密码匹配!"); } else { System.out.println("密码不匹配!"); } } } ``` 上述代码实现了以下几个功能: 1. **getMD5函数**:用于接收一个字符串并返回其MD5哈希值[^1]。 2. **comparePasswords函数**:接受用户输入的明文密码以及数据库中存储的MD5哈希值,调用`getMD5`函数生成当前密码的MD5值并与存储值进行比较[^3]。 3. **main方法测试**:模拟了一个场景,在其中验证用户输入的密码是否与其存储的MD5值相符。 需要注意的是,虽然MD5是一种广泛使用的散列算法,但由于其存在碰撞风险,因此对于高安全性的应用场合建议采用更强大的算法,例如SHA-256或bcrypt[^4]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值