00001 using System;
00002 using System.IO;
00003 using Tamir.Streams;
00004 using Tamir.SharpSsh.jsch;
00005 using Tamir.SharpSsh.jsch.examples;
00006
00007 namespace Tamir
00008 {
00012 public class MainClass
00013 {
00014 public static void Main()
00015 {
00016 Console.WriteLine("sharpSsh 1.0");
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 testExamples();
00027
00028
00029
00030 }
00031 public static void test()
00032 {
00033 JSch jsch = new JSch();
00034 DH dh1 = null;
00035 DH dh2 = null;
00036 try
00037 {
00038 Type t=Type.GetType(jsch.getConfig("dh"));
00039 dh1=(DH)(Activator.CreateInstance(t));
00040 dh1.init();
00041 dh2=(DH)(Activator.CreateInstance(t));
00042 dh2.init();
00043 }
00044 catch(Exception ee)
00045 {
00046 Console.WriteLine(ee);
00047 }
00048
00049 dh1.setP(DHG1.p);
00050 dh1.setG(DHG1.g);
00051 dh2.setP(DHG1.p);
00052 dh2.setG(DHG1.g);
00053
00054
00055
00056
00057
00058
00059 byte[] e=dh1.getE();
00060 byte[] f=dh2.getE();
00061 Console.WriteLine("Private1 = {0}", hex(e));
00062 Console.WriteLine();
00063 Console.WriteLine("Private2 = {0}", hex(f));
00064 Console.WriteLine();
00065 dh1.setF(f);
00066 dh2.setF(e);
00067 byte[] k1 = dh1.getK();
00068 byte[] k2 = dh2.getK();
00069 Console.WriteLine("Public1 = {0}", hex(k1));
00070 Console.WriteLine();
00071 Console.WriteLine("Public2 = {0}", hex(k2));
00072 Console.WriteLine();
00073 }
00074
00075 public static void testSig()
00076 {
00077 byte[] hash = Util.getBytes( "Tamir" );
00078 Tamir.SharpSsh.jsch.jce.SignatureRSA enc_rsa = new Tamir.SharpSsh.jsch.jce.SignatureRSA();
00079 Tamir.SharpSsh.jsch.jce.SignatureRSA dec_rsa = new Tamir.SharpSsh.jsch.jce.SignatureRSA();
00080 Tamir.SharpSsh.jsch.jce.KeyPairGenRSA gen = new Tamir.SharpSsh.jsch.jce.KeyPairGenRSA();
00081 gen.init(512);
00082
00083 enc_rsa.init();
00084 enc_rsa.setPrvKey(gen.KeyInfo);
00085 enc_rsa.update(hash);
00086 byte[] sig = enc_rsa.sign();
00087
00088 dump(gen.getE(), gen.getN(), sig, hash);
00089
00090 dec_rsa.init();
00091 dec_rsa.setPubKey(gen.getE(), gen.getN());
00092 dec_rsa.update(hash);
00093
00094
00095
00096 Console.WriteLine( dec_rsa.verify(sig) );
00097 }
00098
00099 public static void testSigFromJava()
00100 {
00101 FileStream fs = File.OpenRead("e.bin");
00102 byte[] e = new byte[fs.Length];
00103 fs.Read(e, 0, e.Length);
00104 fs.Close();
00105
00106 fs = File.OpenRead("n.bin");
00107 byte[] n = new byte[fs.Length];
00108 fs.Read(n, 0, n.Length);
00109 fs.Close();
00110
00111 fs = File.OpenRead("sig.bin");
00112 byte[] sig = new byte[fs.Length];
00113 fs.Read(sig, 0, sig.Length);
00114 fs.Close();
00115
00116 Console.Write("E: ");
00117 Console.WriteLine( hex(e) );
00118 Console.Write("N: ");
00119 Console.WriteLine( hex(n) );
00120 Console.Write("SIG: ");
00121 Console.WriteLine( hex(sig) );
00122
00123 byte[] hash = Util.getBytes( "Tamir" );
00124 Tamir.SharpSsh.jsch.jce.SignatureRSA dec_rsa = new Tamir.SharpSsh.jsch.jce.SignatureRSA();
00125 dec_rsa.init();
00126 dec_rsa.setPubKey(e, n);
00127 dec_rsa.update(hash);
00128 Console.WriteLine( dec_rsa.verify(sig) );
00129
00130
00131 }
00132
00133 public static void dump(byte[] e, byte[] n, byte[] sig, byte[] hash)
00134 {
00135 String fname = "e.bin";
00136 if(File.Exists(fname)) File.Delete(fname);
00137 FileStream fs = File.OpenWrite(fname);
00138 fs.Write(e, 0, e.Length);
00139 fs.Close();
00140
00141
00142 fname = "n.bin";
00143 if(File.Exists(fname)) File.Delete(fname);
00144 fs = File.OpenWrite(fname);
00145 fs.Write(n, 0, n.Length);
00146 fs.Close();
00147
00148
00149 fname = "sig.bin";
00150 if(File.Exists(fname)) File.Delete(fname);
00151 fs = File.OpenWrite(fname);
00152 fs.Write(sig, 0, sig.Length);
00153 fs.Close();
00154
00155 fname = "hash.bin";
00156 if(File.Exists(fname)) File.Delete(fname);
00157 fs = File.OpenWrite(fname);
00158 fs.Write(hash, 0, hash.Length);
00159 fs.Close();
00160
00161
00162 Console.Write("E: ");
00163 Console.WriteLine( hex(e) );
00164 Console.WriteLine();
00165 Console.Write("N: ");
00166 Console.WriteLine( hex(n) );
00167 Console.WriteLine();
00168 Console.Write("SIG: ");
00169 Console.WriteLine( hex(sig) );
00170 Console.WriteLine();
00171 Console.Write("HASH: ");
00172 Console.WriteLine( hex(hash) );
00173 Console.WriteLine();
00174 }
00175
00176 public static void testDump()
00177 {
00178 String fname = "e.bin";
00179 FileStream fs = File.OpenRead(fname);
00180 byte[] e = new byte[fs.Length];
00181 fs.Read(e, 0, e.Length);
00182 fs.Close();
00183
00184 fname = "n.bin";
00185 fs = File.OpenRead(fname);
00186 byte[] n = new byte[fs.Length];
00187 fs.Read(n, 0, n.Length);
00188 fs.Close();
00189
00190 fname = "sig.bin";
00191 fs = File.OpenRead(fname);
00192 byte[] sig = new byte[fs.Length];
00193 fs.Read(sig, 0, sig.Length);
00194 fs.Close();
00195
00196 fname = "hash.bin";
00197 fs = File.OpenRead(fname);
00198 byte[] hash = new byte[fs.Length];
00199 fs.Read(hash, 0, hash.Length);
00200 fs.Close();
00201
00202 print("E", e);
00203 print("N", n);
00204 print("SIG", sig);
00205 print("HASH", hash);
00206
00207 Tamir.SharpSsh.jsch.jce.SignatureRSA dec_rsa = new Tamir.SharpSsh.jsch.jce.SignatureRSA();
00208 dec_rsa.init();
00209 dec_rsa.setPubKey(e, n);
00210 dec_rsa.update(hash);
00211
00212 Console.WriteLine( dec_rsa.verify(sig) );
00213 Console.WriteLine();
00214 }
00215
00216 public static void testDumpBase64()
00217 {
00218 String fname = "e.bin";
00219 StreamReader fs = File.OpenText(fname);
00220 string base64 = fs.ReadToEnd();
00221 byte[] e = Convert.FromBase64String( base64 );
00222 fs.Close();
00223
00224 fname = "n.bin";
00225 fs = File.OpenText(fname);
00226 base64 = fs.ReadToEnd();
00227 byte[] n = Convert.FromBase64String( base64 );
00228 fs.Close();
00229
00230 fname = "sig.bin";
00231 fs = File.OpenText(fname);
00232 base64 = fs.ReadToEnd();
00233 byte[] sig = Convert.FromBase64String( base64 );
00234 fs.Close();
00235
00236 fname = "hash.bin";
00237 fs = File.OpenText(fname);
00238 base64 = fs.ReadToEnd();
00239 byte[] hash = Convert.FromBase64String( base64 );
00240 fs.Close();
00241
00242 print("E", e);
00243 print("N", n);
00244 print("SIG", sig);
00245 print("HASH", hash);
00246
00247 Tamir.SharpSsh.jsch.jce.SignatureRSA dec_rsa = new Tamir.SharpSsh.jsch.jce.SignatureRSA();
00248 dec_rsa.init();
00249 dec_rsa.setPubKey(e, n);
00250 dec_rsa.update(hash);
00251
00252 Console.WriteLine( dec_rsa.verify(sig) );
00253 Console.WriteLine();
00254 }
00255
00256 public static void testHMacMD5()
00257 {
00258 byte[] msg = Util.getBytes("Tamir");
00259 byte[] key = new byte[]{1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6};
00260 Tamir.SharpSsh.jsch.jce.HMACMD5 md5 = new Tamir.SharpSsh.jsch.jce.HMACMD5();
00261 md5.init( key );
00262 md5.update(msg, 0, msg.Length);
00263 byte[] hash = md5.doFinal();
00264 Console.WriteLine(hex(hash));
00265 }
00266
00267 public static void testExamples()
00268 {
00269
00270
00271
00272
00273
00274
00275
00276
00277 Tamir.SharpSsh.jsch.examples.KnownHosts.RunExample(null);
00278
00279
00280
00281
00282
00283
00284
00285
00286 }
00287
00288 public static void jarAndScp()
00289 {
00290 TextReader r = File.OpenText( "jarAndScp.txt" );
00291 string dir = r.ReadLine();
00292 string host = r.ReadLine();
00293 string path = r.ReadLine();
00294 string user = r.ReadLine();
00295 string pass = r.ReadLine();
00296 r.Close();
00297 string file = dir+".jar";
00298 string jarFile = "\""+dir+".jar\"";
00299
00300 System.Diagnostics.ProcessStartInfo p = new System.Diagnostics.ProcessStartInfo(@"D:\Program Files\Java\jdk1.5.0_03\bin\jar.exe");
00301 p.WorkingDirectory = Directory.GetParent(dir).FullName;
00302 p.Arguments = "-cf "+jarFile+" "+ Path.GetFileName(dir);
00303 p.UseShellExecute = false;
00304
00305
00306 System.Diagnostics.Process.Start(p);
00307 System.Diagnostics.Process pr = new System.Diagnostics.Process();
00308 pr.StartInfo = p;
00309 pr.Start();
00310 pr.WaitForExit();
00311
00312 String[] arg = new string[]{file, user+"@"+host+":"+path+Path.GetFileName(file)};
00313
00314 }
00315
00316 public static void print(string name, byte[] data)
00317 {
00318 Console.WriteLine();
00319 Console.Write(name+": ");
00320 Console.WriteLine( hex(data) );
00321 Console.WriteLine();
00322 }
00323
00324 public static string hex(byte[] arr)
00325 {
00326 string hex = "0x";
00327 for(int i=0;i<arr.Length; i++)
00328 {
00329 string mbyte = arr[i].ToString("X");
00330 if (mbyte.Length == 1)
00331 mbyte = "0"+mbyte;
00332 hex += mbyte;
00333 }
00334 return hex;
00335 }
00336
00337 public static byte[] reverse(byte[] arr)
00338 {
00339 byte[] tmp = new byte[arr.Length];
00340 for(int i=0; i<arr.Length; i++)
00341 {
00342 tmp[i] = arr[ arr.Length-1-i];
00343 }
00344 return tmp;
00345 }
00346
00352 public static byte[] stripLeadingZeros(byte[] a)
00353 {
00354 int lastZero = -1;
00355 for (int i = 0; i < a.Length; i++)
00356 {
00357 if (a[i] == 0)
00358 {
00359 lastZero = i;
00360 }
00361 else
00362 {
00363 break;
00364 }
00365 }
00366 lastZero++;
00367 byte[] result = new byte[a.Length - lastZero];
00368 Array.Copy(a, lastZero, result, 0, result.Length);
00369 return result;
00370 }
00371 }
00372 }