Exemplo n.º 1
0
        public static string CreateOTP(YubikeySettings key, Form1 form)
        {
            string tokenID = ModHex.Encode(key.TokenID);

            // Assemble key unencrypted data
            byte[] keyBytes = new byte[16];
            for (int i = 0; i < key.PrivateID.Length; ++i)
            {
                keyBytes[i] = key.PrivateID[i];
            }
            keyBytes[6]         = (byte)(key.SessionCounter & 0xff);
            keyBytes[7]         = (byte)((key.SessionCounter >> 8) & 0xff);
            form.SessionCounter = key.SessionCounter.ToString();
            TimeSpan diff  = DateTime.Now - key.StartTime;
            int      timer = (int)((((uint)(diff.TotalSeconds / TS_SEC) & 0x00FFFFFF) + key.TimeStamp) & 0x00FFFFFF);

            form.Timestamp  = timer.ToString();
            keyBytes[8]     = (byte)(timer & 0xff);
            keyBytes[9]     = (byte)((timer >> 8) & 0xff);
            keyBytes[10]    = (byte)((timer >> 16) & 0xff);
            keyBytes[11]    = key.UseCounter++;
            form.UseCounter = keyBytes[11].ToString();
            byte[] buffer = new byte[2];
            RNGCryptoServiceProvider.Create().GetBytes(buffer);
            form.Random  = (((int)buffer[1] << 8) + (int)buffer[0]).ToString();
            keyBytes[12] = buffer[0];
            keyBytes[13] = buffer[1];
            CRC(keyBytes);

            using (Rijndael aes = Rijndael.Create())
            {
                aes.Padding = PaddingMode.None;
                aes.Mode    = CipherMode.ECB;

                using (ICryptoTransform xform = aes.CreateEncryptor(key.Secret, new byte[16]))
                {
                    byte[] plainBytes = new byte[16];
                    xform.TransformBlock(keyBytes, 0, keyBytes.Length, plainBytes, 0);

                    string otp = tokenID + ModHex.Encode(plainBytes);
                    return(otp);
                }
            }
        }
Exemplo n.º 2
0
 public static byte[] ModHexString(string input)
 {
     return(ModHex.Decode(input));
 }
 public static string ModHexString(byte[] input)
 {
     return(ModHex.Encode(input));
 }