コード例 #1
0
ファイル: OpenSSLPbeCipher.cs プロジェクト: kamranzafar/kpbe
 public OpenSSLPbeCipher(Pbe pbe)
     : base(pbe)
 {
 }
コード例 #2
0
ファイル: Pkcs12PbeCipher.cs プロジェクト: kamranzafar/kpbe
 public Pkcs12PbeCipher(Pbe pbe)
     : base(pbe)
 {
 }
コード例 #3
0
ファイル: Program.cs プロジェクト: kamranzafar/kpbe
        public static void Main(string[] args)
        {
            string password=null;
            bool encrypt=false;
            string salt=DEFAULT_SALT;
            string algorithm=null;
            string mode=null;
            string padding=null;
            string output=null;
            string type=null;
            string digest=null;
            int keySize=DEFAULT_KEY_SIZE;
            int iterations=DEFAULT_ITERATIONS;

            bool showHelp=false;

            OptionSet p = new OptionSet () {
                {"a|algo=","Encryption algorithm (AES, RC4, RC2, DES, BLOWFISH, TWOFISH)",v=>algorithm=v},
                {"m|mode=","Block cipher mode (NONE, CBC, CTR, CFB, OFB)",v=>mode=v},
                {"b|padding=","Block padding (NONE, PKCS7, ISO10126d2, ISO7816d4, X932, ZEROBYTE)",v=>padding=v},
                {"p|password="******"Encryption password",v=>password=v},
                {"k|keysize=","Key size",(int v)=>keySize=v},
                {"d|digest=","Digest algorithm (SHA1, SHA224, SHA256, SHA384, SHA512, MD2, MD4, MD5)", v=>digest=v},
                {"s|salt=","Salt phrase",v=>salt=v},
                {"i|iterations=","Number of iterations",(int v)=>iterations=v},
                {"e|encrypt","Encrypt",v=>encrypt= v!= null},
                {"t|type=","Type (PKCS12, OPENSSL)",v=>type=v},
                {"o|output=","Output directory",v=>output=v},
                {"h|help","Help", v => showHelp = v != null}
            };

            List<string> files;
            try {
                files = p.Parse (args);

                if (showHelp) {
                    ShowHelp (p);
                    return;
                }

                if(files.Count==0 || output==null || password==null || algorithm==null){
                    throw new OptionException();
                }
            }
            catch (OptionException e) {
                Console.WriteLine ("kpbe: Missing options");
                Console.WriteLine ("Try `kpbe --help' for more information.");
                return;
            }

            BasePbeCipher pbeCipher=null;
            try{
                Pbe pbe=new Pbe(algorithm.ToUpper(), mode, padding, digest, password.ToCharArray(),
                                Utils.ToByteArray(salt), iterations,keySize);

                pbeCipher=new Pkcs12PbeCipher(pbe);
                if(type!=null && type.ToUpper().Equals(Kpbe.Types.OPENSSL)){
                    pbeCipher=new OpenSSLPbeCipher(pbe);
                }
            }catch(Exception e){
                Console.WriteLine("kpbe: "+e.Message);
                return;
            }

            foreach(string file in files){
                FileInfo fi=new FileInfo(file);

                if(!fi.Exists){
                    Console.WriteLine ("kpbe: File "+file+" not found.");
                    return;
                }

                try{
                    DirectoryInfo odir=Directory.CreateDirectory(output);

                    Stream ins=new FileStream(file, FileMode.Open);
                    Stream outs=new FileStream(odir.FullName+"/"+fi.Name, FileMode.Create);

                    CipherStream cipherStream=new CipherStream(ins,pbeCipher.createCipher(encrypt), null);

                    int ch;
                    while ((ch = cipherStream.ReadByte()) >= 0)
                    {
                        outs.WriteByte((byte) ch);
                    }

                    cipherStream.Close();
                    outs.Close();
                }catch(CryptoException e){
                    Console.WriteLine("kpbe: "+e.Message);
                }catch(ArgumentException e){
                    Console.WriteLine("kpbe: "+e.Message);
                }catch(Exception e){
                    Console.WriteLine(e);
                }
            }
        }
コード例 #4
0
ファイル: BasePbeCipher.cs プロジェクト: kamranzafar/kpbe
 public BasePbeCipher(Pbe pbe)
 {
     this.pbe=pbe;
 }