private int Decrypt(ref FileStream inFs, ref FileStream outFs, Properties subprops, Program.Properties props)
        {
            long src_len;

            byte[] buf;
            Buffalo_Lib.Enc_Param ep = new Buffalo_Lib.Enc_Param();

            src_len = inFs.Length;

            if (!inFs.CanSeek || src_len < subprops.offset)
            {
                Console.Error.WriteLine(
                    Lang.Resource.Main_Error_Prefix + Lang.Tools.BuffaloEncRes.Error_LargeOffset,
                    subprops.offset);
                return(1);
            }

            inFs.Seek(subprops.offset, SeekOrigin.Begin);
            buf = new byte[src_len - subprops.offset];

            inFs.Read(buf, 0, Convert.ToInt32(src_len) - subprops.offset);

            ep.magic     = new byte[Buffalo_Lib.ENC_MAGIC_LEN];
            ep.product   = new byte[Buffalo_Lib.ENC_PRODUCT_LEN];
            ep.version   = new byte[Buffalo_Lib.ENC_VERSION_LEN];
            ep.key       = Encoding.ASCII.GetBytes(subprops.crypt_key);
            ep.longstate = subprops.islong;

            Buffalo_Lib bufLib = new Buffalo_Lib();

            if (bufLib.Decrypt_Buf(ref ep, ref buf, buf.Length, subprops.force) != 0)
            {
                Console.Error.WriteLine(
                    Lang.Resource.Main_Error_Prefix + Lang.Tools.BuffaloEncRes.Error_FailDecrypt);
                return(1);
            }

            subprops.magic   = Encoding.ASCII.GetString(ep.magic).TrimEnd('\0');
            subprops.seed    = ep.seed;
            subprops.product = Encoding.ASCII.GetString(ep.product).TrimEnd('\0');
            subprops.version = Encoding.ASCII.GetString(ep.version).TrimEnd('\0');
            subprops.size    = Convert.ToInt32(ep.datalen);
            cksum            = ep.cksum;

            if (!props.quiet)
            {
                PrintInfo(subprops, props.debug);
            }

            outFs.Write(buf, 0, Convert.ToInt32(ep.datalen));

            return(0);
        }
Esempio n. 2
0
        private int Encrypt(ref FileStream inFs, ref FileStream outFs, Properties subprops, bool isdbg)
        {
            int  src_len, tail_dst = 0, tail_len = 0, tail_src;
            long totlen = 0;

            byte[] buf;
            int    hdrlen;

            Buffalo_Lib.Enc_Param ep = new Buffalo_Lib.Enc_Param()
            {
                key       = new byte[subprops.crypt_key.Length + 1],
                magic     = new byte[subprops.magic.Length + 1],
                seed      = subprops.seed,
                product   = new byte[subprops.product.Length + 1],
                version   = new byte[subprops.version.Length + 1],
                longstate = subprops.islong,
            };

            /* key, magic, product, versionは末尾 '\0' で終端 */
            Array.Copy(Encoding.ASCII.GetBytes(subprops.crypt_key), 0,
                       ep.key, 0, subprops.crypt_key.Length);
            Array.Copy(Encoding.ASCII.GetBytes(subprops.magic), 0,
                       ep.magic, 0, subprops.magic.Length);
            Array.Copy(Encoding.ASCII.GetBytes(subprops.product), 0,
                       ep.product, 0, subprops.product.Length);
            Array.Copy(Encoding.ASCII.GetBytes(subprops.version), 0,
                       ep.version, 0, subprops.version.Length);

            src_len = (int)inFs.Length;

            Buffalo_Lib bufLib = new Buffalo_Lib();

            if (subprops.size > 0)
            {
                tail_dst = bufLib.Enc_Compute_BufLen(in ep.product, in ep.version, subprops.size);
                tail_len = src_len - subprops.size;
                totlen   = tail_dst + tail_len;
            }
            else
            {
                totlen = bufLib.Enc_Compute_BufLen(in ep.product, in ep.version, src_len);
            }

            buf = new byte[totlen];

            hdrlen = bufLib.Enc_Compute_HeaderLen(in ep.product, in ep.version);

            inFs.Read(buf, (int)hdrlen, (int)src_len);

            if (subprops.size > 0)
            {
                tail_src = hdrlen + subprops.size;
                Array.Copy(buf, tail_src, buf, tail_dst, tail_len);
                Array.Clear(buf, (int)tail_src, (int)(tail_dst - tail_src));
                src_len = subprops.size;
            }

            cksum = ep.cksum =
                bufLib.Buffalo_Csum((uint)src_len, in buf, hdrlen, src_len);
            ep.datalen = src_len;

            subprops.size = (int)ep.datalen;

            PrintInfo(subprops, isdbg);
            if (bufLib.Encrypt_Buf(in ep, ref buf, hdrlen) != 0)
            {
                Console.Error.WriteLine(
                    Lang.Resource.Main_Error_Prefix + Lang.Tools.BuffaloEncRes.Error_FailEncrypt);
                return(1);
            }

            outFs.Write(buf, 0, (int)totlen);

            return(0);
        }