private void doRunTest( string name, int ivLength) { string lCode = "ABCDEFGHIJKLMNOPQRSTUVWXY0123456789"; string baseName = name; if (name.IndexOf('/') >= 0) { baseName = name.Substring(0, name.IndexOf('/')); } CipherKeyGenerator kGen = GeneratorUtilities.GetKeyGenerator(baseName); IBufferedCipher inCipher = CipherUtilities.GetCipher(name); IBufferedCipher outCipher = CipherUtilities.GetCipher(name); KeyParameter key = ParameterUtilities.CreateKeyParameter(baseName, kGen.GenerateKey()); MemoryStream bIn = new MemoryStream(Encoding.ASCII.GetBytes(lCode), false); MemoryStream bOut = new MemoryStream(); // In the Java build, this IV would be implicitly created and then retrieved with getIV() ICipherParameters cipherParams = key; if (ivLength > 0) { cipherParams = new ParametersWithIV(cipherParams, new byte[ivLength]); } inCipher.Init(true, cipherParams); // TODO Should we provide GetIV() method on IBufferedCipher? //if (inCipher.getIV() != null) //{ // outCipher.Init(false, new ParametersWithIV(key, inCipher.getIV())); //} //else //{ // outCipher.Init(false, key); //} outCipher.Init(false, cipherParams); CipherStream cIn = new CipherStream(bIn, inCipher, null); CipherStream cOut = new CipherStream(bOut, null, outCipher); int c; while ((c = cIn.ReadByte()) >= 0) { cOut.WriteByte((byte)c); } cIn.Close(); cOut.Flush(); cOut.Close(); byte[] bs = bOut.ToArray(); string res = Encoding.ASCII.GetString(bs, 0, bs.Length); if (!res.Equals(lCode)) { Fail("Failed - decrypted data doesn't match."); } }
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); } } }