Beispiel #1
0
		//    myKeyAgree=KeyAgreement.getInstance("DiffieHellman");
		/// <exception cref="System.Exception"></exception>
		public virtual byte[] GetE()
		{
			if (e == null)
			{
				DHParameterSpec dhSkipParamSpec = new DHParameterSpec(p, g);
				myKpairGen.Initialize(dhSkipParamSpec);
				Sharpen.KeyPair myKpair = myKpairGen.GenerateKeyPair();
				myKeyAgree.Init(myKpair.GetPrivate());
				//    BigInteger x=((javax.crypto.interfaces.DHPrivateKey)(myKpair.getPrivate())).getX();
				e = ((DHPublicKey)(myKpair.GetPublic())).GetY();
				e_array = e.GetBytes();
			}
			return e_array;
		}
Beispiel #2
0
        public static byte[] GetCounterHexValue(BigInteger counter)
        {
            byte[] counterBytes = counter.GetBytes();
            counterBytes = PppConvert.SwapBits(counterBytes);
            int padNeeded = 16 - counterBytes.Length;
            List<byte> padedBytes;

            if (counterBytes.Length != 16)
            {
                padedBytes = new List<byte>(16);
                padedBytes.AddRange(counterBytes);
                padedBytes.AddRange(new byte[padNeeded]);
            }
            else
            {
                padedBytes = new List<byte>(counterBytes);
            }
            return padedBytes.ToArray();
        }
Beispiel #3
0
		/// <exception cref="System.Exception"></exception>
		public virtual byte[] GetK()
		{
			if (K == null)
			{
				KeyFactory myKeyFac = KeyFactory.GetInstance("DH");
				DHPublicKeySpec keySpec = new DHPublicKeySpec(f, p, g);
				PublicKey yourPubKey = myKeyFac.GeneratePublic(keySpec);
				myKeyAgree.DoPhase(yourPubKey, true);
				byte[] mySharedSecret = myKeyAgree.GenerateSecret();
				K = new BigInteger(mySharedSecret);
				K_array = K.GetBytes();
				//System.err.println("K.signum(): "+K.signum()+
				//		   " "+Integer.toHexString(mySharedSecret[0]&0xff)+
				//		   " "+Integer.toHexString(K_array[0]&0xff));
				K_array = mySharedSecret;
			}
			return K_array;
		}
Beispiel #4
0
 public static byte[] BigIntToMpi(BigInteger bint)
 {
     var ibytes = bint.GetBytes();//.Reverse().ToArray();
     var shift = 0;// ibytes[0] == 0 ? 1 : 0;
     var length = ibytes.Length - shift;
     var bytes = new byte[length + 2];
     ushort ulength = Convert.ToUInt16(length * 8);
     bytes[0] = (byte)(ulength >> 8);
     bytes[1] = (byte)(ulength & 0xff);
     Array.Copy(ibytes, shift, bytes, 2, length);
     return bytes;
 }
Beispiel #5
0
		private byte[] GetPaddedValue (BigInteger value, int length)
		{
			byte[] result = value.GetBytes ();
			if (result.Length >= length)
				return result;

			// left-pad 0x00 value on the result (same integer, correct length)
			byte[] padded = new byte[length];
			Buffer.BlockCopy (result, 0, padded, (length - result.Length), result.Length);
			// temporary result may contain decrypted (plaintext) data, clear it
			Array.Clear (result, 0, result.Length);
			return padded;
		}
Beispiel #6
0
            private static System.Numerics.BigInteger EncodeBigInteger(BigInteger v)
            {
                var bytes = v.GetBytes();

                Array.Reverse(bytes);

                if ((bytes[bytes.Length - 1] & 128) != 0)
                    Array.Resize(ref bytes, bytes.Length + 1);

                return new System.Numerics.BigInteger(bytes);
            }
        //for that it s strange because it s a quite different of DecryptValue and encryptvalue
        //but maybe it s the same thanks to math ;)
        //so need to do some math to check...
        public byte[] RSAExptmod(byte[] PacketIn, eRSAKeyFormat format)
        {
            BigInteger tmp, tmpa, tmpb;

            //todo more check key generate format packet in and out null...

            tmp = new BigInteger(PacketIn);

            /* are we using the private exponent */
            if (format == eRSAKeyFormat.PK_PRIVATE )
            {
                // m1 = c^dp mod p
                tmpa = tmp.ModPow (dp, p);
                tmpb = tmp.ModPow (dq, q);
                tmp = (tmpa * qP + tmpb*pQ).ModPow (0, n);
            }
            else
            {
                tmp = tmp.ModPow (e, n);
            }

            /* convert it */
            return tmp.GetBytes();
        }