示例#1
0
        /// <summary>
        /// Parses the content and returns an object of proper type
        /// </summary>
        /// <param name="contentBytes"></param>
        /// <param name="type"></param>
        /// <param name="code"></param>
        /// <param name="authenticator"></param>
        /// <param name="sharedSecret"></param>
        /// <returns></returns>
        object ParseContentBytes(byte[] contentBytes, string type, uint code, byte[] authenticator, byte[] sharedSecret)
        {
            switch (type)
            {
            case "string":
                return(Encoding.UTF8.GetString(contentBytes));

            case "tagged-string":
                return(Encoding.UTF8.GetString(contentBytes));

            case "octet":
                // If this is a password attribute it must be decrypted
                if (code == 2)
                {
                    return(RadiusPassword.Decrypt(sharedSecret, authenticator, contentBytes));
                }
                return(contentBytes);

            case "integer":
                return(BitConverter.ToUInt32(contentBytes.Reverse().ToArray(), 0));

            case "tagged-integer":
                return(BitConverter.ToUInt32(contentBytes.Reverse().ToArray(), 0));

            case "ipaddr":
                return(new IPAddress(contentBytes));

            default:
                return(null);
            }
        }
        public void TestPasswordEncryptDecrypt(String password)
        {
            var secret        = "xyzzy5461";
            var authenticator = "1234567890123456";

            var encrypted = RadiusPassword.Encrypt(Encoding.UTF8.GetBytes(secret), Encoding.UTF8.GetBytes(authenticator), Encoding.UTF8.GetBytes(password));

            var decrypted = RadiusPassword.Decrypt(Encoding.UTF8.GetBytes(secret), Encoding.UTF8.GetBytes(authenticator), encrypted);


            Assert.AreEqual(password, decrypted);
        }