public virtual byte[] Encode(BigInteger n, BigInteger r, BigInteger s) { int valueLength = BigIntegers.GetUnsignedByteLength(n); byte[] result = new byte[valueLength * 2]; EncodeValue(n, r, result, 0, valueLength); EncodeValue(n, s, result, valueLength, valueLength); return(result); }
public void Init(BigInteger n, BigInteger d, byte[] message) { this.n = n; Arrays.Fill(V, (byte)0x01); Arrays.Fill(K, (byte)0); int size = BigIntegers.GetUnsignedByteLength(n); byte[] x = new byte[size]; byte[] dVal = BigIntegers.AsUnsignedByteArray(d); Array.Copy(dVal, 0, x, x.Length - dVal.Length, dVal.Length); byte[] m = new byte[size]; BigInteger mInt = BitsToInt(message); if (mInt.CompareTo(n) >= 0) { mInt = mInt.Subtract(n); } byte[] mVal = BigIntegers.AsUnsignedByteArray(mInt); Array.Copy(mVal, 0, m, m.Length - mVal.Length, mVal.Length); hMac.Init(new KeyParameter(K)); hMac.BlockUpdate(V, 0, V.Length); hMac.Update((byte)0x00); hMac.BlockUpdate(x, 0, x.Length); hMac.BlockUpdate(m, 0, m.Length); hMac.DoFinal(K, 0); hMac.Init(new KeyParameter(K)); hMac.BlockUpdate(V, 0, V.Length); hMac.DoFinal(V, 0); hMac.BlockUpdate(V, 0, V.Length); hMac.Update((byte)0x01); hMac.BlockUpdate(x, 0, x.Length); hMac.BlockUpdate(m, 0, m.Length); hMac.DoFinal(K, 0); hMac.Init(new KeyParameter(K)); hMac.BlockUpdate(V, 0, V.Length); hMac.DoFinal(V, 0); }
/** * generate a signature for the loaded message using the key we were * initialised with. */ public virtual byte[] GenerateSignature() { CreateSignatureBlock(); BigInteger t = new BigInteger(1, cipher.ProcessBlock(block, 0, block.Length)); ClearBlock(block); t = t.Min(kParam.Modulus.Subtract(t)); int size = BigIntegers.GetUnsignedByteLength(kParam.Modulus); return(BigIntegers.AsUnsignedByteArray(size, t)); }
public virtual BigInteger[] Decode(BigInteger n, byte[] encoding) { int valueLength = BigIntegers.GetUnsignedByteLength(n); if (encoding.Length != valueLength * 2) { throw new ArgumentException("Encoding has incorrect length", "encoding"); } return(new BigInteger[] { DecodeValue(n, encoding, 0, valueLength), DecodeValue(n, encoding, valueLength, valueLength), }); }
public virtual BigInteger NextK() { byte[] t = new byte[BigIntegers.GetUnsignedByteLength(n)]; for (;;) { int tOff = 0; while (tOff < t.Length) { hMac.BlockUpdate(V, 0, V.Length); hMac.DoFinal(V, 0); int len = System.Math.Min(t.Length - tOff, V.Length); Array.Copy(V, 0, t, tOff, len); tOff += len; } BigInteger k = BitsToInt(t); if (k.SignValue > 0 && k.CompareTo(n) < 0) { return(k); } hMac.BlockUpdate(V, 0, V.Length); hMac.Update((byte)0x00); hMac.DoFinal(K, 0); hMac.Init(new KeyParameter(K)); hMac.BlockUpdate(V, 0, V.Length); hMac.DoFinal(V, 0); } }