C++随机密码代码

 在我们账户中,都需要输入密码,如果密码过于简单账户就会被盗,而随机密码的密码完全是随机的,不易被破解,随机密码代码如下:

#include <iostream>
#include <cstring>
#include <time.h>
using namespace std;
int main(int argc, char** argv)
{
	srand((unsigned)time(NULL));
	char a[]={"1234567890QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm!@#$%^&*()_+-={}[:\";']<>,.?/\\"};//*随机密码字符数组
	for(int i=0,len=strlen(a);i<15;i++)
	{
		cout<<a[rand()%len];//*随机分配字符
	}
	cout<<endl;
	system("pause");
	return 0;
}

运行后得到的随机密码如下:

4V3"0}R@!Es)Nio
请按任意键继续. . .

 使用这种密码,就可以防止密码被破解了

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是ElGamal密码C++代码实现: ```cpp #include <iostream> #include <cstdlib> #include <ctime> #include <cmath> using namespace std; typedef long long LL; LL gcd(LL a, LL b) { return b == 0 ? a : gcd(b, a % b); } LL quickPow(LL a, LL b, LL p) { LL ans = 1 % p; a %= p; while (b) { if (b & 1) ans = ans * a % p; a = a * a % p; b >>= 1; } return ans; } LL getPrimitiveRoot(LL p) { static LL factor[100]; LL phi = p - 1; LL tot = 0; LL tmp = phi; for (LL i = 2; i * i <= tmp; i++) { if (tmp % i == 0) { factor[tot++] = i; while (tmp % i == 0) tmp /= i; } } if (tmp > 1) factor[tot++] = tmp; for (LL root = 1; ; root++) { bool flag = true; for (LL i = 0; i < tot; i++) { if (quickPow(root, phi / factor[i], p) == 1) { flag = false; break; } } if (flag) return root; } } void ElGamal(LL p, LL g, LL x, LL k, LL m, LL &y, LL &a, LL &b) { y = quickPow(g, x, p); a = quickPow(g, k, p); b = m * quickPow(y, k, p) % p; } LL ElGamalDecrypt(LL p, LL x, LL a, LL b) { return b * quickPow(a, p - 1 - x, p) % p; } int main() { srand(time(NULL)); LL p, g, x, k, m, y, a, b; p = 998244353; g = getPrimitiveRoot(p); x = rand() % (p - 2) + 1; // 选择 x k = rand() % (p - 2) + 1; // 随机选择 k m = rand() % (p - 2) + 1; // 选择明文 m ElGamal(p, g, x, k, m, y, a, b); cout << "p = " << p << endl; cout << "g = " << g << endl; cout << "x = " << x << endl; cout << "k = " << k << endl; cout << "m = " << m << endl; cout << "y = " << y << endl; cout << "a = " << a << endl; cout << "b = " << b << endl; cout << "Decrypt: " << ElGamalDecrypt(p, x, a, b) << endl; return 0; } ``` 其中,`getPrimitiveRoot` 函数用于求模数 p 的原根,`ElGamal` 函数用于加密明文,`ElGamalDecrypt` 函数用于解密密文。在示例中,我们使用了模数 p 为 998244353,随机选择了 x、k 和明文 m 进行加密,并输出了加密结果和解密结果。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值