예제 #1
0
        public static string Decrypt(string Source, string Key)
        {
            if (String.IsNullOrEmpty(Source))
            {
                throw new Exception("没有输入密文!");
            }
            else if (Source.Length % 16 != 0)
            {
                throw new Exception("密文长度有误!(密文长度须为16的倍数)");
            }

            if (Encoding.Default.GetBytes(Key).Length != 8)
            {
                throw new Exception("密钥个数必须为8个字符或4个汉字!");
            }

            try
            {
                //用于检测,"密文内容有误!(密文内容应为16进制)"
                int temp;
                for (int i = 0; i < Source.Length; i++)
                {
                    temp = Convert.ToInt32(Source[i].ToString(), 16);
                }
            }
            catch
            {
                throw new Exception("密文内容有误!(密文内容应为16进制)");
            }

            IdeaKey    key  = new IdeaKey(Key, false);
            IdeaCipher idea = new IdeaCipher(Source, false, key);

            return(idea.GetOutStr());
        }
예제 #2
0
        public static string Encrypt(string Source, string Key)
        {
            if (String.IsNullOrEmpty(Source))
            {
                throw new Exception("没有输入明文!");
            }

            if (Encoding.UTF8.GetBytes(Key).Length != 8)
            {
                throw new Exception("密钥个数必须为8个字符或4个汉字!");
            }

            IdeaKey    key  = new IdeaKey(Key, true);
            IdeaCipher idea = new IdeaCipher(Source, true, key);

            return(idea.GetOutStr());
        }
예제 #3
0
        public IdeaCipher(string str, bool flag, IdeaKey key)
        {
            this.flag = flag;
            if (flag == true)
            {
                int blank = 8 - (Encoding.Unicode.GetBytes(str).Length) % 8;
                if (blank != 8)
                {
                    for (int i = 0; i < blank; i++)
                    {
                        str += " ";
                    }
                }
                byte[]        strByte = Encoding.Unicode.GetBytes(str);
                StringBuilder strbdr  = new StringBuilder();
                for (int i = 0; i < strByte.Length; i++)
                {
                    for (int j = 7; j >= 0; j--)
                    {
                        strbdr.Append(((strByte[i] >> j) & 1));
                    }
                }
                str = strbdr.ToString();
            }
            else
            {
                StringBuilder instrbd = new StringBuilder();
                int           inint   = 0;
                string        instr   = null;
                for (int i = 0; i < str.Length; i++)
                {
                    inint = Convert.ToInt32(str.Substring(i, 1), 16);
                    instr = Convert.ToString(inint, 2);
                    while (instr.Length < 4)
                    {
                        instr = 0 + instr;
                    }
                    instrbd.Append(instr);
                }
                str = instrbd.ToString();
            }

            string m1, m2, m3, m4;

            string[] c = new string[15];

            for (int n = 0; n < str.Length / 64; n++)
            {
                m1 = str.Substring(n * 64, 16);
                m2 = str.Substring(n * 64 + 16, 16);
                m3 = str.Substring(n * 64 + 32, 16);
                m4 = str.Substring(n * 64 + 48, 16);
                int pos = 1;

                for (int i = 0; i < 8; i++)//8轮的加密变换
                {
                    c[1]  = Mul(m1, key.getKey(pos++));
                    c[2]  = Add(m2, key.getKey(pos++));
                    c[3]  = Add(m3, key.getKey(pos++));
                    c[4]  = Mul(m4, key.getKey(pos++));
                    c[5]  = Xor(c[1], c[3]);
                    c[6]  = Xor(c[2], c[4]);
                    c[7]  = Mul(c[5], key.getKey(pos++));
                    c[8]  = Add(c[6], c[7]);
                    c[9]  = Mul(c[8], key.getKey(pos++));
                    c[10] = Add(c[7], c[9]);
                    c[11] = Xor(c[1], c[9]);
                    c[12] = Xor(c[3], c[9]);
                    c[13] = Xor(c[2], c[10]);
                    c[14] = Xor(c[4], c[10]);
                    m1    = c[11];
                    m2    = c[13];
                    m3    = c[12];
                    m4    = c[14];
                }

                c[11] = Mul(m1, key.getKey(pos++));//开始第9轮的变换
                c[12] = Add(m2, key.getKey(pos++));
                c[13] = Add(m3, key.getKey(pos++));
                c[14] = Mul(m4, key.getKey(pos++));

                outStr = outStr + c[11] + c[12] + c[13] + c[14];
            }
        }