觅风论坛
标题:
非对称加密RSA C#加密源码
[打印本页]
作者:
小枫嘎嘎
时间:
2024-6-7 16:20
标题:
非对称加密RSA C#加密源码
RSA C#加密源码,不知道写的对不对,请各位指正,只是略微搞懂个原理。还有就是里面肯定有更简洁的方式找公匙私匙,也欢迎大佬补充。
注意:使用RSA加密时密匙必须大于加密内容,否则输出错误信息。
using System;
using System.Collections.Generic;
using System.Numerics;
using System.Security.Cryptography;
public class HelloWorld
{
public static void Main(string[] args)
{
Console.WriteLine("STARTED");
for (int i = 0; i < 1; i++)
{
var primes = GenerateLargePrimes(4096);
CalcKey(primes.Item1, primes.Item2);
}
}
private static (BigInteger, BigInteger) GenerateLargePrimes(int bitLength)
{
BigInteger firstPrime = _GenerateLargePrime(bitLength);
BigInteger secondPrime = _GenerateLargePrime(bitLength);
return (firstPrime, secondPrime);
}
private static BigInteger _GenerateLargePrime(int bitLength)
{
using (var rng = new RNGCryptoServiceProvider())
{
BigInteger prime;
byte[] bytes = new byte[bitLength / 8];
do
{
rng.GetBytes(bytes);
prime = new BigInteger(bytes);
prime = BigInteger.Abs(prime);
prime |= BigInteger.One; // 确保是奇数
} while (!IsProbablyPrime(prime, 10)); // 使用 Miller-Rabin 测试
return prime;
}
}
private static bool IsProbablyPrime(BigInteger source, int certainty)
{
if (source == 2 || source == 3)
return true;
if (source < 2 || source % 2 == 0)
return false;
BigInteger d = source - 1;
int s = 0;
while (d % 2 == 0)
{
d /= 2;
s += 1;
}
for (int i = 0; i < certainty; i++)
{
BigInteger a = RandomIntegerBelow(source - 2) + 1;
BigInteger temp = d;
BigInteger mod = BigInteger.ModPow(a, temp, source);
if (mod == 1 || mod == source - 1)
continue;
for (int j = 0; j < s - 1; j++)
{
mod = BigInteger.ModPow(mod, 2, source);
if (mod == 1)
return false;
if (mod == source - 1)
break;
}
if (mod != source - 1)
return false;
}
return true;
}
private static BigInteger RandomIntegerBelow(BigInteger n)
{
using (var rng = new RNGCryptoServiceProvider())
{
byte[] bytes = n.ToByteArray();
BigInteger r;
do
{
rng.GetBytes(bytes);
r = new BigInteger(bytes);
} while (r >= n || r <= 0);
return r;
}
}
public static void CalcKey(BigInteger p, BigInteger q)
{
BigInteger n = p * q;
BigInteger phi = (p - 1) * (q - 1);
BigInteger e = FindCoprime(phi);
BigInteger d = ModInverse(e, phi);
Console.WriteLine("Public key (e, n): (" + e + ", " + n + ")");
Console.WriteLine("Private key (d, n): (" + d + ", " + n + ")");
BigInteger message = BigInteger.Parse("9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999");
BigInteger encrypted = Encrypt(message, e, n);
Console.WriteLine("Encrypted: " + encrypted);
BigInteger decrypted = Decrypt(encrypted, d, n);
Console.WriteLine("Decrypted: " + decrypted);
}
public static BigInteger GCD(BigInteger a, BigInteger b)
{
if (b == 0)
return a;
return GCD(b, a % b);
}
public static BigInteger FindCoprime(BigInteger r)
{
BigInteger e = 2;
while (e < r)
{
if (GCD(e, r) == 1)
return e;
e++;
}
return 1;
}
public static BigInteger ModInverse(BigInteger e, BigInteger r)
{
BigInteger x, y;
BigInteger gcd = ExtendedGCD(e, r, out x, out y);
if (gcd != 1)
{
throw new Exception("Inverse doesn't exist.");
}
else
{
return (x % r + r) % r;
}
}
public static BigInteger ExtendedGCD(BigInteger a, BigInteger b, out BigInteger x, out BigInteger y)
{
if (a == 0)
{
x = 0;
y = 1;
return b;
}
BigInteger x1, y1;
BigInteger gcd = ExtendedGCD(b % a, a, out x1, out y1);
x = y1 - (b / a) * x1;
y = x1;
return gcd;
}
public static BigInteger Encrypt(BigInteger m, BigInteger e, BigInteger N)
{
return BigInteger.ModPow(m, e, N);
}
public static BigInteger Decrypt(BigInteger c, BigInteger d, BigInteger N)
{
return BigInteger.ModPow(c, d, N);
}
}
复制代码
作者:
老子
时间:
2024-6-8 08:22
我知道错了,感谢大神分享
作者:
咬牙坚持
时间:
2024-6-9 00:24
66666666666666666666
作者:
2549051527
时间:
2024-6-9 16:27
提示:
作者被禁止或删除 内容自动屏蔽
作者:
gwm231
时间:
2024-6-10 08:29
不错不错 支持下
作者:
孤独
时间:
2024-6-11 00:31
非常不错,感谢分享!
作者:
2098817979
时间:
2024-6-11 14:57
9999999999999999
作者:
好吧你又赢了
时间:
2024-6-12 05:23
人设人阿松大
作者:
汨黎
时间:
2024-6-12 19:49
谢谢分享,下载测试
作者:
舞步
时间:
2024-6-13 10:15
支持,赞
作者:
360403967
时间:
2024-6-14 00:41
碉堡了!
作者:
qqq00123
时间:
2024-6-14 12:23
6666666666
作者:
sxy19931021
时间:
2024-6-15 00:04
提示:
作者被禁止或删除 内容自动屏蔽
作者:
舞步
时间:
2024-6-15 11:46
感谢这个i资源
作者:
asd3186789
时间:
2024-6-15 23:27
不错不错 支持下
作者:
舞步
时间:
2024-6-16 11:09
很不错的哦,支持,加油
作者:
张庆伟23
时间:
2024-6-16 14:29
还不错觅风论坛欢迎你
作者:
林哥
时间:
2024-6-16 17:49
好好好好的我要下载看看看
作者:
张庆伟23
时间:
2024-6-16 21:10
我今天才找到这个论坛,非常高兴,加入到觅风老师的论坛
作者:
shjia24
时间:
2024-6-17 00:30
提示:
作者被禁止或删除 内容自动屏蔽
作者:
好萌哦
时间:
2024-6-17 03:50
好像还不错!
作者:
1225061801
时间:
2024-6-17 06:42
还不错觅风论坛欢迎你
作者:
sxy19931021
时间:
2024-6-17 09:33
提示:
作者被禁止或删除 内容自动屏蔽
作者:
哦美国
时间:
2024-6-17 12:25
11111111111111111111111111
作者:
刘小凯
时间:
2024-6-17 15:17
666学习了!!
作者:
汨黎
时间:
2024-6-17 18:08
抢楼了,前排第一次啊
作者:
13778890079
时间:
2024-6-17 22:44
谢谢楼主,,,收藏ing
作者:
1811581892
时间:
2024-6-18 03:19
支持!!!!!!
作者:
萨拉空军司令的
时间:
2024-6-18 07:55
学习一下!十分感谢
作者:
咬牙坚持
时间:
2024-6-18 12:31
路过还不错
作者:
萨拉空军司令的
时间:
2024-6-18 17:06
抢楼了,前排第一次啊
作者:
123456
时间:
2024-6-19 08:29
多上传一点源码
作者:
啦啦啦啦啦啦
时间:
2024-6-19 23:52
感谢感谢分享
作者:
微风
时间:
2024-6-20 15:15
看看,到底好不好,想学学看看
作者:
asd3186789
时间:
2024-6-21 06:38
这就是传说中的好资源吗?赶紧看看去!
作者:
养猪大户
时间:
2024-6-21 22:01
支持,赞
作者:
萨拉空军司令的
时间:
2024-6-22 01:11
看看,到底好不好,想学学看看
作者:
张庆伟23
时间:
2024-6-22 04:21
这就是传说中的好资源吗?赶紧看看去!
作者:
1354541
时间:
2024-6-22 07:31
谢谢楼主,,,收藏ing
作者:
565562216
时间:
2024-6-22 10:41
不错哦 喜欢 嘿嘿
作者:
a1031399528a
时间:
2024-6-22 13:52
非常不错,感谢分享!
作者:
wang798403789
时间:
2024-6-22 16:04
感谢感谢分享
作者:
天汇
时间:
2024-6-22 18:17
我知道错了,感谢大神分享
作者:
天汇
时间:
2024-6-22 20:30
666学习了!!
作者:
天汇
时间:
2024-6-22 22:42
学习了!!!!
作者:
lijianan12300
时间:
2024-6-23 00:55
支持!!!!!!
作者:
SDS
时间:
2024-6-23 13:58
我要下载试试,我要下载试试...
作者:
a32d321as
时间:
2024-6-24 03:01
谢谢大人的分享
作者:
hui861140
时间:
2024-6-24 16:04
不错不错 支持下
作者:
wang798403789
时间:
2024-6-25 05:07
路过还不错
作者:
风月
时间:
2024-6-25 18:09
支持!!!!前排!!!!
作者:
图个简单
时间:
2024-6-26 08:25
666666666666666666666666
作者:
汨黎
时间:
2024-6-26 22:41
感谢这个i资源
作者:
好萌哦
时间:
2024-6-27 12:57
学习下 学习下 学习下
作者:
我爱你苏根
时间:
2024-6-28 03:12
谢谢分享!~
作者:
小粑
时间:
2024-6-28 17:28
看帖子的要发表下看法
欢迎光临 觅风论坛 (https://www.eyyba.com/)
Powered by Discuz! X3.4