public static byte[] IntegerToBytes( BigInteger s, int qLength) { byte[] bytes = s.ToByteArray(); if (qLength < bytes.Length) { byte[] tmp = new byte[qLength]; Array.Copy(bytes, bytes.Length - tmp.Length, tmp, 0, tmp.Length); return tmp; } if (qLength > bytes.Length) { byte[] tmp = new byte[qLength]; Array.Copy(bytes, 0, tmp, tmp.Length - bytes.Length, bytes.Length); return tmp; } return bytes; }
public DerInteger( BigInteger value) { if (value == null) throw new ArgumentNullException("value"); bytes = value.ToByteArray(); }
private static string packInto32(BigInteger b) { var arr = b.ToByteArray (); var start = arr.Length == 33 ? 1 : 0; var len = Math.Min (arr.Length, 32); var result = new byte[32]; /* Make sure the integer b takes 32 bytes. */ Array.Copy (arr, start, result, 32 - len, len); /* Convert the array to a hexadecimal string. */ return BitConverter.ToString (result).Replace ("-", string.Empty).ToLower(); }
/** * Return the passed in value as an unsigned byte array. * * @param value value to be converted. * @return a byte array without a leading zero byte if present in the signed encoding. */ public static byte[] AsUnsignedByteArray( BigInteger value) { byte[] bytes = value.ToByteArray(); if (bytes[0] == 0) { byte[] tmp = new byte[bytes.Length - 1]; Array.Copy(bytes, 1, tmp, 0, tmp.Length); return tmp; } return bytes; }
public static bool Verify(byte[] data, byte[] sigBytes, BigInteger pub) { EcdsaSignature signature = EcdsaSignature.DecodeFromDer(sigBytes); var signer = new ECDsaSigner(); ECPoint pubPoint = Secp256K1.Curve().DecodePoint(pub.ToByteArray()); var parameters = new ECPublicKeyParameters(pubPoint, Secp256K1.Params()); signer.Init(false, parameters); try { return signer.VerifySignature(data, signature.R, signature.S); } catch (Exception) { return false; } }
/// <summary> /// MPI encoded numbers are produced by the OpenSSL BN_bn2mpi function. /// They consist of a 4 byte big endian length field, followed by the stated number of bytes representing the number in big endian format (with a sign bit). /// </summary> /// <param name="value"></param> /// <param name="includeLength">Indicates whether the 4 byte length field should be included</param> /// <returns></returns> public static byte[] Encode(BigInteger value,bool includeLength=true) { if(value.Equals(BigInteger.Zero)) { if(!includeLength) { return new byte[0]; } return new byte[] { 0x00,0x00,0x00,0x00 }; } bool isNegative=value.CompareTo(BigInteger.Zero)<0; if(isNegative) { value=value.Negate(); } byte[] array=value.ToByteArray(); int length=array.Length; if((array[0] & 0x80)==0x80) { length++; } if(includeLength) { byte[] result=new byte[length+4]; Array.Copy(array,0,result,length-array.Length+3,array.Length); ((uint)length).ToByteArrayBe(result); if(isNegative) { result[4]|=0x80; } return result; } else { byte[] result; if(length!=array.Length) { result=new byte[length]; Array.Copy(array,0,result,1,array.Length); } else { result=array; } if(isNegative) { result[0]|=0x80; } return result; } }
public string GetSign(byte[] msg) { //Org.BouncyCastle.Math.BigInteger d = new Org.BouncyCastle.Math.BigInteger(Convert.FromBase64String(privKey)); byte[] prvB = StringToByteArray(prv); byte[] pubB = StringToByteArray(pub); //byte[] prvB = StringToByteArray(prvTmp); //byte[] pubB = StringToByteArray(pubTmp); Org.BouncyCastle.Math.BigInteger sk = new Org.BouncyCastle.Math.BigInteger(+1, prvB); Org.BouncyCastle.Math.EC.ECPoint q = domain.G.Multiply(sk); byte[] pubKeyX = q.Normalize().AffineXCoord.GetEncoded(); byte[] pubKeyY = q.Normalize().AffineYCoord.GetEncoded(); BigInteger k = GenerateRandom(); //BigInteger k = new BigInteger(+1,StringToByteArray("015B931D9C7BF3A7A70E57868BF29712377E74355FC59032CD7547C80E179010")); //Debug.Log("kv:" + BitConverter.ToString(kv.ToByteArray()).Replace("-", "")); Debug.Log("K:" + BitConverter.ToString(k.ToByteArray()).Replace("-", "")); ECPoint Q = domain.G.Multiply(k); Org.BouncyCastle.Crypto.Digests.Sha256Digest digester = new Org.BouncyCastle.Crypto.Digests.Sha256Digest(); byte[] h = new byte[digester.GetDigestSize()]; digester.BlockUpdate(Q.GetEncoded(true), 0, Q.GetEncoded(true).Length); digester.BlockUpdate(pubB, 0, pubB.Length); digester.BlockUpdate(msg, 0, msg.Length); digester.DoFinal(h, 0); Org.BouncyCastle.Math.BigInteger r = new Org.BouncyCastle.Math.BigInteger(+1, h); BigInteger s = r.Multiply(sk); s = k.Subtract(s); s = s.Mod(domain.n); string rt = BitConverter.ToString(r.ToByteArray()).Replace("-", ""); if (rt.Length > 32) { rt = rt.Remove(0, 2); } string st = BitConverter.ToString(s.ToByteArray()).Replace("-", ""); return(rt + st); }
public DerEnumerated( BigInteger value) { bytes = value.ToByteArray(); }
public KeyPair(BigInteger priv, BigInteger pub) { _priv = priv; _pub = pub; _pubBytes = pub.ToByteArray(); }
public void SetKeyByString(string priv, string pub) { PrivateKey = new BigInteger(1,HexString2Bytes(priv)); PublicKeyBytes = HexString2Bytes(pub); AddressBytes = ConvertPubKeyToAddress(PublicKeyBytes); PublicKeyString = bytesToHexString(PublicKeyBytes); PrivateKeyString = bytesToHexString(PrivateKey.ToByteArray()); }
// -- BigInt & UBigInt public void WriteBigInteger(BigInteger value) { var bytes = value.ToByteArray(); Array.Reverse(bytes); WriteByteArray(bytes); }
public static string BigHex(BigInteger bn) { return B16.ToString(bn.ToByteArray()); }
private static byte[] BigIntegerToByteArray(BigInteger input, int length) { byte[] result = new byte[length]; byte[] inputBytes = input.ToByteArray(); Array.Reverse(inputBytes); Buffer.BlockCopy(inputBytes, 0, result, 0, System.Math.Min(inputBytes.Length, result.Length)); Array.Reverse(result); return result; }
public DSAKeyParams(BigInteger p, BigInteger q, BigInteger g, BigInteger x) { if (p == null) throw new ArgumentException("DSAKeyHexStrings:DSA key parameter P cannot be null"); if (q == null) throw new ArgumentException("DSAKeyHexStrings:DSA key parameter Q cannot be null"); if (g == null) throw new ArgumentException("DSAKeyHexStrings:DSA key parameter G cannot be null"); if (x == null) throw new ArgumentException("DSAKeyHexStrings:DSA key parameter X cannot be null"); _p = p; _q = q; _g = g; _x = x; _p_hex = OTR.Utilities.Utility.ByteToHex(p.ToByteArray()); _q_hex = OTR.Utilities.Utility.ByteToHex(q.ToByteArray()); _g_hex = OTR.Utilities.Utility.ByteToHex(g.ToByteArray()); _x_hex = OTR.Utilities.Utility.ByteToHex(x.ToByteArray()); }
private byte[] int2octets(BigInteger v) { byte[] @out = v.ToByteArray(); if(@out.Length < rolen) { byte[] out2 = new byte[rolen]; Array.Copy(@out, 0, out2, rolen - @out.Length, @out.Length); return out2; } else if(@out.Length > rolen) { byte[] out2 = new byte[rolen]; Array.Copy(@out, @out.Length - rolen, out2, 0, rolen); return out2; } else { return @out; } }
/// <summary> /// Adds BigInteger to builder prefixed with size /// </summary> /// <param name="bigInt"></param> public void AddBigIntBlob(BigInteger aBigInt) { byte[] bytes = aBigInt.ToByteArray(); AddBlob(bytes); }
/** Generates an entirely new keypair. */ public CMicroCashKeyPair(bool bGenerate) { if(bGenerate) { ECKeyPairGenerator gen = new ECKeyPairGenerator(); var secureRandom = new SecureRandom(); var ps = Org.BouncyCastle.Asn1.Sec.SecNamedCurves.GetByName("secp256k1"); var ecParams = new ECDomainParameters(ps.Curve, ps.G, ps.N, ps.H); var keyGenParam = new ECKeyGenerationParameters(ecParams, secureRandom); gen.Init(keyGenParam); AsymmetricCipherKeyPair kp = gen.GenerateKeyPair(); ECPrivateKeyParameters priv = (ECPrivateKeyParameters)kp.Private; ECPublicKeyParameters pub = (ECPublicKeyParameters)kp.Public; byte[] hexpriv = priv.D.ToByteArrayUnsigned(); //byte[] hexPub = pub Org.BouncyCastle.Math.BigInteger Db = new Org.BouncyCastle.Math.BigInteger(1,hexpriv); ECPoint dd = ps.G.Multiply(Db); byte[] pubaddr = new byte[65]; byte[] Y = dd.Y.ToBigInteger().ToByteArray(); Array.Copy(Y, 0, pubaddr, 64 - Y.Length + 1, Y.Length); byte[] X = dd.X.ToBigInteger().ToByteArray(); Array.Copy(X, 0, pubaddr, 32 - X.Length + 1, X.Length); pubaddr[0] = 4; m_Priv = new BigInteger(hexpriv); m_Pub = pubaddr; m_Address = ConvertPubKeyToAddress(m_Pub); m_PubKeyString = bytesToHexString(m_Pub); m_PrivKeyString = bytesToHexString(m_Priv.ToByteArray()); } }
public void SetKeyByString(string priv, string pub) { m_Priv = new BigInteger(1,HexString2Bytes(priv)); m_Pub = HexString2Bytes(pub); m_Address = ConvertPubKeyToAddress(m_Pub); m_PubKeyString = bytesToHexString(m_Pub); m_PrivKeyString = bytesToHexString(m_Priv.ToByteArray()); }
private static void ExtractBytes( byte[] encKey, int offset, BigInteger bI) { byte[] val = bI.ToByteArray(); int n = (bI.BitLength + 7) / 8; for (int i = 0; i < n; ++i) { encKey[offset + i] = val[val.Length - 1 - i]; } }
public void CheckEquality(BigInteger m, BigInteger r, BigInteger y) { //r = new BigInteger("2"); //n = new BigInteger("3"); //Console.WriteLine("\n\nr: " + r); //var one = new BigInteger("1"); //Console.WriteLine("\n\none: " + one); //Console.WriteLine("\n\nr^{-1}: " + one.Divide(r)); //Console.WriteLine("\n\nr * r^{-1}: " + r.Multiply(one.Divide(r))); //Console.WriteLine("\n\n2 * 2 % 3: " + r.Multiply(r).Mod(n)); //Console.WriteLine("\n\n2 ^ 3 % 3: " + r.ModPow(n, n)); //Console.WriteLine("\n\nr / r: " + r.Divide(r)); //Console.WriteLine("\n\nr / r: " + r.Divide(r)); byte[] blind = BlindObject(m.ToByteArray(), r).ToByteArray(); byte[] unblind = UnblindObject(BlindObject(m.ToByteArray(), r), r); byte[] m_byte = m.ToByteArray(); for(int i =0; i< m_byte.Length; i++) { if (m_byte[i] == unblind[i]) { Console.WriteLine("jupiii"); } else { Console.WriteLine("not equal"); } } }