internal static string GetFingerPrint(HASH hash, byte[] data) { try { hash.Init(); hash.Update(data, 0, data.Length); byte[] foo = hash.Digest(); StringBuilder sb = new StringBuilder(); int bar; for (int i = 0; i < foo.Length; i++) { bar = foo[i] & unchecked ((int)(0xff)); sb.Append(chars[((int)(((uint)bar) >> 4)) & unchecked ((int)(0xf))]); sb.Append(chars[(bar) & unchecked ((int)(0xf))]); if (i + 1 < foo.Length) { sb.Append(":"); } } return(sb.ToString()); } catch (Exception) { return("???"); } }
/* Initialize RNG with some real entropy from some external source */ public virtual void seed(int rawlen, sbyte[] raw) { // initialise from at least 128 byte string of raw random entropy int i; sbyte[] digest; sbyte[] b = new sbyte[4]; HASH sh = new HASH(); pool_ptr = 0; for (i = 0; i < NK; i++) { ira[i] = 0; } if (rawlen > 0) { for (i = 0; i < rawlen; i++) { sh.process(raw[i]); } digest = sh.hash(); /* initialise PRNG from distilled randomness */ for (i = 0; i < 8; i++) { b[0] = digest[4 * i]; b[1] = digest[4 * i + 1]; b[2] = digest[4 * i + 2]; b[3] = digest[4 * i + 3]; sirand(pack(b)); } } fill_pool(); }
/* IEEE1363 ECDSA Signature Verification. Signature C and D on F is verified using public key W */ public static int ECPVP_DSA(sbyte[] W, sbyte[] F, sbyte[] C, sbyte[] D) { BIG r, gx, gy, f, c, d, h2; int res = 0; ECP G, WP, P; HASH H = new HASH(); H.process_array(F); sbyte[] B = H.hash(); gx = new BIG(ROM.CURVE_Gx); gy = new BIG(ROM.CURVE_Gy); G = new ECP(gx, gy); r = new BIG(ROM.CURVE_Order); c = BIG.fromBytes(C); d = BIG.fromBytes(D); f = BIG.fromBytes(B); if (c.iszilch() || BIG.comp(c, r) >= 0 || d.iszilch() || BIG.comp(d, r) >= 0) { res = INVALID; } if (res == 0) { d.invmodp(r); f.copy(BIG.modmul(f, d, r)); h2 = BIG.modmul(c, d, r); WP = ECP.fromBytes(W); if (WP.is_infinity()) { res = ERROR; } else { P = new ECP(); P.copy(WP); P = P.mul2(h2, G, f); if (P.is_infinity()) { res = INVALID; } else { d = P.X; d.mod(r); if (BIG.comp(d, c) != 0) { res = INVALID; } } } } return(res); }
public static string getFingerPrint(HASH hash, byte[] data) { try { hash.init(); hash.update(data, 0, data.Length); byte[] foo = hash.digest(); StringBuilder sb = new StringBuilder(); int bar; for (int i = 0; i < foo.Length; i++) { bar = foo[i] & 0xff; sb.Append(chars[(((uint)bar) >> 4) & 0xf]); sb.Append(chars[(bar) & 0xf]); if (i + 1 < foo.Length) { sb.Append(":"); } } return(sb.ToString()); } catch //(Exception e) { return("???"); } }
byte[] genKey(byte[] passphrase, byte[] iv) { if (cipher == null) { cipher = genCipher(); } if (hash == null) { hash = genHash(); } byte[] key = new byte[cipher.getBlockSize()]; int hsize = hash.getBlockSize(); byte[] hn = new byte[key.Length / hsize * hsize + (key.Length % hsize == 0 ? 0 : hsize)]; try { byte[] tmp = null; if (vendor == VENDOR_OPENSSH) { for (int index = 0; index + hsize <= hn.Length;) { if (tmp != null) { hash.update(tmp, 0, tmp.Length); } hash.update(passphrase, 0, passphrase.Length); hash.update(iv, 0, iv.Length); tmp = hash.digest(); Array.Copy(tmp, 0, hn, index, tmp.Length); index += tmp.Length; } Array.Copy(hn, 0, key, 0, key.Length); } else if (vendor == VENDOR_FSECURE) { for (int index = 0; index + hsize <= hn.Length;) { if (tmp != null) { hash.update(tmp, 0, tmp.Length); } hash.update(passphrase, 0, passphrase.Length); tmp = hash.digest(); Array.Copy(tmp, 0, hn, index, tmp.Length); index += tmp.Length; } Array.Copy(hn, 0, key, 0, key.Length); } } catch (Exception e) { Console.Error.WriteLine(e); } return(key); }
/* IEEE ECDSA Signature, C and D are signature on F using private key S */ public static int ECPSP_DSA(RAND RNG, sbyte[] S, sbyte[] F, sbyte[] C, sbyte[] D) { sbyte[] T = new sbyte[EFS]; BIG gx, gy, r, s, f, c, d, u, vx; ECP G, V; HASH H = new HASH(); H.process_array(F); sbyte[] B = H.hash(); gx = new BIG(ROM.CURVE_Gx); gy = new BIG(ROM.CURVE_Gy); G = new ECP(gx, gy); r = new BIG(ROM.CURVE_Order); s = BIG.fromBytes(S); f = BIG.fromBytes(B); c = new BIG(0); d = new BIG(0); V = new ECP(); do { u = BIG.randomnum(r, RNG); V.copy(G); V = V.mul(u); vx = V.X; c.copy(vx); c.mod(r); if (c.iszilch()) { continue; } u.invmodp(r); d.copy(BIG.modmul(s, c, r)); d.add(f); d.copy(BIG.modmul(u, d, r)); } while (d.iszilch()); c.toBytes(T); for (int i = 0; i < EFS; i++) { C[i] = T[i]; } d.toBytes(T); for (int i = 0; i < EFS; i++) { D[i] = T[i]; } return(0); }
private void fill_pool() { HASH sh = new HASH(); for (int i = 0; i < 128; i++) { sh.process(sbrand()); } pool = sh.hash(); pool_ptr = 0; }
public string getFingerPrint(JSch jsch) { HASH hash = null; try { Type c = Type.GetType(JSch.getConfig("md5")); hash = (HASH)(c.newInstance()); } catch (Exception e) { Console.Error.WriteLine("getFingerPrint: " + e); } return(Util.getFingerPrint(hash, key)); }
public const int TRAP = 200; // 200 for 4 digit PIN, 2000 for 6-digit PIN - approx 2*sqrt(MAXPIN) /* Hash number (optional) and string to point on curve */ public static sbyte[] hashit(int n, sbyte[] ID) { HASH H = new HASH(); if (n != 0) { H.process_num(n); } H.process_array(ID); sbyte[] h = H.hash(); return(h); }
public string getFingerPrint() { if (hash == null) { hash = genHash(); } byte[] kblob = getPublicKeyBlob(); if (kblob == null) { return(null); } return(getKeySize() + " " + Util.getFingerPrint(hash, kblob)); }
private HASH genHash() { try { Type c = Type.GetType(JSch.getConfig("md5")); hash = (HASH)(c.newInstance()); hash.init(); } catch //(Exception e) { } return(hash); }
public virtual string GetFingerPrint() { if (hash == null) { hash = GenHash(); } byte[] kblob = GetPublicKeyBlob(); if (kblob == null) { return(null); } return(GetKeySize() + " " + Util.GetFingerPrint(hash, kblob)); }
private HASH GenHash() { try { Type c = Sharpen.Runtime.GetType(JSch.GetConfig("md5")); hash = (HASH)(System.Activator.CreateInstance(c)); hash.Init(); } catch (Exception) { } return(hash); }
public virtual string GetFingerPrint(JSch jsch) { HASH hash = null; try { Type c = Sharpen.Runtime.GetType(JSch.GetConfig("md5")); hash = (HASH)(System.Activator.CreateInstance(c)); } catch (Exception e) { System.Console.Error.WriteLine("getFingerPrint: " + e); } return(Util.GetFingerPrint(hash, key)); }
/* Key Derivation Functions */ /* Input octet Z */ /* Output key of length olen */ public static sbyte[] KDF1(sbyte[] Z, int olen) { /* NOTE: the parameter olen is the length of the output K in bytes */ HASH H = new HASH(); int hlen = HASH.len; sbyte[] K = new sbyte[olen]; sbyte[] B; int counter, cthreshold, k = 0; for (int i = 0; i < K.Length; i++) { K[i] = 0; } cthreshold = olen / hlen; if (olen % hlen != 0) { cthreshold++; } for (counter = 0; counter < cthreshold; counter++) { H.process_array(Z); if (counter > 0) { H.process_num(counter); } B = H.hash(); if (k + hlen > olen) { for (int i = 0; i < olen % hlen; i++) { K[k++] = B[i]; } } else { for (int i = 0; i < hlen; i++) { K[k++] = B[i]; } } } return(K); }
/* Mask Generation Function */ public static void MGF1(sbyte[] Z, int olen, sbyte[] K) { HASH H = new HASH(); int hlen = HASH.len; sbyte[] B = new sbyte[hlen]; int counter, cthreshold, k = 0; for (int i = 0; i < K.Length; i++) { K[i] = 0; } cthreshold = olen / hlen; if (olen % hlen != 0) { cthreshold++; } for (counter = 0; counter < cthreshold; counter++) { H.process_array(Z); H.process_num(counter); B = H.hash(); if (k + hlen > olen) { for (int i = 0; i < olen % hlen; i++) { K[k++] = B[i]; } } else { for (int i = 0; i < hlen; i++) { K[k++] = B[i]; } } } }
public void Test_CTF() { byte[] source_buf = File.ReadAllBytes("testdata/Message.ctf"); var target = new CTF.File(new ByteReader(source_buf)); Assert.AreEqual(3204, target.Messages.Count); Assert.AreEqual("UniCastle", target.GetMessage(0, 6, 312).Text); Assert.AreEqual("Frost Staff@", target.GetMessage(4, 4, 22).Text); Assert.AreEqual(10, target.Categories.Count); Assert.AreEqual("Skills", target.Categories[2].Name); Assert.AreEqual("Items1:Weapons", target.Categories[4].Name); Assert.AreEqual("Kitara", target.Categories[2][7].Name); Assert.AreEqual("Wand", target.Categories[4][4].Name); Assert.AreEqual( HASH.SHA256(new MemoryStream(source_buf)), HASH.SHA256(new MemoryStream(target.Save( new ByteWriter()).ToByteArray()))); }
public virtual string GetFingerPrint() { if (hash == null) { hash = GenHash(); } byte[] kblob = GetPublicKeyBlob(); if (kblob == null) { return null; } return GetKeySize() + " " + Util.GetFingerPrint(hash, kblob); }
/* OAEP Message Encoding for Encryption */ public static sbyte[] OAEP_ENCODE(sbyte[] m, RAND rng, sbyte[] p) { int i, slen, olen = RFS - 1; int mlen = m.Length; int hlen, seedlen; sbyte[] f = new sbyte[RFS]; HASH H = new HASH(); hlen = HASH.len; sbyte[] SEED = new sbyte[hlen]; seedlen = hlen; if (mlen > olen - hlen - seedlen - 1) { return(new sbyte[0]); } sbyte[] DBMASK = new sbyte[olen - seedlen]; if (p != null) { H.process_array(p); } sbyte[] h = H.hash(); for (i = 0; i < hlen; i++) { f[i] = h[i]; } slen = olen - mlen - hlen - seedlen - 1; for (i = 0; i < slen; i++) { f[hlen + i] = 0; } f[hlen + slen] = 1; for (i = 0; i < mlen; i++) { f[hlen + slen + 1 + i] = m[i]; } for (i = 0; i < seedlen; i++) { SEED[i] = (sbyte)rng.Byte; } MGF1(SEED, olen - seedlen, DBMASK); for (i = 0; i < olen - seedlen; i++) { DBMASK[i] ^= f[i]; } MGF1(DBMASK, seedlen, f); for (i = 0; i < seedlen; i++) { f[i] ^= SEED[i]; } for (i = 0; i < olen - seedlen; i++) { f[i + seedlen] = DBMASK[i]; } /* pad to length RFS */ int d = 1; for (i = RFS - 1; i >= d; i--) { f[i] = f[i - d]; } for (i = d - 1; i >= 0; i--) { f[i] = 0; } return(f); }
byte[] genKey(byte[] passphrase, byte[] iv) { if (cipher == null) cipher = genCipher(); if (hash == null) hash = genHash(); byte[] key = new byte[cipher.getBlockSize()]; int hsize = hash.getBlockSize(); byte[] hn = new byte[key.Length / hsize * hsize + (key.Length % hsize == 0 ? 0 : hsize)]; try { byte[] tmp = null; if (vendor == VENDOR_OPENSSH) { for (int index = 0; index + hsize <= hn.Length; ) { if (tmp != null) { hash.update(tmp, 0, tmp.Length); } hash.update(passphrase, 0, passphrase.Length); hash.update(iv, 0, iv.Length); tmp = hash.digest(); Array.Copy(tmp, 0, hn, index, tmp.Length); index += tmp.Length; } Array.Copy(hn, 0, key, 0, key.Length); } else if (vendor == VENDOR_FSECURE) { for (int index = 0; index + hsize <= hn.Length; ) { if (tmp != null) { hash.update(tmp, 0, tmp.Length); } hash.update(passphrase, 0, passphrase.Length); tmp = hash.digest(); Array.Copy(tmp, 0, hn, index, tmp.Length); index += tmp.Length; } Array.Copy(hn, 0, key, 0, key.Length); } } catch (Exception e) { Console.Error.WriteLine(e); } return key; }
internal static string GetFingerPrint(HASH hash, byte[] data) { try { hash.Init(); hash.Update(data, 0, data.Length); byte[] foo = hash.Digest(); StringBuilder sb = new StringBuilder(); int bar; for (int i = 0; i < foo.Length; i++) { bar = foo[i] & unchecked((int)(0xff)); sb.Append(chars[((int)(((uint)bar) >> 4)) & unchecked((int)(0xf))]); sb.Append(chars[(bar) & unchecked((int)(0xf))]); if (i + 1 < foo.Length) { sb.Append(":"); } } return sb.ToString(); } catch (Exception) { return "???"; } }
/* Calculate HMAC of m using key k. HMAC is tag of length olen */ public static int HMAC(sbyte[] M, sbyte[] K, sbyte[] tag) { /* Input is from an octet m * * olen is requested output length in bytes. k is the key * * The output is the calculated tag */ int b; sbyte[] B; sbyte[] K0 = new sbyte[64]; int olen = tag.Length; b = K0.Length; if (olen < 4 || olen > HASH.len) { return(0); } for (int i = 0; i < b; i++) { K0[i] = 0; } HASH H = new HASH(); if (K.Length > b) { H.process_array(K); B = H.hash(); for (int i = 0; i < 32; i++) { K0[i] = B[i]; } } else { for (int i = 0; i < K.Length; i++) { K0[i] = K[i]; } } for (int i = 0; i < b; i++) { K0[i] ^= 0x36; } H.process_array(K0); H.process_array(M); B = H.hash(); for (int i = 0; i < b; i++) { K0[i] ^= 0x6a; } H.process_array(K0); H.process_array(B); B = H.hash(); for (int i = 0; i < olen; i++) { tag[i] = B[i]; } return(1); }
/// <exception cref="NSch.JSchException"></exception> private IdentityFile(string name, byte[] prvkey, byte[] pubkey, JSch jsch) { this.identity = name; this.jsch = jsch; try { Type c; c = Sharpen.Runtime.GetType((string)JSch.GetConfig("3des-cbc")); cipher = (NSch.Cipher)(System.Activator.CreateInstance(c)); key = new byte[cipher.GetBlockSize()]; // 24 iv = new byte[cipher.GetIVSize()]; // 8 c = Sharpen.Runtime.GetType((string)JSch.GetConfig("md5")); hash = (HASH)(System.Activator.CreateInstance(c)); hash.Init(); byte[] buf = prvkey; int len = buf.Length; int i = 0; while (i < len) { if (buf[i] == '-' && i + 4 < len && buf[i + 1] == '-' && buf[i + 2] == '-' && buf [i + 3] == '-' && buf[i + 4] == '-') { break; } i++; } while (i < len) { if (buf[i] == 'B' && i + 3 < len && buf[i + 1] == 'E' && buf[i + 2] == 'G' && buf [i + 3] == 'I') { i += 6; if (buf[i] == 'D' && buf[i + 1] == 'S' && buf[i + 2] == 'A') { type = DSS; } else { if (buf[i] == 'R' && buf[i + 1] == 'S' && buf[i + 2] == 'A') { type = RSA; } else { if (buf[i] == 'S' && buf[i + 1] == 'S' && buf[i + 2] == 'H') { // FSecure type = UNKNOWN; keytype = FSECURE; } else { //System.err.println("invalid format: "+identity); throw new JSchException("invalid privatekey: " + identity); } } } i += 3; continue; } if (buf[i] == 'A' && i + 7 < len && buf[i + 1] == 'E' && buf[i + 2] == 'S' && buf [i + 3] == '-' && buf[i + 4] == '2' && buf[i + 5] == '5' && buf[i + 6] == '6' && buf[i + 7] == '-') { i += 8; if (Session.CheckCipher((string)JSch.GetConfig("aes256-cbc"))) { c = Sharpen.Runtime.GetType((string)JSch.GetConfig("aes256-cbc")); cipher = (NSch.Cipher)(System.Activator.CreateInstance(c)); key = new byte[cipher.GetBlockSize()]; iv = new byte[cipher.GetIVSize()]; } else { throw new JSchException("privatekey: aes256-cbc is not available " + identity); } continue; } if (buf[i] == 'C' && i + 3 < len && buf[i + 1] == 'B' && buf[i + 2] == 'C' && buf [i + 3] == ',') { i += 4; for (int ii = 0; ii < iv.Length; ii++) { iv[ii] = unchecked((byte)(((A2b(buf[i++]) << 4) & unchecked((int)(0xf0))) + (A2b( buf[i++]) & unchecked((int)(0xf))))); } continue; } if (buf[i] == unchecked((int)(0x0d)) && i + 1 < len && buf[i + 1] == unchecked((int )(0x0a))) { i++; continue; } if (buf[i] == unchecked((int)(0x0a)) && i + 1 < len) { if (buf[i + 1] == unchecked((int)(0x0a))) { i += 2; break; } if (buf[i + 1] == unchecked((int)(0x0d)) && i + 2 < len && buf[i + 2] == unchecked( (int)(0x0a))) { i += 3; break; } bool inheader = false; for (int j = i + 1; j < len; j++) { if (buf[j] == unchecked((int)(0x0a))) { break; } //if(buf[j]==0x0d) break; if (buf[j] == ':') { inheader = true; break; } } if (!inheader) { i++; encrypted = false; // no passphrase break; } } i++; } if (type == ERROR) { throw new JSchException("invalid privatekey: " + identity); } int start = i; while (i < len) { if (buf[i] == unchecked((int)(0x0a))) { bool xd = (buf[i - 1] == unchecked((int)(0x0d))); System.Array.Copy(buf, i + 1, buf, i - (xd ? 1 : 0), len - i - 1 - (xd ? 1 : 0)); if (xd) { len--; } len--; continue; } if (buf[i] == '-') { break; } i++; } encoded_data = Util.FromBase64(buf, start, i - start); if (encoded_data.Length > 4 && encoded_data[0] == unchecked((byte)unchecked((int) (0x3f))) && encoded_data[1] == unchecked((byte)unchecked((int)(0x6f))) && encoded_data [2] == unchecked((byte)unchecked((int)(0xf9))) && encoded_data[3] == unchecked(( byte)unchecked((int)(0xeb)))) { // FSecure Buffer _buf = new Buffer(encoded_data); _buf.GetInt(); // 0x3f6ff9be _buf.GetInt(); byte[] _type = _buf.GetString(); //System.err.println("type: "+new String(_type)); byte[] _cipher = _buf.GetString(); string cipher2 = Util.Byte2str(_cipher); //System.err.println("cipher: "+cipher); if (cipher2.Equals("3des-cbc")) { _buf.GetInt(); byte[] foo = new byte[encoded_data.Length - _buf.GetOffSet()]; _buf.GetByte(foo); encoded_data = foo; encrypted = true; throw new JSchException("unknown privatekey format: " + identity); } else { if (cipher2.Equals("none")) { _buf.GetInt(); //_buf.getInt(); encrypted = false; byte[] foo = new byte[encoded_data.Length - _buf.GetOffSet()]; _buf.GetByte(foo); encoded_data = foo; } } } if (pubkey == null) { return; } buf = pubkey; len = buf.Length; if (buf.Length > 4 && buf[0] == '-' && buf[1] == '-' && buf[2] == '-' && buf[3] == '-') { // FSecure's public key i = 0; do { i++; } while (len > i && buf[i] != unchecked((int)(0x0a))); if (len <= i) { return; } while (i < len) { if (buf[i] == unchecked((int)(0x0a))) { bool inheader = false; for (int j = i + 1; j < len; j++) { if (buf[j] == unchecked((int)(0x0a))) { break; } if (buf[j] == ':') { inheader = true; break; } } if (!inheader) { i++; break; } } i++; } if (len <= i) { return; } start = i; while (i < len) { if (buf[i] == unchecked((int)(0x0a))) { System.Array.Copy(buf, i + 1, buf, i, len - i - 1); len--; continue; } if (buf[i] == '-') { break; } i++; } publickeyblob = Util.FromBase64(buf, start, i - start); if (type == UNKNOWN && publickeyblob.Length > 8) { if (publickeyblob[8] == 'd') { type = DSS; } else { if (publickeyblob[8] == 'r') { type = RSA; } } } } else { if (buf[0] != 's' || buf[1] != 's' || buf[2] != 'h' || buf[3] != '-') { return; } i = 0; while (i < len) { if (buf[i] == ' ') { break; } i++; } i++; if (i >= len) { return; } start = i; while (i < len) { if (buf[i] == ' ' || buf[i] == '\n') { break; } i++; } publickeyblob = Util.FromBase64(buf, start, i - start); if (publickeyblob.Length < 4 + 7) { // It must start with "ssh-XXX". if (JSch.GetLogger().IsEnabled(Logger.WARN)) { JSch.GetLogger().Log(Logger.WARN, "failed to parse the public key"); } publickeyblob = null; } } } catch (Exception e) { //System.err.println("IdentityFile: "+e); if (e is JSchException) { throw (JSchException)e; } if (e is Exception) { throw new JSchException(e.ToString(), (Exception)e); } throw new JSchException(e.ToString()); } }
/* OAEP Message Decoding for Decryption */ public static sbyte[] OAEP_DECODE(sbyte[] p, sbyte[] f) { int x, t; bool comp; int i, k, olen = RFS - 1; int hlen, seedlen; HASH H = new HASH(); hlen = HASH.len; sbyte[] SEED = new sbyte[hlen]; seedlen = hlen; sbyte[] CHASH = new sbyte[hlen]; if (olen < seedlen + hlen + 1) { return(new sbyte[0]); } sbyte[] DBMASK = new sbyte[olen - seedlen]; for (i = 0; i < olen - seedlen; i++) { DBMASK[i] = 0; } if (f.Length < RFS) { int d = RFS - f.Length; for (i = RFS - 1; i >= d; i--) { f[i] = f[i - d]; } for (i = d - 1; i >= 0; i--) { f[i] = 0; } } if (p != null) { H.process_array(p); } sbyte[] h = H.hash(); for (i = 0; i < hlen; i++) { CHASH[i] = h[i]; } x = f[0]; for (i = seedlen; i < olen; i++) { DBMASK[i - seedlen] = f[i + 1]; } MGF1(DBMASK, seedlen, SEED); for (i = 0; i < seedlen; i++) { SEED[i] ^= f[i + 1]; } MGF1(SEED, olen - seedlen, f); for (i = 0; i < olen - seedlen; i++) { DBMASK[i] ^= f[i]; } comp = true; for (i = 0; i < hlen; i++) { if (CHASH[i] != DBMASK[i]) { comp = false; } } for (i = 0; i < olen - seedlen - hlen; i++) { DBMASK[i] = DBMASK[i + hlen]; } for (i = 0; i < hlen; i++) { SEED[i] = CHASH[i] = 0; } for (k = 0;; k++) { if (k >= olen - seedlen - hlen) { return(new sbyte[0]); } if (DBMASK[k] != 0) { break; } } t = DBMASK[k]; if (!comp || x != 0 || t != 0x01) { for (i = 0; i < olen - seedlen; i++) { DBMASK[i] = 0; } return(new sbyte[0]); } sbyte[] r = new sbyte[olen - seedlen - hlen - k - 1]; for (i = 0; i < olen - seedlen - hlen - k - 1; i++) { r[i] = DBMASK[i + k + 1]; } for (i = 0; i < olen - seedlen; i++) { DBMASK[i] = 0; } return(r); }
public static string getFingerPrint(HASH hash, byte[] data) { try { hash.init(); hash.update(data, 0, data.Length); byte[] foo = hash.digest(); StringBuilder sb = new StringBuilder(); int bar; for (int i = 0; i < foo.Length; i++) { bar = foo[i] & 0xff; sb.Append(chars[(((uint)bar) >> 4) & 0xf]); sb.Append(chars[(bar) & 0xf]); if (i + 1 < foo.Length) sb.Append(":"); } return sb.ToString(); } catch //(Exception e) { return "???"; } }
/* calculate common key on server side */ /* Z=r.A - no time permits involved */ public static int SERVER_KEY(sbyte[] Z, sbyte[] SST, sbyte[] W, sbyte[] xID, sbyte[] xCID, sbyte[] SK) { HASH H = new HASH(); sbyte[] t = new sbyte[EFS]; ECP2 sQ = ECP2.fromBytes(SST); if (sQ.is_infinity()) { return(INVALID_POINT); } ECP R = ECP.fromBytes(Z); if (R.is_infinity()) { return(INVALID_POINT); } ECP U; if (xCID != null) { U = ECP.fromBytes(xCID); } else { U = ECP.fromBytes(xID); } if (U.is_infinity()) { return(INVALID_POINT); } BIG w = BIG.fromBytes(W); U = PAIR.G1mul(U, w); FP12 g = PAIR.ate(sQ, R); g = PAIR.fexp(g); FP4 c = g.trace(); c.geta().A.toBytes(t); H.process_array(t); c.geta().B.toBytes(t); H.process_array(t); c.getb().A.toBytes(t); H.process_array(t); c.getb().B.toBytes(t); H.process_array(t); U.X.toBytes(t); H.process_array(t); U.Y.toBytes(t); H.process_array(t); t = H.hash(); for (int i = 0; i < PAS; i++) { SK[i] = t[i]; } return(0); }
static void Main(string[] args) { HASH hash = new HASH(); int opcion; Menu(); void Menu() { Console.WriteLine("MENÚ BUSQUEDA HASH" + '\n'); Console.WriteLine("1. Generar arreglo K"); Console.WriteLine("2. Desplegar arreglo K"); Console.WriteLine("3. Generar direcciones"); Console.WriteLine("4. Desplegar vector V y Buscar un elemento"); Console.WriteLine("5. Salir del Programa"); Console.Write('\n' + "Seleccione una opción: "); opcion = int.Parse(Console.ReadLine()); switch (opcion) { case 1: opcion = 1; { Console.Clear(); hash.Generar(); hash.InicializarV(); Console.WriteLine("Arreglo generado con éxito"); Console.ReadKey(); Console.Clear(); Menu(); break; } case 2: opcion = 2; { Console.Clear(); Console.WriteLine("ARREGLO: " + '\n'); hash.Imprimir(); Console.ReadKey(); Console.Clear(); Menu(); break; } case 3: opcion = 3; { Console.Clear(); hash.AsignarDireccion(); Console.WriteLine("Direcciones asignadas con éxito"); Console.ReadKey(); Console.Clear(); Menu(); break; } case 4: opcion = 4; { Console.Clear(); hash.ImprimirDireccion(); Console.WriteLine('\n'); hash.Buscar(); Console.ReadKey(); Console.Clear(); Menu(); break; } case 5: opcion = 5; { Console.Clear(); Console.WriteLine("Presione cualquier tecla para continuar"); Console.ReadKey(); break; } } } }
public string getFingerPrint() { if (hash == null) hash = genHash(); byte[] kblob = getPublicKeyBlob(); if (kblob == null) return null; return getKeySize() + " " + Util.getFingerPrint(hash, kblob); }
private HASH genHash() { try { Type c = Type.GetType(JSch.getConfig("md5")); hash = (HASH)(c.newInstance()); hash.init(); } catch //(Exception e) { } return hash; }
/// <exception cref="NSch.JSchException"></exception> private IdentityFile(string name, byte[] prvkey, byte[] pubkey, JSch jsch) { this.identity = name; this.jsch = jsch; // prvkey from "ssh-add" command on the remote. if (pubkey == null && prvkey != null && (prvkey.Length > 11 && prvkey[0] == 0 && prvkey[1] == 0 && prvkey[2] == 0 && prvkey[3] == 7)) { Buffer buf = new Buffer(prvkey); string _type = Sharpen.Runtime.GetStringForBytes(buf.GetString()); // ssh-rsa if (_type.Equals("ssh-rsa")) { type = RSA; n_array = buf.GetString(); e_array = buf.GetString(); d_array = buf.GetString(); buf.GetString(); buf.GetString(); buf.GetString(); this.identity += Sharpen.Runtime.GetStringForBytes(buf.GetString()); } else { if (_type.Equals("ssh-dss")) { type = DSS; P_array = buf.GetString(); Q_array = buf.GetString(); G_array = buf.GetString(); pub_array = buf.GetString(); prv_array = buf.GetString(); this.identity += Sharpen.Runtime.GetStringForBytes(buf.GetString()); } else { throw new JSchException("privatekey: invalid key " + Sharpen.Runtime.GetStringForBytes (prvkey, 4, 7)); } } encoded_data = prvkey; encrypted = false; keytype = OPENSSH; return; } try { Type c; c = Sharpen.Runtime.GetType((string)JSch.GetConfig("3des-cbc")); cipher = (NSch.Cipher)(System.Activator.CreateInstance(c)); key = new byte[cipher.GetBlockSize()]; // 24 iv = new byte[cipher.GetIVSize()]; // 8 c = Sharpen.Runtime.GetType((string)JSch.GetConfig("md5")); hash = (HASH)(System.Activator.CreateInstance(c)); hash.Init(); byte[] buf = prvkey; int len = buf.Length; int i = 0; while (i < len) { if (buf[i] == '-' && i + 4 < len && buf[i + 1] == '-' && buf[i + 2] == '-' && buf [i + 3] == '-' && buf[i + 4] == '-') { break; } i++; } while (i < len) { if (buf[i] == 'B' && i + 3 < len && buf[i + 1] == 'E' && buf[i + 2] == 'G' && buf [i + 3] == 'I') { i += 6; if (buf[i] == 'D' && buf[i + 1] == 'S' && buf[i + 2] == 'A') { type = DSS; } else { if (buf[i] == 'R' && buf[i + 1] == 'S' && buf[i + 2] == 'A') { type = RSA; } else { if (buf[i] == 'S' && buf[i + 1] == 'S' && buf[i + 2] == 'H') { // FSecure type = UNKNOWN; keytype = FSECURE; } else { //System.err.println("invalid format: "+identity); throw new JSchException("invalid privatekey: " + identity); } } } i += 3; continue; } if (buf[i] == 'A' && i + 7 < len && buf[i + 1] == 'E' && buf[i + 2] == 'S' && buf [i + 3] == '-' && buf[i + 4] == '2' && buf[i + 5] == '5' && buf[i + 6] == '6' && buf[i + 7] == '-') { i += 8; if (Session.CheckCipher((string)JSch.GetConfig("aes256-cbc"))) { c = Sharpen.Runtime.GetType((string)JSch.GetConfig("aes256-cbc")); cipher = (NSch.Cipher)(System.Activator.CreateInstance(c)); key = new byte[cipher.GetBlockSize()]; iv = new byte[cipher.GetIVSize()]; } else { throw new JSchException("privatekey: aes256-cbc is not available " + identity); } continue; } if (buf[i] == 'A' && i + 7 < len && buf[i + 1] == 'E' && buf[i + 2] == 'S' && buf [i + 3] == '-' && buf[i + 4] == '1' && buf[i + 5] == '9' && buf[i + 6] == '2' && buf[i + 7] == '-') { i += 8; if (Session.CheckCipher((string)JSch.GetConfig("aes192-cbc"))) { c = Sharpen.Runtime.GetType((string)JSch.GetConfig("aes192-cbc")); cipher = (NSch.Cipher)(System.Activator.CreateInstance(c)); key = new byte[cipher.GetBlockSize()]; iv = new byte[cipher.GetIVSize()]; } else { throw new JSchException("privatekey: aes192-cbc is not available " + identity); } continue; } if (buf[i] == 'A' && i + 7 < len && buf[i + 1] == 'E' && buf[i + 2] == 'S' && buf [i + 3] == '-' && buf[i + 4] == '1' && buf[i + 5] == '2' && buf[i + 6] == '8' && buf[i + 7] == '-') { i += 8; if (Session.CheckCipher((string)JSch.GetConfig("aes128-cbc"))) { c = Sharpen.Runtime.GetType((string)JSch.GetConfig("aes128-cbc")); cipher = (NSch.Cipher)(System.Activator.CreateInstance(c)); key = new byte[cipher.GetBlockSize()]; iv = new byte[cipher.GetIVSize()]; } else { throw new JSchException("privatekey: aes128-cbc is not available " + identity); } continue; } if (buf[i] == 'C' && i + 3 < len && buf[i + 1] == 'B' && buf[i + 2] == 'C' && buf [i + 3] == ',') { i += 4; for (int ii = 0; ii < iv.Length; ii++) { iv[ii] = unchecked ((byte)(((A2b(buf[i++]) << 4) & unchecked ((int)(0xf0))) + (A2b( buf[i++]) & unchecked ((int)(0xf))))); } continue; } if (buf[i] == unchecked ((int)(0x0d)) && i + 1 < len && buf[i + 1] == unchecked ((int )(0x0a))) { i++; continue; } if (buf[i] == unchecked ((int)(0x0a)) && i + 1 < len) { if (buf[i + 1] == unchecked ((int)(0x0a))) { i += 2; break; } if (buf[i + 1] == unchecked ((int)(0x0d)) && i + 2 < len && buf[i + 2] == unchecked ( (int)(0x0a))) { i += 3; break; } bool inheader = false; for (int j = i + 1; j < len; j++) { if (buf[j] == unchecked ((int)(0x0a))) { break; } //if(buf[j]==0x0d) break; if (buf[j] == ':') { inheader = true; break; } } if (!inheader) { i++; encrypted = false; // no passphrase break; } } i++; } if (type == ERROR) { throw new JSchException("invalid privatekey: " + identity); } int start = i; while (i < len) { if (buf[i] == unchecked ((int)(0x0a))) { bool xd = (buf[i - 1] == unchecked ((int)(0x0d))); System.Array.Copy(buf, i + 1, buf, i - (xd ? 1 : 0), len - i - 1 - (xd ? 1 : 0)); if (xd) { len--; } len--; continue; } if (buf[i] == '-') { break; } i++; } encoded_data = Util.FromBase64(buf, start, i - start); if (encoded_data.Length > 4 && encoded_data[0] == unchecked ((byte)unchecked ((int) (0x3f))) && encoded_data[1] == unchecked ((byte)unchecked ((int)(0x6f))) && encoded_data [2] == unchecked ((byte)unchecked ((int)(0xf9))) && encoded_data[3] == unchecked (( byte)unchecked ((int)(0xeb)))) { // FSecure Buffer _buf = new Buffer(encoded_data); _buf.GetInt(); // 0x3f6ff9be _buf.GetInt(); byte[] _type = _buf.GetString(); //System.err.println("type: "+new String(_type)); byte[] _cipher = _buf.GetString(); string cipherStr = Util.Byte2str(_cipher); //System.err.println("cipher: "+cipher); if (cipherStr.Equals("3des-cbc")) { _buf.GetInt(); byte[] foo = new byte[encoded_data.Length - _buf.GetOffSet()]; _buf.GetByte(foo); encoded_data = foo; encrypted = true; throw new JSchException("unknown privatekey format: " + identity); } else { if (cipherStr.Equals("none")) { _buf.GetInt(); //_buf.getInt(); encrypted = false; byte[] foo = new byte[encoded_data.Length - _buf.GetOffSet()]; _buf.GetByte(foo); encoded_data = foo; } } } if (pubkey == null) { return; } buf = pubkey; len = buf.Length; if (buf.Length > 4 && buf[0] == '-' && buf[1] == '-' && buf[2] == '-' && buf[3] == '-') { // FSecure's public key i = 0; do { i++; }while (len > i && buf[i] != unchecked ((int)(0x0a))); if (len <= i) { return; } while (i < len) { if (buf[i] == unchecked ((int)(0x0a))) { bool inheader = false; for (int j = i + 1; j < len; j++) { if (buf[j] == unchecked ((int)(0x0a))) { break; } if (buf[j] == ':') { inheader = true; break; } } if (!inheader) { i++; break; } } i++; } if (len <= i) { return; } start = i; while (i < len) { if (buf[i] == unchecked ((int)(0x0a))) { System.Array.Copy(buf, i + 1, buf, i, len - i - 1); len--; continue; } if (buf[i] == '-') { break; } i++; } publickeyblob = Util.FromBase64(buf, start, i - start); if (type == UNKNOWN && publickeyblob.Length > 8) { if (publickeyblob[8] == 'd') { type = DSS; } else { if (publickeyblob[8] == 'r') { type = RSA; } } } } else { if (buf[0] != 's' || buf[1] != 's' || buf[2] != 'h' || buf[3] != '-') { return; } i = 0; while (i < len) { if (buf[i] == ' ') { break; } i++; } i++; if (i >= len) { return; } start = i; while (i < len) { if (buf[i] == ' ' || buf[i] == '\n') { break; } i++; } publickeyblob = Util.FromBase64(buf, start, i - start); if (publickeyblob.Length < 4 + 7) { // It must start with "ssh-XXX". if (JSch.GetLogger().IsEnabled(Logger.WARN)) { JSch.GetLogger().Log(Logger.WARN, "failed to parse the public key"); } publickeyblob = null; } } } catch (Exception e) { //System.err.println("IdentityFile: "+e); if (e is JSchException) { throw (JSchException)e; } if (e is Exception) { throw new JSchException(e.ToString(), (Exception)e); } throw new JSchException(e.ToString()); } }
/* calculate common key on client side */ /* wCID = w.(A+AT) */ public static int CLIENT_KEY(sbyte[] G1, sbyte[] G2, int pin, sbyte[] R, sbyte[] X, sbyte[] wCID, sbyte[] CK) { HASH H = new HASH(); sbyte[] t = new sbyte[EFS]; FP12 g1 = FP12.fromBytes(G1); FP12 g2 = FP12.fromBytes(G2); BIG z = BIG.fromBytes(R); BIG x = BIG.fromBytes(X); ECP W = ECP.fromBytes(wCID); if (W.is_infinity()) { return(INVALID_POINT); } W = PAIR.G1mul(W, x); FP2 f = new FP2(new BIG(ROM.CURVE_Fra), new BIG(ROM.CURVE_Frb)); BIG r = new BIG(ROM.CURVE_Order); BIG q = new BIG(ROM.Modulus); BIG m = new BIG(q); m.mod(r); BIG a = new BIG(z); a.mod(m); BIG b = new BIG(z); b.div(m); g2.pinpow(pin, PBLEN); g1.mul(g2); FP4 c = g1.trace(); g2.copy(g1); g2.frob(f); FP4 cp = g2.trace(); g1.conj(); g2.mul(g1); FP4 cpm1 = g2.trace(); g2.mul(g1); FP4 cpm2 = g2.trace(); c = c.xtr_pow2(cp, cpm1, cpm2, a, b); c.geta().A.toBytes(t); H.process_array(t); c.geta().B.toBytes(t); H.process_array(t); c.getb().A.toBytes(t); H.process_array(t); c.getb().B.toBytes(t); H.process_array(t); W.X.toBytes(t); H.process_array(t); W.Y.toBytes(t); H.process_array(t); t = H.hash(); for (int i = 0; i < PAS; i++) { CK[i] = t[i]; } return(0); }
// Does a hash check and fails if the two byte buffers are not identical. public static void CompareFiles(byte[] file, ByteWriter bw) { Assert.AreEqual(HASH.SHA256(new MemoryStream(file)), HASH.SHA256(new MemoryStream(bw.ToByteArray()))); }
private HASH GenHash() { try { Type c = Sharpen.Runtime.GetType(JSch.GetConfig("md5")); hash = (HASH)(System.Activator.CreateInstance(c)); hash.Init(); } catch (Exception) { } return hash; }
internal virtual byte[] GenKey(byte[] passphrase, byte[] iv) { lock (this) { if (cipher == null) { cipher = GenCipher(); } if (hash == null) { hash = GenHash(); } byte[] key = new byte[cipher.GetBlockSize()]; int hsize = hash.GetBlockSize(); byte[] hn = new byte[key.Length / hsize * hsize + (key.Length % hsize == 0 ? 0 : hsize)]; try { byte[] tmp = null; if (vendor == VENDOR_OPENSSH) { for (int index = 0; index + hsize <= hn.Length; ) { if (tmp != null) { hash.Update(tmp, 0, tmp.Length); } hash.Update(passphrase, 0, passphrase.Length); hash.Update(iv, 0, iv.Length); tmp = hash.Digest(); System.Array.Copy(tmp, 0, hn, index, tmp.Length); index += tmp.Length; } System.Array.Copy(hn, 0, key, 0, key.Length); } else { if (vendor == VENDOR_FSECURE) { for (int index = 0; index + hsize <= hn.Length; ) { if (tmp != null) { hash.Update(tmp, 0, tmp.Length); } hash.Update(passphrase, 0, passphrase.Length); tmp = hash.Digest(); System.Array.Copy(tmp, 0, hn, index, tmp.Length); index += tmp.Length; } System.Array.Copy(hn, 0, key, 0, key.Length); } } } catch (Exception e) { System.Console.Error.WriteLine(e); } return key; } }
private IdentityFile(string name, byte[] prvkey, byte[] pubkey, JSch jsch) { this.identity = name; this.jsch = jsch; try { Type c; c = Type.GetType((string)JSch.getConfig("3des-cbc")); cipher = (Cipher)(c.newInstance()); key = new byte[cipher.getBlockSize()]; // 24 iv = new byte[cipher.getIVSize()]; // 8 c = Type.GetType((string)JSch.getConfig("md5")); hash = (HASH)(c.newInstance()); hash.init(); byte[] buf = prvkey; int len = buf.Length; int i = 0; while (i < len) { if (buf[i] == 'B' && buf[i + 1] == 'E' && buf[i + 2] == 'G' && buf[i + 3] == 'I') { i += 6; if (buf[i] == 'D' && buf[i + 1] == 'S' && buf[i + 2] == 'A') { type = DSS; } else if (buf[i] == 'R' && buf[i + 1] == 'S' && buf[i + 2] == 'A') { type = RSA; } else if (buf[i] == 'S' && buf[i + 1] == 'S' && buf[i + 2] == 'H') { // FSecure type = UNKNOWN; keytype = FSECURE; } else { //Console.Error.WriteLine("invalid format: "+identity); throw new JSchException("invalid privatekey: " + identity); } i += 3; continue; } if (buf[i] == 'A' && buf[i + 1] == 'E' && buf[i + 2] == 'S' && buf[i + 3] == '-' && buf[i + 4] == '2' && buf[i + 5] == '5' && buf[i + 6] == '6' && buf[i + 7] == '-') { i += 8; if (Session.checkCipher((string)JSch.getConfig("aes256-cbc"))) { c = Type.GetType((string)JSch.getConfig("aes256-cbc")); cipher = (Cipher)(c.newInstance()); key = new byte[cipher.getBlockSize()]; iv = new byte[cipher.getIVSize()]; } else { throw new JSchException("privatekey: aes256-cbc is not available " + identity); } continue; } if (buf[i] == 'C' && buf[i + 1] == 'B' && buf[i + 2] == 'C' && buf[i + 3] == ',') { i += 4; for (int ii = 0; ii < iv.Length; ii++) { iv[ii] = (byte)(((a2b(buf[i++]) << 4) & 0xf0) + (a2b(buf[i++]) & 0xf)); } continue; } if (buf[i] == 0x0d && i + 1 < buf.Length && buf[i + 1] == 0x0a) { i++; continue; } if (buf[i] == 0x0a && i + 1 < buf.Length) { if (buf[i + 1] == 0x0a) { i += 2; break; } if (buf[i + 1] == 0x0d && i + 2 < buf.Length && buf[i + 2] == 0x0a) { i += 3; break; } bool inheader = false; for (int j = i + 1; j < buf.Length; j++) { if (buf[j] == 0x0a) { break; } //if(buf[j]==0x0d) break; if (buf[j] == ':') { inheader = true; break; } } if (!inheader) { i++; encrypted = false; // no passphrase break; } } i++; } if (type == ERROR) { throw new JSchException("invalid privatekey: " + identity); } int start = i; while (i < len) { if (buf[i] == 0x0a) { bool xd = (buf[i - 1] == 0x0d); Array.Copy(buf, i + 1, buf, i - (xd ? 1 : 0), len - i - 1 - (xd ? 1 : 0) ); if (xd) { len--; } len--; continue; } if (buf[i] == '-') { break; } i++; } encoded_data = Util.fromBase64(buf, start, i - start); if (encoded_data.Length > 4 && // FSecure encoded_data[0] == (byte)0x3f && encoded_data[1] == (byte)0x6f && encoded_data[2] == (byte)0xf9 && encoded_data[3] == (byte)0xeb) { Buffer _buf = new Buffer(encoded_data); _buf.getInt(); // 0x3f6ff9be _buf.getInt(); byte[] _type = _buf.getString(); //Console.Error.WriteLine("type: "+Encoding.UTF8.GetString(_type)); byte[] _cipher = _buf.getString(); string scipher = Encoding.UTF8.GetString(_cipher); //Console.Error.WriteLine("cipher: "+cipher); if (scipher.Equals("3des-cbc")) { _buf.getInt(); byte[] foo = new byte[encoded_data.Length - _buf.getOffSet()]; _buf.getByte(foo); encoded_data = foo; encrypted = true; throw new JSchException("unknown privatekey format: " + identity); } else if (scipher.Equals("none")) { _buf.getInt(); //_buf.getInt(); encrypted = false; byte[] foo = new byte[encoded_data.Length - _buf.getOffSet()]; _buf.getByte(foo); encoded_data = foo; } } if (pubkey == null) { return; } buf = pubkey; len = buf.Length; if (buf.Length > 4 && // FSecure's public key buf[0] == '-' && buf[1] == '-' && buf[2] == '-' && buf[3] == '-') { i = 0; do { i++; } while (len > i && buf[i] != 0x0a); if (len <= i) { return; } while (i < len) { if (buf[i] == 0x0a) { bool inheader = false; for (int j = i + 1; j < len; j++) { if (buf[j] == 0x0a) { break; } if (buf[j] == ':') { inheader = true; break; } } if (!inheader) { i++; break; } } i++; } if (len <= i) { return; } start = i; while (i < len) { if (buf[i] == 0x0a) { Array.Copy(buf, i + 1, buf, i, len - i - 1); len--; continue; } if (buf[i] == '-') { break; } i++; } publickeyblob = Util.fromBase64(buf, start, i - start); if (type == UNKNOWN && publickeyblob.Length > 8) { if (publickeyblob[8] == 'd') { type = DSS; } else if (publickeyblob[8] == 'r') { type = RSA; } } } else { if (buf[0] != 's' || buf[1] != 's' || buf[2] != 'h' || buf[3] != '-') { return; } i = 0; while (i < len) { if (buf[i] == ' ') { break; } i++; } i++; if (i >= len) { return; } start = i; while (i < len) { if (buf[i] == ' ' || buf[i] == '\n') { break; } i++; } publickeyblob = Util.fromBase64(buf, start, i - start); if (publickeyblob.Length < 4 + 7) { // It must start with "ssh-XXX". if (JSch.getLogger().isEnabled(Logger.WARN)) { JSch.getLogger().log(Logger.WARN, "failed to parse the public key"); } publickeyblob = null; } } } catch (Exception e) { //Console.Error.WriteLine("IdentityFile: "+e); if (e is JSchException) { throw (JSchException)e; } throw new JSchException(e.Message, e); } }