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); }
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); }