本文整理汇总了C#中Org.BouncyCastle.Math.BigInteger.ToByteArrayUnsigned方法的典型用法代码示例。如果您正苦于以下问题:C# BigInteger.ToByteArrayUnsigned方法的具体用法?C# BigInteger.ToByteArrayUnsigned怎么用?C# BigInteger.ToByteArrayUnsigned使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Org.BouncyCastle.Math.BigInteger
的用法示例。
在下文中一共展示了BigInteger.ToByteArrayUnsigned方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SrpConstants
private SrpConstants(string hexN, string decG)
{
N = new BigInteger(hexN, 16);
Nb = N.ToByteArrayUnsigned();
g = new BigInteger(decG, 10);
gb = g.ToByteArrayUnsigned();
kb = ComputeHash(Nb, gb);
k = new BigInteger(1, kb);
kb2 = Xor(ComputeHash(Nb), ComputeHash(gb));
}
示例2: KeyPair
/// <summary>
/// Generates a KeyPair using a BigInteger as a private key.
/// BigInteger is checked for appropriate range.
/// </summary>
public KeyPair(BigInteger bi, bool compressed = false, byte addressType = 0)
{
this.IsCompressedPoint = compressed;
this.AddressType = addressType;
var ps = Org.BouncyCastle.Asn1.Sec.SecNamedCurves.GetByName("secp256k1");
if (bi.CompareTo(ps.N) >= 0 || bi.SignValue <= 0) {
throw new ArgumentException("BigInteger is out of range of valid private keys");
}
byte[] bb = Util.Force32Bytes(bi.ToByteArrayUnsigned());
PrivateKeyBytes = bb;
}
示例3: Encode
internal static void Encode(
BcpgOutputStream bcpgOut,
BigInteger val)
{
bcpgOut.WriteShort((short) val.BitLength);
bcpgOut.Write(val.ToByteArrayUnsigned());
}
示例4: ECPrivateKeyStructure
public ECPrivateKeyStructure(
BigInteger key)
{
this.seq = new DerSequence(
new DerInteger(1),
new DerOctetString(key.ToByteArrayUnsigned()));
}
示例5: ECPrivateKeyStructure
public ECPrivateKeyStructure(
BigInteger key)
{
if (key == null)
throw new ArgumentNullException("key");
this.seq = new DerSequence(
new DerInteger(1),
new DerOctetString(key.ToByteArrayUnsigned()));
}
示例6: ConvertRsaParametersField
private static Byte[] ConvertRsaParametersField(BigInteger n, Int32 size)
{
var byteArrayUnsigned = n.ToByteArrayUnsigned();
if (byteArrayUnsigned.Length == size)
return byteArrayUnsigned;
if (byteArrayUnsigned.Length > size)
throw new ArgumentException("Specified size too small", nameof(size));
var numArray = new Byte[size];
Array.Copy(byteArrayUnsigned, 0, numArray, size - byteArrayUnsigned.Length, byteArrayUnsigned.Length);
return numArray;
}
示例7: TestAddBigInt
public void TestAddBigInt()
{
BlobBuilder builder = new BlobBuilder();
BigInteger value = new BigInteger("12398259028592293582039293420948023");
builder.AddBigIntBlob(value);
byte[] valueBytes = value.ToByteArrayUnsigned();
//Assert.That(valueBytes[0], Is.EqualTo(0));
byte[] expected = new byte[valueBytes.Length + 4];
Array.Copy(valueBytes.Length.ToBytes(), expected, 4);
Array.Copy(valueBytes, 0, expected, 4, valueBytes.Length);
Assert.That(builder.GetBlob(), Is.EqualTo(expected));
}
示例8: AsUnsignedByteArray
/**
* Return the passed in value as an unsigned byte array of specified length, zero-extended as necessary.
*
* @param length desired length of result array.
* @param n value to be converted.
* @return a byte array of specified length, with leading zeroes as necessary given the size of n.
*/
public static byte[] AsUnsignedByteArray(int length, BigInteger n)
{
byte[] bytes = n.ToByteArrayUnsigned();
if (bytes.Length > length)
throw new ArgumentException("standard length exceeded", "n");
if (bytes.Length == length)
return bytes;
byte[] tmp = new byte[length];
Array.Copy(bytes, 0, tmp, tmp.Length - bytes.Length, bytes.Length);
return tmp;
}
示例9: ECPrivateKeyStructure
public ECPrivateKeyStructure(
BigInteger key)
{
// byte[] bytes = key.ToByteArray();
//
// if (bytes[0] == 0)
// {
// byte[] tmp = new byte[bytes.Length - 1];
// Array.Copy(bytes, 1, tmp, 0, tmp.Length);
// bytes = tmp;
// }
byte[] bytes = key.ToByteArrayUnsigned();
seq = new DerSequence(new DerInteger(1), new DerOctetString(bytes));
}
示例10: IntegerToBytes
public static byte[] IntegerToBytes(
BigInteger s,
int qLength)
{
// TODO Add methods to allow writing BigInteger to existing byte array?
byte[] bytes = s.ToByteArrayUnsigned();
if (bytes.Length > qLength)
throw new ArgumentException("s does not fit in specified number of bytes", "s");
if (qLength > bytes.Length)
{
byte[] tmp = new byte[qLength];
Array.Copy(bytes, 0, tmp, tmp.Length - bytes.Length, bytes.Length);
return tmp;
}
return bytes;
}
示例11: IntegerToBytes
public static byte[] IntegerToBytes(BigInteger s, int qLength)
{
byte[] bytes = s.ToByteArrayUnsigned();
if (qLength < bytes.Length)
{
byte[] tmp = new byte[qLength];
Array.Copy(bytes, bytes.Length - tmp.Length, tmp, 0, tmp.Length);
return tmp;
}
else if (qLength > bytes.Length)
{
byte[] tmp = new byte[qLength];
Array.Copy(bytes, 0, tmp, tmp.Length - bytes.Length, bytes.Length);
return tmp;
}
return bytes;
}
示例12: ConvertOutput
public byte[] ConvertOutput(
BigInteger result)
{
byte[] output = result.ToByteArrayUnsigned();
if (forEncryption)
{
int outSize = GetOutputBlockSize();
// TODO To avoid this, create version of BigInteger.ToByteArray that
// writes to an existing array
if (output.Length < outSize) // have ended up with less bytes than normal, lengthen
{
byte[] tmp = new byte[outSize];
output.CopyTo(tmp, tmp.Length - output.Length);
output = tmp;
}
}
return output;
}
示例13: ECPrivateKeyStructure
public ECPrivateKeyStructure(
BigInteger key,
DerBitString publicKey,
Asn1Encodable parameters)
{
if (key == null)
throw new ArgumentNullException("key");
Asn1EncodableVector v = new Asn1EncodableVector(
new DerInteger(1),
new DerOctetString(key.ToByteArrayUnsigned()));
if (parameters != null)
{
v.Add(new DerTaggedObject(true, 0, parameters));
}
if (publicKey != null)
{
v.Add(new DerTaggedObject(true, 1, publicKey));
}
this.seq = new DerSequence(v);
}
示例14: 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);
//.........这里部分代码省略.........
示例15: ConvertRSAParametersField
// TODO Move functionality to more general class
private static byte[] ConvertRSAParametersField(BigInteger n, int size)
{
byte[] bs = n.ToByteArrayUnsigned();
if (bs.Length == size)
return bs;
if (bs.Length > size)
throw new ArgumentException("Specified size too small", "size");
byte[] padded = new byte[size];
Array.Copy(bs, 0, padded, size - bs.Length, bs.Length);
return padded;
}