A
download KeyPairRSA.cs
Language: C#
Copyright: (c) 2002,2003,2004 ymnk, JCraft,Inc. All rights reserved.
LOC: 228
Project Info
labmanager - A C# application for administ...bs...(labmanager)
Server: Google
Type: svn
...er\trunk\lib\SharpSSH\jsch\
   Buffer.cs
   Channel.cs
   Channel.old.cs
   ChannelDirectTCPIP.cs
   ChannelExec.cs
   ChannelForwardedTCPIP.cs
   ChannelSession.cs
   ChannelSftp.cs
   ChannelSftpOld.cs
   ChannelSftpStreamGet.cs
   ChannelSftpStreamPut.cs
   ChannelShell.cs
   ChannelSubsystem.cs
   ChannelX11.cs
   Cipher.cs
   Compression.cs
   DH.cs
   DHG1.cs
   DHGEX.cs
   ForwardedTCPIPDaemon.cs
   HASH.cs
   HostKey.cs
   HostKeyRepository.cs
   Identity.cs
   IdentityFile.cs
   IO.cs
   JSch.cs
   JSchAuthCancelException.cs
   JSchException.cs
   ...PartialAuthException.cs
   KeyExchange.cs
   KeyPair.cs
   KeyPairDSA.cs
   KeyPairGenDSA.cs
   KeyPairGenRSA.cs
   KeyPairRSA.cs
   KnownHosts.cs
   MAC.cs
   Packet.cs
   PortWatcher.cs
   Proxy.cs
   Random.cs
   Request.cs
   RequestExec.cs
   RequestPtyReq.cs
   RequestSftp.cs
   RequestShell.cs
   RequestSignal.cs
   RequestSubsystem.cs
   RequestWindowChange.cs
   RequestX11.cs
   ServerSocketFactory.cs
   Session.cs
   Session.old.cs
   SftpATTRS.cs
   SftpException.cs
   SftpProgressMonitor.cs
   SignatureDSA.cs
   SignatureRSA.cs
   SocketFactory.cs
   UIKeyboardInteractive.cs
   UserAuth.cs
   ...hKeyboardInteractive.cs
   UserAuthNone.cs
   UserAuthPassword.cs
   UserAuthPublicKey.cs
   UserInfo.cs
   Util.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
using System;

namespace Tamir.SharpSsh.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 class KeyPairRSA : KeyPair
	{
		private byte[] prv_array;
		private byte[] pub_array;
		private byte[] n_array;

		private byte[] p_array;  // prime p
		private byte[] q_array;  // prime q
		private byte[] ep_array; // prime exponent p
		private byte[] eq_array; // prime exponent q
		private byte[] c_array;  // coefficient

		//private int key_size=0;
		private int key_size=1024;

		public KeyPairRSA(JSch jsch):base(jsch)
		{
		}

		internal override void generate(int key_size)
		{
			this.key_size=key_size;
			try
			{
				Type t=Type.GetType(jsch.getConfig("keypairgen.rsa"));
				KeyPairGenRSA keypairgen=(KeyPairGenRSA)(Activator.CreateInstance(t));
				keypairgen.init(key_size);
				pub_array=keypairgen.getE();
				prv_array=keypairgen.getD();
				n_array=keypairgen.getN();

				p_array=keypairgen.getP();
				q_array=keypairgen.getQ();
				ep_array=keypairgen.getEP();
				eq_array=keypairgen.getEQ();
				c_array=keypairgen.getC();

				keypairgen=null;
			}
			catch(Exception e)
			{
				Console.WriteLine("KeyPairRSA: "+e); 
				throw new JSchException(e.ToString());
			}
		}

		private static  byte[] begin= Util.getBytes( "-----BEGIN RSA PRIVATE KEY-----");
		private static  byte[] end=Util.getBytes("-----END RSA PRIVATE KEY-----");

		internal override byte[] getBegin(){ return begin; }
		internal override byte[] getEnd(){ return end; }

		internal override byte[] getPrivateKey()
		{
			int content=
				1+countLength(1) + 1 +                           // INTEGER
				1+countLength(n_array.Length) + n_array.Length + // INTEGER  N
				1+countLength(pub_array.Length) + pub_array.Length + // INTEGER  pub
				1+countLength(prv_array.Length) + prv_array.Length+  // INTEGER  prv
				1+countLength(p_array.Length) + p_array.Length+      // INTEGER  p
				1+countLength(q_array.Length) + q_array.Length+      // INTEGER  q
				1+countLength(ep_array.Length) + ep_array.Length+    // INTEGER  ep
				1+countLength(eq_array.Length) + eq_array.Length+    // INTEGER  eq
				1+countLength(c_array.Length) + c_array.Length;      // INTEGER  c

			int total=
				1+countLength(content)+content;   // SEQUENCE

			byte[] plain=new byte[total];
			int index=0;
			index=writeSEQUENCE(plain, index, content);
			index=writeINTEGER(plain, index, new byte[1]);  // 0
			index=writeINTEGER(plain, index, n_array);
			index=writeINTEGER(plain, index, pub_array);
			index=writeINTEGER(plain, index, prv_array);
			index=writeINTEGER(plain, index, p_array);
			index=writeINTEGER(plain, index, q_array);
			index=writeINTEGER(plain, index, ep_array);
			index=writeINTEGER(plain, index, eq_array);
			index=writeINTEGER(plain, index, c_array);
			return plain;
		}

		internal override bool parse(byte [] plain)
		{
			/*
			byte[] p_array;
			byte[] q_array;
			byte[] dmp1_array;
			byte[] dmq1_array;
			byte[] iqmp_array;
			*/
			try
			{
				int index=0;
				int Length=0;

				if(vendor==VENDOR_FSECURE)
				{
					if(plain[index]!=0x30)
					{                  // FSecure
						Buffer buf=new Buffer(plain);
						pub_array=buf.getMPIntBits();
						prv_array=buf.getMPIntBits();
						n_array=buf.getMPIntBits();
						byte[] u_array=buf.getMPIntBits();
						p_array=buf.getMPIntBits();
						q_array=buf.getMPIntBits();
						return true;
					}
					return false;
				}

				index++; // SEQUENCE
				Length=plain[index++]&0xff;
				if((Length&0x80)!=0)
				{
					int foo=Length&0x7f; Length=0;
					while(foo-->0){ Length=(Length<<8)+(plain[index++]&0xff); }
				}

				if(plain[index]!=0x02)return false;
				index++; // INTEGER
				Length=plain[index++]&0xff;
				if((Length&0x80)!=0)
				{
					int foo=Length&0x7f; Length=0;
					while(foo-->0){ Length=(Length<<8)+(plain[index++]&0xff); }
				}
				index+=Length;

				//System.out.println("int: len="+Length);
				//System.out.print(Integer.toHexString(plain[index-1]&0xff)+":");
				//System.out.println("");

				index++;
				Length=plain[index++]&0xff;
				if((Length&0x80)!=0)
				{
					int foo=Length&0x7f; Length=0;
					while(foo-->0){ Length=(Length<<8)+(plain[index++]&0xff); }
				}
				n_array=new byte[Length];
				Array.Copy(plain, index, n_array, 0, Length);
				index+=Length;
				/*
				System.out.println("int: N len="+Length);
				for(int i=0; i<n_array.Length; i++){
				System.out.print(Integer.toHexString(n_array[i]&0xff)+":");
				}
				System.out.println("");
				*/
				index++;
				Length=plain[index++]&0xff;
				if((Length&0x80)!=0)
				{
					int foo=Length&0x7f; Length=0;
					while(foo-->0){ Length=(Length<<8)+(plain[index++]&0xff); }
				}
				pub_array=new byte[Length];
				Array.Copy(plain, index, pub_array, 0, Length);
				index+=Length;
				/*
				System.out.println("int: E len="+Length);
				for(int i=0; i<pub_array.Length; i++){
				System.out.print(Integer.toHexString(pub_array[i]&0xff)+":");
				}
				System.out.println("");
				*/
				index++;
				Length=plain[index++]&0xff;
				if((Length&0x80)!=0)
				{
					int foo=Length&0x7f; Length=0;
					while(foo-->0){ Length=(Length<<8)+(plain[index++]&0xff); }
				}
				prv_array=new byte[Length];
				Array.Copy(plain, index, prv_array, 0, Length);
				index+=Length;
				/*
				System.out.println("int: prv len="+Length);
				for(int i=0; i<prv_array.Length; i++){
				System.out.print(Integer.toHexString(prv_array[i]&0xff)+":");
				}
				System.out.println("");
				*/

				index++;
				Length=plain[index++]&0xff;
				if((Length&0x80)!=0)
				{
					int foo=Length&0x7f; Length=0;
					while(foo-->0){ Length=(Length<<8)+(plain[index++]&0xff); }
				}
				p_array=new byte[Length];
				Array.Copy(plain, index, p_array, 0, Length);
				index+=Length;
				/*
				System.out.println("int: P len="+Length);
				for(int i=0; i<p_array.Length; i++){
				System.out.print(Integer.toHexString(p_array[i]&0xff)+":");
				}
				System.out.println("");
				*/
				index++;
				Length=plain[index++]&0xff;
				if((Length&0x80)!=0)
				{
					int foo=Length&0x7f; Length=0;
					while(foo-->0){ Length=(Length<<8)+(plain[index++]&0xff); }
				}
				q_array=new byte[Length];
				Array.Copy(plain, index, q_array, 0, Length);
				index+=Length;
				/*
				System.out.println("int: q len="+Length);
				for(int i=0; i<q_array.Length; i++){
				System.out.print(Integer.toHexString(q_array[i]&0xff)+":");
				}
				System.out.println("");
				*/
				index++;
				Length=plain[index++]&0xff;
				if((Length&0x80)!=0)
				{
					int foo=Length&0x7f; Length=0;
					while(foo-->0){ Length=(Length<<8)+(plain[index++]&0xff); }
				}
				ep_array=new byte[Length];
				Array.Copy(plain, index, ep_array, 0, Length);
				index+=Length;
				/*
				System.out.println("int: ep len="+Length);
				for(int i=0; i<ep_array.Length; i++){
				System.out.print(Integer.toHexString(ep_array[i]&0xff)+":");
				}
				System.out.println("");
				*/
				index++;
				Length=plain[index++]&0xff;
				if((Length&0x80)!=0)
				{
					int foo=Length&0x7f; Length=0;
					while(foo-->0){ Length=(Length<<8)+(plain[index++]&0xff); }
				}
				eq_array=new byte[Length];
				Array.Copy(plain, index, eq_array, 0, Length);
				index+=Length;
				/*
				System.out.println("int: eq len="+Length);
				for(int i=0; i<eq_array.Length; i++){
				System.out.print(Integer.toHexString(eq_array[i]&0xff)+":");
				}
				System.out.println("");
				*/
				index++;
				Length=plain[index++]&0xff;
				if((Length&0x80)!=0)
				{
					int foo=Length&0x7f; Length=0;
					while(foo-->0){ Length=(Length<<8)+(plain[index++]&0xff); }
				}
				c_array=new byte[Length];
				Array.Copy(plain, index, c_array, 0, Length);
				index+=Length;
				/*
				System.out.println("int: c len="+Length);
				for(int i=0; i<c_array.Length; i++){
				System.out.print(Integer.toHexString(c_array[i]&0xff)+":");
				}
				System.out.println("");
				*/
			}
			catch//(Exception e)
			{
				//System.out.println(e);
				return false;
			}
			return true;
		}


		public override byte[] getPublicKeyBlob()
		{
			byte[] foo=base.getPublicKeyBlob();
			if(foo!=null) return foo;

			if(pub_array==null) return null;

			Buffer buf=new Buffer(sshrsa.Length+4+
				pub_array.Length+4+ 
				n_array.Length+4);
			buf.putString(sshrsa);
			buf.putString(pub_array);
			buf.putString(n_array);
			return buf.buffer;
		}

		private static byte[] sshrsa= Util.getBytes( "ssh-rsa" );
		internal override byte[] getKeyTypeName(){return sshrsa;}
		public override int getKeyType(){return RSA;}

		public override int getKeySize(){return key_size; }
		public override void dispose()
		{
			base.dispose();
			pub_array=null;
			prv_array=null;
			n_array=null;

			p_array=null;
			q_array=null;
			ep_array=null;
			eq_array=null;
			c_array=null;
		}
	}

}

About Koders | Resources | Downloads | Support | Black Duck | Terms of Service | DMCA | Privacy Policy | Contact Us