📄 keypair.cs
字号:
#region Licences
// Copyright (C) 2005 Sebastian Faltoni <sebastian@dotnetfireball.net>
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#endregion Licences
using System;
using System.IO;
using System.Runtime.CompilerServices;
namespace Fireball.Ssh.jsch
{
/* -*-mode:java; c-basic-offset:2; -*- */
/*
Copyright (c) 2002,2003,2004 ymnk, JCraft,Inc. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the distribution.
3. The names of the authors may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
public abstract class KeyPair
{
public const int ERROR=0;
public const int DSA=1;
public const int RSA=2;
public const int UNKNOWN=3;
internal const int VENDOR_OPENSSH=0;
internal const int VENDOR_FSECURE=1;
internal int vendor=VENDOR_OPENSSH;
private static byte[] cr=Util.getBytes("\n");
public static KeyPair genKeyPair(JSch jsch, int type)
{
return genKeyPair(jsch, type, 1024);
}
public static KeyPair genKeyPair(JSch jsch, int type, int key_size)
{
KeyPair kpair=null;
if(type==DSA){ kpair=new KeyPairDSA(jsch); }
else if(type==RSA){ kpair=new KeyPairRSA(jsch); }
if(kpair!=null)
{
kpair.generate(key_size);
}
return kpair;
}
internal abstract void generate(int key_size);
internal abstract byte[] getBegin();
internal abstract byte[] getEnd();
public abstract int getKeySize();
internal JSch jsch=null;
private Cipher cipher;
private HASH hash;
private Random random;
private byte[] passphrase;
public KeyPair(JSch jsch)
{
this.jsch=jsch;
}
static byte[][] header={Util.getBytes( "Proc-Type: 4,ENCRYPTED"),
Util.getBytes("DEK-Info: DES-EDE3-CBC,")};
internal abstract byte[] getPrivateKey();
private void Write(Stream s, byte[] arr)
{
s.Write(arr, 0, arr.Length);
}
public void writePrivateKey(Stream outs)
{
byte[] plain=getPrivateKey();
byte[][] _iv=new byte[1][];
byte[] encoded=encrypt(plain, _iv);
byte[] iv=_iv[0];
byte[] prv=Util.toBase64(encoded, 0, encoded.Length);
try
{
Write(outs, getBegin()); Write(outs,cr);
if(passphrase!=null)
{
Write(outs, header[0]); Write(outs,cr);
Write(outs, header[1]);
for(int j=0; j<iv.Length; j++)
{
outs.WriteByte(b2a((byte)((iv[j]>>4)&0x0f)));
outs.WriteByte(b2a((byte)(iv[j]&0x0f)));
}
Write(outs,cr);
Write(outs,cr);
}
int i=0;
while(i<prv.Length)
{
if(i+64<prv.Length)
{
outs.Write(prv, i, 64);
Write(outs,cr);
i+=64;
continue;
}
outs.Write(prv, i, prv.Length-i);
Write(outs,cr);
break;
}
Write(outs, getEnd()); Write(outs,cr);
//outs.close();
}
catch(Exception e)
{
Console.WriteLine(e);
}
}
private static byte[] space=Util.getBytes(" ");
internal abstract byte[] getKeyTypeName();
public abstract int getKeyType();
public virtual byte[] getPublicKeyBlob(){ return publickeyblob; }
public void writePublicKey(Stream outs, String comment)
{
byte[] pubblob=getPublicKeyBlob();
byte[] pub=Util.toBase64(pubblob, 0, pubblob.Length);
try
{
Write(outs, getKeyTypeName()); Write(outs, space);
outs.Write(pub, 0, pub.Length); Write(outs, space);
Write(outs, Util.getBytes( comment));
Write(outs,cr);
}
catch(Exception e)
{
Console.WriteLine(e);
}
}
public void writePublicKey(String name, String comment)
{
FileStream fos=new FileStream(name,FileMode.OpenOrCreate);
writePublicKey(fos, comment);
fos.Close();
}
public void writeSECSHPublicKey(Stream outs, String comment)
{
byte[] pubblob=getPublicKeyBlob();
byte[] pub=Util.toBase64(pubblob, 0, pubblob.Length);
try
{
Write(outs, Util.getBytes( "---- BEGIN SSH2 PUBLIC KEY ----")); Write(outs, cr);
Write(outs, Util.getBytes("Comment: \""+comment+"\"")); Write(outs,cr);
int index=0;
while(index<pub.Length)
{
int len=70;
if((pub.Length-index)<len)len=pub.Length-index;
outs.Write(pub, index, len); Write(outs, cr);
index+=len;
}
Write(outs, Util.getBytes("---- END SSH2 PUBLIC KEY ----")); Write(outs,cr);
}
catch(Exception e)
{
Console.WriteLine(e);
}
}
public void writeSECSHPublicKey(String name, String comment)
{
FileStream fos=new FileStream(name, FileMode.OpenOrCreate);
writeSECSHPublicKey(fos, comment);
fos.Close();
}
public void writePrivateKey(String name)
{
FileStream fos=new FileStream(name, FileMode.OpenOrCreate);
writePrivateKey(fos);
fos.Close();
}
public String getFingerPrint()
{
if(hash==null) hash=genHash();
byte[] kblob=getPublicKeyBlob();
if(kblob==null) return null;
return getKeySize()+" "+Util.getFingerPrint(hash, kblob);
}
private byte[] encrypt(byte[] plain, byte[][] _iv)
{
if(passphrase==null) return plain;
if(cipher==null) cipher=genCipher();
byte[] iv=_iv[0]=new byte[cipher.getIVSize()];
if(random==null) random=genRandom();
random.fill(iv, 0, iv.Length);
byte[] key=genKey(passphrase, iv);
byte[] encoded=plain;
int bsize=cipher.getBlockSize();
if(encoded.Length%bsize!=0)
{
byte[] foo=new byte[(encoded.Length/bsize+1)*bsize];
Array.Copy(encoded, 0, foo, 0, encoded.Length);
encoded=foo;
}
try
{
cipher.init(Cipher.ENCRYPT_MODE, key, iv);
cipher.update(encoded, 0, encoded.Length, encoded, 0);
}
catch(Exception e)
{
Console.WriteLine(e);
}
return encoded;
}
internal abstract bool parse(byte[] data);
private byte[] decrypt(byte[] data, byte[] passphrase, byte[] iv)
{
/*
if(iv==null){ // FSecure
iv=new byte[8];
for(int i=0; i<iv.Length; i++)iv[i]=0;
}
*/
try
{
byte[] key=genKey(passphrase, iv);
cipher.init(Cipher.DECRYPT_MODE, key, iv);
byte[] plain=new byte[data.Length];
cipher.update(data, 0, data.Length, plain, 0);
return plain;
}
catch(Exception e)
{
Console.WriteLine(e);
}
return null;
}
internal int writeSEQUENCE(byte[] buf, int index, int len)
{
buf[index++]=0x30;
index=writeLength(buf, index, len);
return index;
}
internal int writeINTEGER(byte[] buf, int index, byte[] data)
{
buf[index++]=0x02;
index=writeLength(buf, index, data.Length);
Array.Copy(data, 0, buf, index, data.Length);
index+=data.Length;
return index;
}
internal int countLength(int len)
{
int i=1;
if(len<=0x7f) return i;
while(len>0)
{
len>>=8;
i++;
}
return i;
}
internal int writeLength(byte[] data, int index, int len)
{
int i=countLength(len)-1;
if(i==0)
{
data[index++]=(byte)len;
return index;
}
data[index++]=(byte)(0x80|i);
int j=index+i;
while(i>0)
{
data[index+i-1]=(byte)(len&0xff);
len>>=8;
i--;
}
return j;
}
private Random genRandom()
{
if(random==null)
{
try
{
Type t=Type.GetType(jsch.getConfig("random"));
random=(Random)Activator.CreateInstance(t);
}
catch(Exception e){ Console.Error.WriteLine("connect: random "+e); }
}
return random;
}
private HASH genHash()
{
try
{
Type t=Type.GetType(jsch.getConfig("md5"));
hash=(HASH)Activator.CreateInstance(t);
hash.init();
}
catch//(Exception e)
{
}
return hash;
}
private Cipher genCipher()
{
try
{
Type t;
t=Type.GetType(jsch.getConfig("3des-cbc"));
cipher=(Cipher)(Activator.CreateInstance(t));
}
catch//(Exception e)
{
}
return cipher;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -