本文整理汇总了C#中Org.BouncyCastle.Math.BigInteger.ToString方法的典型用法代码示例。如果您正苦于以下问题:C# BigInteger.ToString方法的具体用法?C# BigInteger.ToString怎么用?C# BigInteger.ToString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Org.BouncyCastle.Math.BigInteger
的用法示例。
在下文中一共展示了BigInteger.ToString方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: BigIntegerRSAPublicKey
public BigIntegerRSAPublicKey(RSAParameters rsaParameters)
{
this.Modulus = new BigInteger(1, rsaParameters.Modulus);
this.modulusAsString = Modulus.ToString(10);
this.Exponent = new BigInteger(1, rsaParameters.Exponent);
this.exponentAsString = Exponent.ToString(10);
}
示例2: DoTestKey
private void DoTestKey(BigInteger keyId, string passphrase, bool utf8)
{
PgpSecretKeyRingBundle secretKeyRing = LoadSecretKeyCollection("secring.gpg");
PgpSecretKeyRing secretKey = secretKeyRing.GetSecretKeyRing(keyId.LongValue);
Assert.NotNull(secretKey, "Could not locate secret keyring with Id=" + keyId.ToString(16));
PgpSecretKey key = secretKey.GetSecretKey();
Assert.NotNull(key, "Could not locate secret key!");
try
{
char[] pass = passphrase.ToCharArray();
PgpPrivateKey privateKey = utf8
? key.ExtractPrivateKeyUtf8(pass)
: key.ExtractPrivateKey(pass);
Assert.IsTrue(privateKey.KeyId == keyId.LongValue);
}
catch (PgpException e)
{
throw new PgpException("Password incorrect!", e);
}
// all fine!
}
示例3: H2hash
public static string H2hash(BigInteger qid, BigInteger p)
{
// H2 je RiPEMD-120: točka iz polja -> niz bita mod p
string tocka = qid.ToString();
RIPEMD160Managed crypt = new RIPEMD160Managed();
StringBuilder hash = new StringBuilder();
byte[] crypto = crypt.ComputeHash(Encoding.UTF8.GetBytes(tocka), 0, Encoding.UTF8.GetByteCount(tocka));
foreach (byte theByte in crypto)
{
hash.Append(theByte.ToString("x2"));
}
BigInteger hsh = new BigInteger(hash.ToString(), 16);
// hash mod n
BigInteger c = hsh.DivideAndRemainder(p)[1];
return c.ToString();
}
示例4: Miller
/// <summary>
/// millerov agoritam
/// </summary>
/// <param name="a">točka</param>
/// <param name="b">točka</param>
/// <param name="m">red grupe</param>
/// <param name="p">red polja, prim</param>
/// <returns></returns>
private static BigInteger Miller(FpPoint P, FpPoint Q, BigInteger m, BigInteger prim)
{
// Millerov algoritam
string mBin = m.ToString(2);
BigInteger t1 = new BigInteger("1", 10);
BigInteger t2 = new BigInteger("1", 10);
FpPoint V = P;
for (int i = 0; i < m.BitLength; i++)
{
V = (FpPoint)V.Twice();
t1 = t1.ModPow(new BigInteger("2", 10), prim).Multiply(MLF(V, V, Q));
if (mBin[i] == '1')
{
t1 = t1.Multiply(MLF(V, P, Q));
V = (FpPoint)V.Add(P);
}
}
return t1;
}
示例5: Perform
public ITestResult Perform()
{
BigInteger r = new BigInteger("8f79a582513df84dc247bcb624340cc0e5a34c4324a20ce7fe3ab8ff38a9db71",16);
BigInteger s = new BigInteger("7508d22fd6cbb45efd438cb875e43f137247088d0f54b29a7c91f68a65b5fa85",16);
Gost3410ParametersGenerator pGen = new Gost3410ParametersGenerator();
pGen.Init(1024, 2, init_random);
Gost3410Parameters parameters = pGen.GenerateParameters();
if (!pValue.Equals(parameters.P) || !qValue.Equals(parameters.Q))
{
return new SimpleTestResult(false, Name + ": p or q wrong");
}
Gost3410KeyPairGenerator Gost3410KeyGen = new Gost3410KeyPairGenerator();
Gost3410KeyGenerationParameters genParam = new Gost3410KeyGenerationParameters(keyRandom, parameters);
Gost3410KeyGen.Init(genParam);
AsymmetricCipherKeyPair pair = Gost3410KeyGen.GenerateKeyPair();
ParametersWithRandom param = new ParametersWithRandom(pair.Private, random);
Gost3410Signer Gost3410 = new Gost3410Signer();
Gost3410.Init(true, param);
BigInteger[] sig = Gost3410.GenerateSignature(hashmessage);
if (!r.Equals(sig[0]))
{
return new SimpleTestResult(false, Name
+ ": r component wrong." + SimpleTest.NewLine
+ " expecting: " + r.ToString(16) + SimpleTest.NewLine
+ " got : " + sig[0].ToString(16));
}
if (!s.Equals(sig[1]))
{
return new SimpleTestResult(false, Name
+ ": s component wrong." + SimpleTest.NewLine
+ " expecting: " + s.ToString(16) + SimpleTest.NewLine
+ " got : " + sig[1].ToString(16));
}
Gost3410.Init(false, pair.Public);
if (Gost3410.VerifySignature(hashmessage, sig[0], sig[1]))
{
return new SimpleTestResult(true, Name + ": Okay");
}
else
{
return new SimpleTestResult(false, Name + ": verification fails");
}
}
示例6: TestToString
public void TestToString()
{
string s = "12345667890987654321";
Assert.AreEqual(s, new BigInteger(s).ToString());
Assert.AreEqual(s, new BigInteger(s, 10).ToString(10));
Assert.AreEqual(s, new BigInteger(s, 16).ToString(16));
for (int i = 0; i < 100; ++i)
{
BigInteger n = new BigInteger(i, random);
Assert.AreEqual(n, new BigInteger(n.ToString(2), 2));
Assert.AreEqual(n, new BigInteger(n.ToString(10), 10));
Assert.AreEqual(n, new BigInteger(n.ToString(16), 16));
}
}
示例7: TestDivideAndRemainder
public void TestDivideAndRemainder()
{
// TODO More basic tests
BigInteger n = new BigInteger(48, random);
BigInteger[] qr = n.DivideAndRemainder(one);
Assert.AreEqual(n, qr[0]);
Assert.AreEqual(zero, qr[1]);
for (int rep = 0; rep < 10; ++rep)
{
BigInteger a = new BigInteger(100 - rep, 0, random);
BigInteger b = new BigInteger(100 + rep, 0, random);
BigInteger c = new BigInteger(10 + rep, 0, random);
BigInteger d = a.Multiply(b).Add(c);
BigInteger[] es = d.DivideAndRemainder(a);
Assert.AreEqual(b, es[0]);
Assert.AreEqual(c, es[1]);
}
// Special tests for power of two since uses different code path internally
for (int i = 0; i < 100; ++i)
{
int shift = random.Next(64);
BigInteger a = one.ShiftLeft(shift);
BigInteger b = new BigInteger(64 + random.Next(64), random);
BigInteger bShift = b.ShiftRight(shift);
BigInteger bMod = b.And(a.Subtract(one));
string data = "shift=" + shift +", b=" + b.ToString(16);
qr = b.DivideAndRemainder(a);
Assert.AreEqual(bShift, qr[0], data);
Assert.AreEqual(bMod, qr[1], data);
qr = b.DivideAndRemainder(a.Negate());
Assert.AreEqual(bShift.Negate(), qr[0], data);
Assert.AreEqual(bMod, qr[1], data);
qr = b.Negate().DivideAndRemainder(a);
Assert.AreEqual(bShift.Negate(), qr[0], data);
Assert.AreEqual(bMod.Negate(), qr[1], data);
qr = b.Negate().DivideAndRemainder(a.Negate());
Assert.AreEqual(bShift, qr[0], data);
Assert.AreEqual(bMod.Negate(), qr[1], data);
}
}
示例8: TestDivide
public void TestDivide()
{
for (int i = -5; i <= 5; ++i)
{
try
{
val(i).Divide(zero);
Assert.Fail("expected ArithmeticException");
}
catch (ArithmeticException) {}
}
int product = 1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9;
int productPlus = product + 1;
BigInteger bigProduct = val(product);
BigInteger bigProductPlus = val(productPlus);
for (int divisor = 1; divisor < 10; ++divisor)
{
// Exact division
BigInteger expected = val(product / divisor);
Assert.AreEqual(expected, bigProduct.Divide(val(divisor)));
Assert.AreEqual(expected.Negate(), bigProduct.Negate().Divide(val(divisor)));
Assert.AreEqual(expected.Negate(), bigProduct.Divide(val(divisor).Negate()));
Assert.AreEqual(expected, bigProduct.Negate().Divide(val(divisor).Negate()));
expected = val((product + 1)/divisor);
Assert.AreEqual(expected, bigProductPlus.Divide(val(divisor)));
Assert.AreEqual(expected.Negate(), bigProductPlus.Negate().Divide(val(divisor)));
Assert.AreEqual(expected.Negate(), bigProductPlus.Divide(val(divisor).Negate()));
Assert.AreEqual(expected, bigProductPlus.Negate().Divide(val(divisor).Negate()));
}
for (int rep = 0; rep < 10; ++rep)
{
BigInteger a = new BigInteger(100 - rep, 0, random);
BigInteger b = new BigInteger(100 + rep, 0, random);
BigInteger c = new BigInteger(10 + rep, 0, random);
BigInteger d = a.Multiply(b).Add(c);
BigInteger e = d.Divide(a);
Assert.AreEqual(b, e);
}
// Special tests for power of two since uses different code path internally
for (int i = 0; i < 100; ++i)
{
int shift = random.Next(64);
BigInteger a = one.ShiftLeft(shift);
BigInteger b = new BigInteger(64 + random.Next(64), random);
BigInteger bShift = b.ShiftRight(shift);
string data = "shift=" + shift +", b=" + b.ToString(16);
Assert.AreEqual(bShift, b.Divide(a), data);
Assert.AreEqual(bShift.Negate(), b.Divide(a.Negate()), data);
Assert.AreEqual(bShift.Negate(), b.Negate().Divide(a), data);
Assert.AreEqual(bShift, b.Negate().Divide(a.Negate()), data);
}
// Regression
{
int shift = 63;
BigInteger a = one.ShiftLeft(shift);
BigInteger b = new BigInteger(1, Hex.Decode("2504b470dc188499"));
BigInteger bShift = b.ShiftRight(shift);
string data = "shift=" + shift +", b=" + b.ToString(16);
Assert.AreEqual(bShift, b.Divide(a), data);
Assert.AreEqual(bShift.Negate(), b.Divide(a.Negate()), data);
// Assert.AreEqual(bShift.Negate(), b.Negate().Divide(a), data);
Assert.AreEqual(bShift, b.Negate().Divide(a.Negate()), data);
}
}
示例9: MLF
// Miller "function"
private static BigInteger MLF(FpPoint P, FpPoint R, FpPoint Q)
{
if (!P.Equals(R))
{
if (P.X.Equals(R.X))
{
return Q.X.Subtract(P.X).ToBigInteger();
}
else {
BigInteger l = R.Y.Subtract(P.Y).Divide((R.X.Subtract(P.X))).ToBigInteger();
return Q.Y.Subtract(P.Y).ToBigInteger().Subtract(l.Multiply((Q.X.Subtract(P.X).ToBigInteger())));
}
}
else
{
// z*y^2=d*x^3+a*x+b -> derivacija po x -> 3*x^2+a
BigInteger brojnik = new BigInteger("3", 10).Multiply(P.X.ToBigInteger().Pow(2));
// z*y^2=d*x^3+a*x+b -> derivacija po y -> 2*y
BigInteger nazivnik = new BigInteger("2", 10).Multiply(P.Y.ToBigInteger());
if (nazivnik.ToString(10) == "0")
{
return (Q.X.ToBigInteger().Subtract(P.X.ToBigInteger()));
}
else
{
double koef = (double)(brojnik.IntValue / nazivnik.IntValue);
double rez = Q.Y.Subtract(P.Y).ToBigInteger().IntValue - koef * (Q.X.Subtract(P.X).ToBigInteger().IntValue);
return new BigInteger(rez.ToString(), 10);
}
}
}
示例10: CreateCertificate
//.........这里部分代码省略.........
s_certGenerator.AddExtension(X509Extensions.SubjectAlternativeName, true, new DerSequence(subjectAlternativeNamesAsAsn1EncodableList.ToArray()));
}
else
{
if (subjectAlternativeNames.Length > 1)
{
var subjectAlternativeNamesAsAsn1EncodableList = new Asn1EncodableVector();
// Only add a SAN for the user if there are any
for (int i = 1; i < subjectAlternativeNames.Length; i++)
{
if (!string.IsNullOrWhiteSpace(subjectAlternativeNames[i]))
{
Asn1EncodableVector otherNames = new Asn1EncodableVector();
otherNames.Add(new DerObjectIdentifier(_upnObjectId));
otherNames.Add(new DerTaggedObject(true, 0, new DerUtf8String(subjectAlternativeNames[i])));
Asn1Object genName = new DerTaggedObject(false, 0, new DerSequence(otherNames));
subjectAlternativeNamesAsAsn1EncodableList.Add(genName);
}
}
s_certGenerator.AddExtension(X509Extensions.SubjectAlternativeName, true, new DerSequence(subjectAlternativeNamesAsAsn1EncodableList));
}
}
}
// Our CRL Distribution Point has the serial number in the query string to fool Windows into doing a fresh query
// rather than using a cached copy of the CRL in the case where the CRL has been previously accessed before
var crlDistributionPoints = new DistributionPoint[2] {
new DistributionPoint(new DistributionPointName(
new GeneralNames(new GeneralName(
GeneralName.UniformResourceIdentifier, string.Format("{0}?serialNum={1}", _crlUri, serialNum.ToString(radix: 16))))),
null,
null),
new DistributionPoint(new DistributionPointName(
new GeneralNames(new GeneralName(
GeneralName.UniformResourceIdentifier, string.Format("{0}?serialNum={1}", _crlUri, serialNum.ToString(radix: 16))))),
null,
new GeneralNames(new GeneralName(authorityX509Name)))
};
var revocationListExtension = new CrlDistPoint(crlDistributionPoints);
s_certGenerator.AddExtension(X509Extensions.CrlDistributionPoints, false, revocationListExtension);
X509Certificate cert = s_certGenerator.Generate(_authorityKeyPair.Private, _random);
switch (certificateCreationSettings.ValidityType)
{
case CertificateValidityType.Revoked:
RevokeCertificateBySerialNumber(serialNum.ToString(radix: 16));
break;
case CertificateValidityType.Expired:
break;
default:
EnsureCertificateIsValid(cert);
break;
}
// For now, given that we don't know what format to return it in, preserve the formats so we have
// the flexibility to do what we need to
X509CertificateContainer container = new X509CertificateContainer();
X509CertificateEntry[] chain = new X509CertificateEntry[1];
chain[0] = new X509CertificateEntry(cert);
示例11: Generate
/// <summary>
/// Generate a set of M-of-N parts for a specific private key.
/// If desiredPrivKey is null, then a random key will be selected.
/// </summary>
public void Generate(int PartsNeededToDecode, int PartsToGenerate, byte[] desiredPrivKey)
{
if (PartsNeededToDecode > PartsToGenerate) {
throw new ApplicationException("Number of parts needed exceeds number of parts to generate.");
}
if (PartsNeededToDecode > 8 || PartsToGenerate > 8) {
throw new ApplicationException("Maximum number of parts is 8");
}
if (PartsNeededToDecode < 1 || PartsToGenerate < 1) {
throw new ApplicationException("Minimum number of parts is 1");
}
if (desiredPrivKey != null && desiredPrivKey.Length != 32) {
throw new ApplicationException("Desired private key must be 32 bytes");
}
KeyParts.Clear();
decodedKeyParts.Clear();
SecureRandom sr = new SecureRandom();
// Get 8 random big integers into v[i].
byte[][] vvv = new byte[8][];
BigInteger[] v = new BigInteger[8];
for (int i = 0; i < 8; i++) {
byte[] b = new byte[32];
sr.NextBytes(b, 0, 32);
// For larger values of i, chop off some most-significant-bits to prevent overflows as they are
// multiplied with increasingly larger factors.
if (i >= 7) {
b[0] &= 0x7f;
}
v[i] = new BigInteger(1, b);
Debug.WriteLine(String.Format("v({0})={1}", i, v[i].ToString()));
}
// if a certain private key is desired, then specify it.
if (desiredPrivKey != null) {
// replace v[0] with xor(v[1...7]) xor desiredPrivKey
BigInteger newv0 = BigInteger.Zero;
for (int i=1; i<PartsNeededToDecode; i++) {
newv0 = newv0.Xor(v[i]);
}
v[0] = newv0.Xor(new BigInteger(1,desiredPrivKey));
}
// Generate the expected private key from all the parts
BigInteger privkey = new BigInteger("0");
for (int i = 0; i < PartsNeededToDecode; i++) {
privkey = privkey.Xor(v[i]);
}
// Get the bitcoin address
byte[] keybytes = privkey.ToByteArrayUnsigned();
// make sure we have 32 bytes, we'll need it
if (keybytes.Length < 32) {
byte[] array32 = new byte[32];
Array.Copy(keybytes, 0, array32, 32 - keybytes.Length, keybytes.Length);
keybytes = array32;
}
KeyPair = new KeyPair(keybytes);
byte[] checksum = Util.ComputeSha256(BitcoinAddress);
// Generate the parts
for (int i = 0; i < PartsToGenerate; i++) {
BigInteger total = new BigInteger("0");
for (int j = 0; j < PartsNeededToDecode; j++) {
int factor = 1;
for (int ii = 0; ii <= i; ii++) factor = factor * (j + 1);
BigInteger bfactor = new BigInteger(factor.ToString());
total = total.Add(v[j].Multiply(bfactor));
}
Debug.WriteLine(String.Format(" pc{0}={1}", i, total.ToString()));
byte[] parts = new byte[39];
parts[0] = 0x4f;
parts[1] = (byte)(0x93 + PartsNeededToDecode);
int parts23 = (((checksum[0] << 8) + checksum[1]) & 0x1ff);
Debug.WriteLine("checksum " + parts23.ToString());
parts23 += 0x6000;
parts23 += (i << 9);
byte[] btotal = total.ToByteArrayUnsigned();
for (int jj = 0; jj < btotal.Length; jj++) {
parts[jj + 4 + (35 - btotal.Length)] = btotal[jj];
}
parts[2] = (byte)((parts23 & 0xFF00) >> 8);
//.........这里部分代码省略.........
示例12: IncrementSerial
/// <summary>
/// Increments the serial number in the serial file
/// </summary>
public void IncrementSerial()
{
String txt = File.ReadAllText(SerialFile);
var bi = new BigInteger(txt, SerialNumberRadix);
bi = bi.Add(BigInteger.One);
File.WriteAllText(SerialFile, bi.ToString(SerialNumberRadix));
}
示例13: TestToString
public void TestToString()
{
string s = "12345667890987654321";
Assert.AreEqual(s, new BigInteger(s).ToString());
Assert.AreEqual(s, new BigInteger(s, 10).ToString(10));
Assert.AreEqual(s, new BigInteger(s, 16).ToString(16));
for (int i = 0; i < 100; ++i)
{
BigInteger n = new BigInteger(i, random);
Assert.AreEqual(n, new BigInteger(n.ToString(2), 2));
Assert.AreEqual(n, new BigInteger(n.ToString(10), 10));
Assert.AreEqual(n, new BigInteger(n.ToString(16), 16));
}
// Radix version
int[] radices = new int[] { 2, 8, 10, 16 };
int trials = 256;
BigInteger[] tests = new BigInteger[trials];
for (int i = 0; i < trials; ++i)
{
int len = random.Next(i + 1);
tests[i] = new BigInteger(len, random);
}
foreach (int radix in radices)
{
for (int i = 0; i < trials; ++i)
{
BigInteger n1 = tests[i];
string str = n1.ToString(radix);
BigInteger n2 = new BigInteger(str, radix);
Assert.AreEqual(n1, n2);
}
}
}