Monday, August 15, 2011

C# simples byte hash encrypt reflection formats

Byte or Int to HexString

Console.WriteLine(String.Format("{0:X}", 69));
            Console.WriteLine(((byte)69).ToString("x2").ToUpper());
            Console.ReadLine();

CreateInstance By reflection

ObjectHandle obj = AppDomain.CurrentDomain.CreateInstance("ClassLibrary1", "ClassLibrary1.Class1");
ClassLibrary1.Class1 c = (ClassLibrary1.Class1)obj.Unwrap();
c.name = "asdfasdf";

MD5 hash simple

public static String md5hash(String input)
        {
            MD5CryptoServiceProvider x = new MD5CryptoServiceProvider();
            byte[] bs = Encoding.ASCII.GetBytes(input);
            bs = x.ComputeHash(bs);
            StringBuilder s = new StringBuilder();
            foreach (byte b in bs)
            {
                s.Append(b.ToString("x2").ToUpper());
            }
            return s.ToString();
        }

Encrypt simple

public static byte[] encrypt(byte[] content, byte[] key)
        {
            byte[] ret = new byte[content.Length];
            for (int i = 0, k = 0; i < ret.Length; i++, k++)
            {
                if (key.Length <= k) k = 0;
                ret[i] = (byte)(((int)content[i]) ^ ((int)key[k]));
            }
            return ret;
        }


No comments: