本文整理汇总了C#中Granados.Mono.Math.BigInteger类的典型用法代码示例。如果您正苦于以下问题:C# BigInteger类的具体用法?C# BigInteger怎么用?C# BigInteger使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
BigInteger类属于Granados.Mono.Math命名空间,在下文中一共展示了BigInteger类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: RSAKeyPair
public RSAKeyPair(BigInteger e, BigInteger d, BigInteger n, BigInteger u, BigInteger p, BigInteger q)
{
_publickey = new RSAPublicKey(e, n);
_d = d;
_u = u;
_p = p;
_q = q;
}
示例2: GenerateNew
public static DSAKeyPair GenerateNew(int bits, Rng random)
{
BigInteger one = new BigInteger(1);
BigInteger[] pq = findRandomStrongPrime(bits, 160, random);
BigInteger p = pq[0], q = pq[1];
BigInteger g = findRandomGenerator(q, p, random);
BigInteger x;
do {
x = BigInteger.GenerateRandom(q.BitCount());
} while ((x < one) || (x > q));
BigInteger y = g.ModPow(x, p);
return new DSAKeyPair(p, g, q, y, x);
}
示例3: LoadSSH1PrivateKey
/// <summary>
/// Read SSH1 private key parameters.
/// </summary>
/// <param name="passphrase">passphrase for decrypt the key file</param>
/// <param name="modulus">private key parameter is set</param>
/// <param name="publicExponent">private key parameter is set</param>
/// <param name="privateExponent">private key parameter is set</param>
/// <param name="primeP">private key parameter is set</param>
/// <param name="primeQ">private key parameter is set</param>
/// <param name="crtCoefficient">private key parameter is set</param>
/// <exception cref="SSHException">failed to parse</exception>
public void LoadSSH1PrivateKey(
string passphrase,
out BigInteger modulus,
out BigInteger publicExponent,
out BigInteger privateExponent,
out BigInteger primeP,
out BigInteger primeQ,
out BigInteger crtCoefficient)
{
PrivateKeyFileFormat format = ProbeFormat();
ISSH1PrivateKeyLoader loader;
if (format == PrivateKeyFileFormat.SSH1)
loader = new SSH1PrivateKeyLoader(keyFile, keyFilePath);
else
throw new SSHException(Strings.GetString("UnsupportedPrivateKeyFormat"));
loader.Load(passphrase, out modulus, out publicExponent, out privateExponent, out primeP, out primeQ, out crtCoefficient);
}
示例4: Load
/// <summary>
/// Read SSH1 private key parameters.
/// </summary>
/// <param name="passphrase">passphrase for decrypt the key file</param>
/// <param name="modulus">private key parameter</param>
/// <param name="publicExponent">private key parameter</param>
/// <param name="privateExponent">private key parameter</param>
/// <param name="primeP">private key parameter</param>
/// <param name="primeQ">private key parameter</param>
/// <param name="crtCoefficient">private key parameter</param>
/// <param name="comment">comment</param>
/// <exception cref="SSHException">failed to parse</exception>
public void Load(
string passphrase,
out BigInteger modulus,
out BigInteger publicExponent,
out BigInteger privateExponent,
out BigInteger primeP,
out BigInteger primeQ,
out BigInteger crtCoefficient,
out string comment)
{
if (keyFile == null)
throw new SSHException("A key file is not loaded yet");
byte[] hdr = Encoding.ASCII.GetBytes(PrivateKeyFileHeader.SSH1_HEADER);
if (!ByteArrayUtil.ByteArrayStartsWith(keyFile, hdr))
throw new SSHException(Strings.GetString("NotValidPrivateKeyFile"));
SSH1DataReader reader = new SSH1DataReader(keyFile);
reader.Read(hdr.Length);
byte[] cipher = reader.Read(2); //first 2 bytes indicates algorithm and next 8 bytes is space
reader.Read(8);
modulus = reader.ReadMPInt();
publicExponent = reader.ReadMPInt();
comment = reader.ReadString();
byte[] prvt = reader.GetRemainingDataView().GetBytes();
//必要なら復号
CipherAlgorithm algo = (CipherAlgorithm)cipher[1];
if (algo != 0) {
Cipher c = CipherFactory.CreateCipher(SSHProtocol.SSH1, algo, SSH1PassphraseToKey(passphrase));
c.Decrypt(prvt, 0, prvt.Length, prvt, 0);
}
SSH1DataReader prvtreader = new SSH1DataReader(prvt);
byte[] mark = prvtreader.Read(4);
if (mark[0] != mark[2] || mark[1] != mark[3])
throw new SSHException(Strings.GetString("WrongPassphrase"));
privateExponent = prvtreader.ReadMPInt();
crtCoefficient = prvtreader.ReadMPInt();
primeP = prvtreader.ReadMPInt();
primeQ = prvtreader.ReadMPInt();
}
示例5: ReadInteger
/// <summary>
/// Read integer.
/// </summary>
/// <param name="bigint">BigInteger instance will be stored if succeeded.</param>
/// <returns>true if succeeded.</returns>
public bool ReadInteger(out BigInteger bigint)
{
BERTagInfo tagInfo = new BERTagInfo();
if (ReadTagInfo(ref tagInfo)
&& tagInfo.IsConstructed == false
&& tagInfo.TagNumber == TAG_INTEGER
&& tagInfo.Length != LENGTH_INDEFINITE
&& tagInfo.Length > 0) {
byte[] buff = new byte[tagInfo.Length];
int len = strm.Read(buff, 0, tagInfo.Length);
if (len == tagInfo.Length) {
bigint = new BigInteger(buff);
return true;
}
}
bigint = null;
return false;
}
示例6: CurveEd25519
/// <summary>
/// Constructor
/// </summary>
public CurveEd25519()
{
_curveName = "edwards25519";
_publicKeyAlgorithm = PublicKeyAlgorithm.ED25519;
_p = new BigInteger(BigIntegerConverter.ParseHex(
// 2^255 - 19
"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffed"
));
_d = BigInteger.Parse(
"37095705934669439343138083508754565189542113879843219016388785533085940283555"
);
_bx = BigInteger.Parse(
"15112221349535400772501151409588531511454012693041857206046113283949847762202"
);
_by = BigInteger.Parse(
"46316835694926478169428394003475163141307993866256225615783033603165251855960"
);
_l = new BigInteger(BigIntegerConverter.ParseHex(
// 2^252 + 0x14def9dea2f79cd65812631a5cf5d3ed
"1000000000000000000000000000000014def9dea2f79cd65812631a5cf5d3ed"
));
}
示例7: RSAPublicKey
public RSAPublicKey(BigInteger exp, BigInteger mod)
{
_e = exp;
_n = mod;
}
示例8: StripPKCS1Pad
/// <summary>
/// Extract an message from the encoded message (EM) described in PKCS#1
/// </summary>
/// <param name="input">encoded message bits</param>
/// <param name="type">type number (1 or 2)</param>
/// <returns>message bits</returns>
public static BigInteger StripPKCS1Pad(BigInteger input, int type)
{
byte[] strip = input.GetBytes();
int stripLen = strip.Length;
int i = 0;
while (true) {
if (i >= stripLen) {
throw new ArgumentException("Invalid EM format");
}
if (strip[i] != 0) {
break;
}
i++;
}
if (strip[i] != type) {
throw new ArgumentException(String.Format("Invalid PKCS1 padding {0}", type));
}
i++;
int padLen = 0;
while (true) {
if (i >= stripLen) {
throw new ArgumentException("Invalid EM format");
}
byte b = strip[i];
if (b == 0) {
break;
}
if (type == 1 && b != 0xff) {
throw new ArgumentException("Invalid PKCS1 padding");
}
padLen++;
i++;
}
if (padLen < 8) {
throw new ArgumentException("Invalid PKCS1 padding");
}
i++; // skip 0x00
if (i >= stripLen) {
throw new ArgumentException("Invalid PKCS1 padding, corrupt data");
}
byte[] val = new byte[stripLen - i];
Buffer.BlockCopy(strip, i, val, 0, val.Length);
return new BigInteger(val);
}
示例9: TestSignatureVerification
// Tests signature verification using test vectors from NIST CAVP.
// http://csrc.nist.gov/groups/STM/cavp/digital-signatures.html
internal static void TestSignatureVerification() {
using (var reader = new System.IO.StreamReader(@"186-3ecdsatestvectors\SigVer.rsp")) {
EllipticCurve curve = null;
byte[] msg = null;
BigInteger qx = null;
BigInteger qy = null;
BigInteger r = null;
BigInteger s = null;
string result = null;
int testCount = 0;
while (true) {
string line = reader.ReadLine();
if (line == null) {
break;
}
var match = System.Text.RegularExpressions.Regex.Match(line, @"\[([-\w]+),(SHA-\d+)\]");
if (match.Success) {
string curveName = "nist" + match.Groups[1].Value.ToLowerInvariant().Replace("-", "");
curve = FindByName(curveName);
if (curve != null) {
using (var hashFunc = ECDSAHashAlgorithmChooser.Choose(curve)) {
var hashName = "SHA-" + hashFunc.HashSize.ToString();
if (hashName == match.Groups[2].Value) {
Debug.WriteLine("Test " + curve.CurveName);
}
else {
// hash function doesn't match
curve = null;
}
}
}
msg = null;
qx = qy = r = s = null;
result = null;
testCount = 0;
continue;
}
if (line.StartsWith("Msg = ") && curve != null) {
msg = BigIntegerConverter.ParseHex(line.Substring(6).Trim());
continue;
}
if (line.StartsWith("Qx = ") && curve != null) {
qx = new BigInteger(BigIntegerConverter.ParseHex(line.Substring(5).Trim()));
continue;
}
if (line.StartsWith("Qy = ") && curve != null) {
qy = new BigInteger(BigIntegerConverter.ParseHex(line.Substring(5).Trim()));
continue;
}
if (line.StartsWith("R = ") && curve != null) {
r = new BigInteger(BigIntegerConverter.ParseHex(line.Substring(4).Trim()));
continue;
}
if (line.StartsWith("S = ") && curve != null) {
s = new BigInteger(BigIntegerConverter.ParseHex(line.Substring(4).Trim()));
continue;
}
if (line.StartsWith("Result = ") && curve != null) {
result = line.Substring(9, 1);
if (msg != null && qx != null && qy != null && r != null && s != null) {
var pk = new ECDSAPublicKey(curve, new ECPoint(qx, qy));
var buf = new SSH2DataWriter();
buf.WriteBigInteger(r);
buf.WriteBigInteger(s);
var sig = buf.ToByteArray();
string verRes;
try {
pk.Verify(sig, msg);
verRes = "P";
}
catch (VerifyException) {
verRes = "F";
}
if (verRes != result) {
throw new Exception("verification result doesn't match");
}
++testCount;
Debug.WriteLine("Pass #{0}", testCount);
}
msg = null;
qx = qy = r = s = null;
result = null;
}
}
}
}
示例10: SignCore
private BigInteger SignCore(BigInteger input, BigInteger pe, BigInteger qe)
{
BigInteger p2 = (input % _p).ModPow(pe, _p);
BigInteger q2 = (input % _q).ModPow(qe, _q);
if (p2 == q2)
return p2;
BigInteger k;
if (q2 > p2) {
k = (q2 - p2) % _q;
}
else {
// add multiple of _q greater than _p
BigInteger d = _q + (_p / _q) * _q;
k = (d + q2 - p2) % _q;
}
k = (k * _u) % _q;
BigInteger result = k * _p + p2;
return result;
}
示例11: DSAPublicKey
public DSAPublicKey(BigInteger p, BigInteger g, BigInteger q, BigInteger y)
{
_p = p;
_g = g;
_q = q;
_y = y;
}
示例12: DSAKeyPair
public DSAKeyPair(BigInteger p, BigInteger g, BigInteger q, BigInteger y, BigInteger x)
{
_publickey = new DSAPublicKey(p, g, q, y);
_x = x;
}
示例13: PointDouble
/// <summary>
/// Point dooubling over the curve
/// </summary>
private bool PointDouble(
BigInteger.ModulusRing ring,
ECPoint p1,
out ECPoint p3) {
if (p1 is ECPointAtInfinity) {
p3 = p1;
return true;
}
if (p1.Y == 0) {
p3 = new ECPointAtInfinity();
return true;
}
// x3 = {(3 * x1^2 + a)/(2 * y1)}^2 - (2 * x1)
// y3 = {(3 * x1^2 + a)/(2 * y1)} * (x1 - x3) - y1
try {
BigInteger x1 = p1.X;
BigInteger y1 = p1.Y;
BigInteger x1_2 = ring.Multiply(x1, x1);
BigInteger lambda = ring.Multiply(x1_2 + x1_2 + x1_2 + a, (y1 + y1).ModInverse(p));
BigInteger x3 = ring.Difference(ring.Multiply(lambda, lambda), x1 + x1);
BigInteger y3 = ring.Difference(ring.Multiply(lambda, ring.Difference(x1, x3)), y1);
p3 = new ECPoint(x3, y3);
return true;
}
catch (Exception) {
p3 = null;
return false;
}
}
示例14: findRandomGenerator
private static BigInteger findRandomGenerator(BigInteger order, BigInteger modulo, Rng random)
{
BigInteger one = new BigInteger(1);
BigInteger aux = modulo - new BigInteger(1);
BigInteger t = aux % order;
BigInteger generator;
if (AsUInt64(t) != 0) {
return null;
}
t = aux / order;
while (true) {
generator = BigInteger.GenerateRandom(modulo.BitCount());
generator = generator % modulo;
generator = generator.ModPow(t, modulo);
if (generator != one)
break;
}
aux = generator.ModPow(order, modulo);
if (aux != one) {
return null;
}
return generator;
}
示例15: PointMul
/// <summary>
/// Calculate point multiplication
/// </summary>
/// <param name="k1">scalar</param>
/// <param name="k2">scalar</param>
/// <param name="t">point</param>
/// <param name="infinityToNull">
/// if result was point-at-infinity, and this parameter was true,
/// null is returned instead of <see cref="ECPointAtInfinity"/>.
/// </param>
/// <returns>point on the curve, point at infinity, or null if failed</returns>
public override ECPoint PointMul(BigInteger k1, BigInteger k2, ECPoint t, bool infinityToNull) {
BigInteger.ModulusRing ring = new BigInteger.ModulusRing(p);
BigInteger k = ring.Multiply(k1, k2);
return PointMul(k, t, infinityToNull);
}