SSH1UserAuthKey.cs :  » Network-Clients » Granados » Routrek » SSHCV1 » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Network Clients » Granados 
Granados » Routrek » SSHCV1 » SSH1UserAuthKey.cs
/* ---------------------------------------------------------------------------
 *
 * Copyright (c) Routrek Networks, Inc.    All Rights Reserved..
 * 
 * This file is a part of the Granados SSH Client Library that is subject to
 * the license included in the distributed package.
 * You may not use this file except in compliance with the license.
 * 
 * ---------------------------------------------------------------------------
 */
using System;
using System.IO;
using System.Text;
using System.Security.Cryptography;
using System.Diagnostics;

using Routrek.SSHC;

namespace Routrek.SSHCV1{
  /// <summary>
  /// private key for user authentication
  /// </summary>
  public class SSH1UserAuthKey
  {
    private BigInteger _modulus;
    private BigInteger _publicExponent;
    private BigInteger _privateExponent;
    private BigInteger _primeP;
    private BigInteger _primeQ;
    private BigInteger _crtCoefficient;

    public BigInteger PublicModulus {
      get {
        return _modulus;
      }
    }
    public BigInteger PublicExponent {
      get {
        return _publicExponent;
      }
    }

    /// <summary>
    /// constructs from file
    /// </summary>
    /// <param name="path">file name</param>
    /// <param name="passphrase">passphrase or empty string if passphrase is not required</param>
    public SSH1UserAuthKey(string path, string passphrase)
    {
      Stream s = File.Open(path, FileMode.Open);
      byte[] header = new byte[32];
      s.Read(header, 0, header.Length);
      if(Encoding.ASCII.GetString(header)!="SSH PRIVATE KEY FILE FORMAT 1.1\n")
        throw new SSHException(String.Format(Strings.GetString("BrokenKeyFile"), path));

      SSH1DataReader reader = new SSH1DataReader(ReadAll(s));
      s.Close();

      byte[] cipher = reader.Read(2); //first 2 bytes indicates algorithm and next 8 bytes is space
      reader.Read(8);

      _modulus = reader.ReadMPInt();
      _publicExponent = reader.ReadMPInt();
      byte[] comment = reader.ReadString();
      byte[] prvt = reader.ReadAll();
      //Kv
      CipherAlgorithm algo = (CipherAlgorithm)cipher[1];
      if(algo!=0) {
        Cipher c = CipherFactory.CreateCipher(SSHProtocol.SSH1, algo, ConvertToKey(passphrase));
        byte[] buf = new byte[prvt.Length];
        c.Decrypt(prvt, 0, prvt.Length, buf, 0);
        prvt = buf;
      }

      SSH1DataReader prvtreader = new SSH1DataReader(prvt);
      byte[] mark = prvtreader.Read(4);
      if(mark[0]!=mark[2] || mark[1]!=mark[3])
        throw new SSHException(Strings.GetString("WrongPassphrase"));

      _privateExponent = prvtreader.ReadMPInt();
      _crtCoefficient = prvtreader.ReadMPInt();
      _primeP = prvtreader.ReadMPInt();
      _primeQ = prvtreader.ReadMPInt();
    }

    public BigInteger decryptChallenge(BigInteger encryptedchallenge) {
      BigInteger primeExponentP = PrimeExponent(_privateExponent, _primeP);
      BigInteger primeExponentQ = PrimeExponent(_privateExponent, _primeQ);

      BigInteger p2;
      BigInteger q2;
      BigInteger k;
      BigInteger result;

      p2 = encryptedchallenge % _primeP;
      p2 = p2.modPow(primeExponentP, _primeP);

      q2 = encryptedchallenge % _primeQ;
      q2 = q2.modPow(primeExponentQ, _primeQ);

      if(p2==q2) return p2;

      k = (q2 - p2) % _primeQ;
      if(k.IsNegative) k += _primeQ;
      k = k * _crtCoefficient;
      k = k % _primeQ;

      result = k * _primeP;
      result = result + p2;
      
      return result;
    }

    private static BigInteger PrimeExponent(BigInteger privateExponent,  BigInteger prime) {
      BigInteger pe = prime - new BigInteger(1);
      return privateExponent % pe;

    }

    private static byte[] ReadAll(Stream s) {
      byte[] t = new byte[0x1000];
      int l = s.Read(t, 0, 0x1000);
      if(l==t.Length) throw new IOException("Key file is too big");
      return t;
    }

    //key from passphrase
    private static byte[] ConvertToKey(string passphrase) {
      byte[] t = Encoding.ASCII.GetBytes(passphrase);
      byte[] md5 = new MD5CryptoServiceProvider().ComputeHash(t);
      byte[] result = new byte[32];
      Array.Copy(md5, 0, result, 0, 16);
      Array.Copy(md5, 0, result, 16, 16);
      return result;
    }
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.