public int Do_BuffaloEnc(string[] args, Program.Properties props) { int ret = 0; Properties subprops = new Properties { crypt_key = DEFAULT_KEY, magic = DEFAULT_MAGIC, seed = 0x4F // Char: O }; ToolsArgMap argMap = new ToolsArgMap(); argMap.Init_args_BuffaloEnc(args, ref subprops); if (props.help) { PrintHelp(); return(0); } if (CheckParams(subprops) != 0) { return(1); } FileStream inFs; FileStream outFs; FileMode outFMode = File.Exists(props.outFile) ? FileMode.Truncate : FileMode.Create; try { inFs = new FileStream(props.inFile, FileMode.Open, FileAccess.Read, FileShare.Read); outFs = new FileStream(props.outFile, outFMode, FileAccess.Write, FileShare.None); } catch (IOException e) { Console.Error.WriteLine(e.Message); return(1); } if (inFs.Length >= 0x80000000L) { Console.Error.WriteLine(Lang.Tools.BuffaloEncRes.Error_BigFile); return(1); } ret = subprops.isde ? Decrypt(ref inFs, ref outFs, subprops, props.debug) : Encrypt(ref inFs, ref outFs, subprops, props.debug); inFs.Close(); outFs.Close(); return(ret); }
/// <summary> /// コマンドライン引数 (<paramref name="args"/>) を解析し、メインプロパティを取得します /// </summary> /// <param name="args">コマンドライン引数</param> /// <param name="props">取得したオプションを格納するターゲット</param> public void Init_args(string[] args, int arg_idx, ref Program.Properties props) { for (int i = arg_idx; i < args.Length; i++) { if (args[i].StartsWith("-")) { switch (args[i].Replace("-", "")) { case "i": if (Set_StrParamFromArgs(args, i, ref props.inFile) == 0) { i++; } break; case "o": if (Set_StrParamFromArgs(args, i, ref props.outFile) == 0) { i++; } break; case "h": case "help": props.help = true; break; case "D": props.debug = true; break; case "Q": props.quiet = true; break; case "": // ハイフンのみ ('-') 対策 props.param_invalid = true; break; } props.paramcnt++; } } }
public int Do_Aes(string[] args, int arg_idx, Program.Properties props) { byte[] iv; byte[] key; int keylen; long offset = 0; long len = 0; Properties subprops = new Properties() { keylen = 256, }; CryptoStream Cs; ToolsArgMap argMap = new ToolsArgMap(); argMap.Init_args_Aes(args, arg_idx, ref subprops); if (props.help) { PrintHelp(arg_idx); return(0); } keylen = subprops.keylen; iv = new byte[16]; key = new byte[keylen / 8]; // keylenはbit値、128または256 /* * check/build iv and key */ /* iv */ if (subprops.iv == null || subprops.iv.Length == 0) // if iv is not specified or blank { /* use default array of iv (filled by '0') */ Console.Error.WriteLine( Lang.Resource.Main_Warning_Prefix + Lang.Tools.AesRes.Warning_NoIV); } else // if iv is specified { if (!subprops.hex_iv && subprops.iv.Length > iv.Length) { Console.Error.WriteLine( Lang.Resource.Main_Error_Prefix + Lang.Tools.AesRes.Error_LongIVLen); return(1); } else if (subprops.hex_iv) { if (subprops.iv.Length % 2 != 0) { Console.Error.WriteLine( Lang.Resource.Main_Error_Prefix + Lang.Tools.AesRes.Error_InvalidIVLenHex); return(1); } if (subprops.iv.Length > iv.Length * 2) { Console.Error.WriteLine( Lang.Resource.Main_Error_Prefix + Lang.Tools.AesRes.Error_LongIVLenHex); } } if (subprops.hex_iv) { for (int i = 0; i < (subprops.iv.Length / 2); i++) { iv[i] = Convert.ToByte(subprops.iv.Substring(i * 2, 2), 16); } } else { byte[] tmp_iv = Encoding.ASCII.GetBytes(subprops.iv); Array.Copy(tmp_iv, iv, tmp_iv.Length); } } /* iv end */ /* key */ if (subprops.key == null || subprops.key.Length == 0) { Console.Error.WriteLine( Lang.Resource.Main_Error_Prefix + Lang.Tools.AesRes.Error_NoKey); return(1); } if (!subprops.hex_key && subprops.key.Length > key.Length) { Console.Error.WriteLine( Lang.Resource.Main_Error_Prefix + Lang.Tools.AesRes.Error_LongKeyLen, key.Length); return(1); } else if (subprops.hex_key) { if (subprops.key.Length % 2 != 0) { Console.Error.WriteLine( Lang.Resource.Main_Error_Prefix + Lang.Tools.AesRes.Error_InvalidKeyLenHex); return(1); } if (subprops.key.Length > key.Length * 2) { Console.Error.WriteLine( Lang.Resource.Main_Error_Prefix + Lang.Tools.AesRes.Error_LongKeyLenHex, key.Length, key.Length * 2); return(1); } } if (subprops.hex_key) { for (int i = 0; i < (subprops.key.Length / 2); i++) { key[i] = Convert.ToByte(subprops.key.Substring(i * 2, 2), 16); } } else { byte[] tmp_key = Encoding.ASCII.GetBytes(subprops.key); Array.Copy(tmp_key, key, tmp_key.Length); //if (tmp_key.Length < keylen / 8) // Console.Error.WriteLine("specified key is too short, padded by '0'"); } /* key end */ /* * check/build iv and key end */ FileStream inFs; FileStream outFs; FileMode outMode = File.Exists(props.outFile) ? FileMode.Truncate : FileMode.Create; try { inFs = new FileStream(props.inFile, FileMode.Open, FileAccess.Read, FileShare.Write); outFs = new FileStream(props.outFile, outMode, FileAccess.Write, FileShare.None); } catch (IOException e) { Console.Error.WriteLine(e.Message); return(1); } /* check offset/length */ if (subprops.offset > inFs.Length) { Console.Error.WriteLine( Lang.Resource.Main_Warning_Prefix + Lang.Tools.AesRes.Warning_LargeOffset); } else { offset = subprops.offset; } if (subprops.len != null && // something is specified for len (!Program.StrToLong(subprops.len, out len, NumberStyles.None) || // fail to convert (invalid chars for num) len <= 0 || // equal or smaller than 0 len > inFs.Length - offset)) // larger than valid length { Console.Error.WriteLine( Lang.Resource.Main_Warning_Prefix + Lang.Tools.AesRes.Warning_InvalidLength); subprops.len = null; } if (subprops.len != null ? len % 16 != 0 : // if "length" specified (inFs.Length - offset) % 16 != 0) // no length specified { if (subprops.decrypt) { Console.Error.WriteLine( Lang.Resource.Main_Error_Prefix + Lang.Tools.AesRes.Error_InvalidDecLen); return(1); } else { Console.Error.WriteLine( Lang.Resource.Main_Warning_Prefix + Lang.Tools.AesRes.Warning_ShortEncLen); } } /* check offset/length end */ if (!props.quiet) { PrintInfo(subprops, key, iv, subprops.len != null ? len : inFs.Length - offset); } AesManaged aes = new AesManaged { KeySize = keylen, IV = iv, Key = key, Mode = CipherMode.CBC, Padding = PaddingMode.Zeros }; ICryptoTransform endec = subprops.decrypt ? aes.CreateDecryptor(aes.Key, aes.IV) : aes.CreateEncryptor(aes.Key, aes.IV); Cs = new CryptoStream(outFs, endec, CryptoStreamMode.Write); inFs.Seek(offset, SeekOrigin.Begin); byte[] buf = new byte[0x10000]; int readlen; while ((readlen = inFs.Read(buf, 0, buf.Length)) > 0) { if (subprops.len == null) { Cs.Write(buf, 0, readlen); } else if (len > readlen) { len -= readlen; Cs.Write(buf, 0, readlen); } else { Cs.Write(buf, 0, (int)len); break; } } Cs.Close(); inFs.Close(); outFs.Close(); return(0); }
public int Do_BinCut(string[] args, int arg_idx, Program.Properties props) { Properties subprops = new Properties(); if (props.help) { PrintHelp(arg_idx); return(0); } ToolsArgMap argMap = new ToolsArgMap(); argMap.Init_args_BinCut(args, arg_idx, ref subprops); FileStream inFs; FileStream outFs; FileMode outFMode = File.Exists(props.outFile) ? FileMode.Truncate : FileMode.Create; try { inFs = new FileStream(props.inFile, FileMode.Open, FileAccess.Read, FileShare.Read); outFs = new FileStream(props.outFile, outFMode, FileAccess.Write, FileShare.None); } catch (IOException e) { Console.Error.WriteLine(e.Message); return(1); } /* check offset/length */ if (subprops.offset > inFs.Length) { Console.Error.WriteLine(Lang.Resource.Main_Warning_Prefix + Lang.Tools.BinCutRes.Warning_LargeOffset); subprops.offset = 0; } if (subprops.len < 0 || subprops.len > inFs.Length - subprops.offset) { Console.Error.WriteLine(Lang.Resource.Main_Warning_Prefix + Lang.Tools.BinCutRes.Warning_InvalidLength); subprops.len = 0; } if (subprops.pad > 0 && subprops.padBS > 0) { Console.Error.WriteLine(Lang.Resource.Main_Error_Prefix + Lang.Tools.BinCutRes.Error_DualPad); return(1); } if (subprops.pad != 0 && subprops.pad < (subprops.len != 0 ? subprops.len : inFs.Length - subprops.offset)) { Console.Error.WriteLine(Lang.Resource.Main_Error_Prefix + Lang.Tools.BinCutRes.Error_SmallPadSize); return(1); } if (subprops.padBS < 0) { Console.Error.WriteLine(Lang.Resource.Main_Error_Prefix + Lang.Tools.BinCutRes.Error_InvalidPadBSSize); return(1); } /* check offset/length/pad/pad_with_bs end */ long data_len = subprops.len != 0 ? subprops.len : (inFs.Length - subprops.offset); if (!props.quiet) { PrintInfo(subprops, data_len); } inFs.Seek(subprops.offset, SeekOrigin.Begin); inFs.CopyTo(outFs); if (subprops.len != 0) { outFs.SetLength(subprops.len); } /* padding無しであればここで終了 */ if (subprops.pad == 0 && subprops.padBS == 0) { return(0); } long padded_len; /* * blocksizeでのpaddingの際は余りを確認して、あるようならblocksizeを足す * 指定サイズへのpaddingの際はそのサイズをそのまま指定 */ if (subprops.padBS > 0) { padded_len = subprops.padBS * ((data_len / subprops.padBS) + (data_len % subprops.padBS > 0 ? 1 : 0)); } else { padded_len = subprops.pad; } long len; byte[] buf = new byte[0x10000]; for (len = padded_len - data_len; len >= buf.Length; len -= buf.Length) { outFs.Write(buf, 0, buf.Length); } if (len > 0) { outFs.Write(buf, 0, (int)len); } return(0); }
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); }
private int Encrypt(ref FileStream inFs, ref FileStream outFs, Properties subprops, Program.Properties props) { 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; if (!props.quiet) { PrintInfo(subprops, props.debug); } 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); }