PageRenderTime 56ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/SharpSSH/jsch/KeyPair.cs

https://bitbucket.org/bluetoque/bluetoque-pub
C# | 803 lines | 671 code | 86 blank | 46 comment | 199 complexity | 139187b536964b4df6c4928c9518f5a4 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause
  1. using System;
  2. using System.IO;
  3. using System.Runtime.CompilerServices;
  4. using SharpSsh.Jsch;
  5. using SharpSsh;
  6. namespace SharpSsh.jsch
  7. {
  8. /*
  9. Copyright (c) 2002,2003,2004 ymnk, JCraft,Inc. All rights reserved.
  10. Redistribution and use in source and binary forms, with or without
  11. modification, are permitted provided that the following conditions are met:
  12. 1. Redistributions of source code must retain the above copyright notice,
  13. this list of conditions and the following disclaimer.
  14. 2. Redistributions in binary form must reproduce the above copyright
  15. notice, this list of conditions and the following disclaimer in
  16. the documentation and/or other materials provided with the distribution.
  17. 3. The names of the authors may not be used to endorse or promote products
  18. derived from this software without specific prior written permission.
  19. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  20. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  21. FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
  22. INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
  23. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  25. OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  26. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  27. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  28. EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. public abstract class KeyPair
  31. {
  32. public const int ERROR = 0;
  33. public const int DSA = 1;
  34. public const int RSA = 2;
  35. public const int UNKNOWN = 3;
  36. internal const int VENDOR_OPENSSH = 0;
  37. internal const int VENDOR_FSECURE = 1;
  38. internal int vendor = VENDOR_OPENSSH;
  39. private static byte[] m_cr = Util.GetBytes("\n");
  40. private static byte[] m_space = Util.GetBytes(" ");
  41. private bool m_encrypted = false;
  42. private byte[] m_data = null;
  43. private byte[] m_iv = null;
  44. private byte[] m_publickeyblob = null;
  45. internal JSch m_jsch = null;
  46. private Cipher m_cipher;
  47. private HashAlgorithm m_hash;
  48. private IRandom m_random;
  49. private byte[] m_passphrase;
  50. static byte[][] m_header ={Util.GetBytes( "Proc-Type: 4,ENCRYPTED"),
  51. Util.GetBytes("DEK-Info: DES-EDE3-CBC,")};
  52. public static KeyPair GenKeyPair(JSch jsch, int type)
  53. {
  54. return GenKeyPair(jsch, type, 1024);
  55. }
  56. public static KeyPair GenKeyPair(JSch jsch, int type, int key_size)
  57. {
  58. KeyPair kpair = null;
  59. if (type == DSA) { kpair = new KeyPairDSA(jsch); }
  60. else if (type == RSA) { kpair = new KeyPairRSA(jsch); }
  61. if (kpair != null)
  62. {
  63. kpair.Generate(key_size);
  64. }
  65. return kpair;
  66. }
  67. internal abstract void Generate(int key_size);
  68. internal abstract byte[] Begin { get; }
  69. internal abstract byte[] End { get; }
  70. public abstract int KeySize { get; }
  71. public KeyPair(JSch jsch)
  72. {
  73. this.m_jsch = jsch;
  74. }
  75. internal abstract byte[] PrivateKey { get; }
  76. private void Write(Stream s, byte[] arr)
  77. {
  78. s.Write(arr, 0, arr.Length);
  79. }
  80. public void WritePrivateKey(Stream outs)
  81. {
  82. byte[] plain = PrivateKey;
  83. byte[][] _iv = new byte[1][];
  84. byte[] encoded = Encrypt(plain, _iv);
  85. byte[] iv = _iv[0];
  86. byte[] prv = Util.ToBase64(encoded, 0, encoded.Length);
  87. try
  88. {
  89. Write(outs, Begin);
  90. Write(outs, m_cr);
  91. if (m_passphrase != null)
  92. {
  93. Write(outs, m_header[0]); Write(outs, m_cr);
  94. Write(outs, m_header[1]);
  95. for (int j = 0; j < iv.Length; j++)
  96. {
  97. outs.WriteByte(b2a((byte)((iv[j] >> 4) & 0x0f)));
  98. outs.WriteByte(b2a((byte)(iv[j] & 0x0f)));
  99. }
  100. Write(outs, m_cr);
  101. Write(outs, m_cr);
  102. }
  103. int i = 0;
  104. while (i < prv.Length)
  105. {
  106. if (i + 64 < prv.Length)
  107. {
  108. outs.Write(prv, i, 64);
  109. Write(outs, m_cr);
  110. i += 64;
  111. continue;
  112. }
  113. outs.Write(prv, i, prv.Length - i);
  114. Write(outs, m_cr);
  115. break;
  116. }
  117. Write(outs, End); Write(outs, m_cr);
  118. //outs.close();
  119. }
  120. catch (Exception e)
  121. {
  122. Trace.TraceError("Error:\r\n{0}", e.ToString());
  123. }
  124. }
  125. internal abstract byte[] KeyTypeName { get; }
  126. public abstract int KeyType { get; }
  127. public virtual byte[] PublicKeyBlob { get { return m_publickeyblob; } }
  128. public void WritePublicKey(Stream outs, string comment)
  129. {
  130. byte[] pubblob = PublicKeyBlob;
  131. byte[] pub = Util.ToBase64(pubblob, 0, pubblob.Length);
  132. try
  133. {
  134. Write(outs, KeyTypeName);
  135. Write(outs, m_space);
  136. outs.Write(pub, 0, pub.Length);
  137. Write(outs, m_space);
  138. Write(outs, Util.GetBytes(comment));
  139. Write(outs, m_cr);
  140. }
  141. catch (Exception e)
  142. {
  143. Trace.TraceError("Error:\r\n{0}", e.ToString());
  144. }
  145. }
  146. public void WritePublicKey(string name, string comment)
  147. {
  148. FileStream fos = new FileStream(name, FileMode.OpenOrCreate);
  149. WritePublicKey(fos, comment);
  150. fos.Close();
  151. }
  152. public void WriteSECSHPublicKey(Stream outs, string comment)
  153. {
  154. byte[] pubblob = PublicKeyBlob;
  155. byte[] pub = Util.ToBase64(pubblob, 0, pubblob.Length);
  156. try
  157. {
  158. Write(outs, Util.GetBytes("---- BEGIN SSH2 PUBLIC KEY ----"));
  159. Write(outs, m_cr);
  160. Write(outs, Util.GetBytes("Comment: \"" + comment + "\""));
  161. Write(outs, m_cr);
  162. int index = 0;
  163. while (index < pub.Length)
  164. {
  165. int len = 70;
  166. if ((pub.Length - index) < len) len = pub.Length - index;
  167. outs.Write(pub, index, len); Write(outs, m_cr);
  168. index += len;
  169. }
  170. Write(outs, Util.GetBytes("---- END SSH2 PUBLIC KEY ----"));
  171. Write(outs, m_cr);
  172. }
  173. catch (Exception e)
  174. {
  175. Trace.TraceError("Error:\r\n{0}", e.ToString());
  176. }
  177. }
  178. public void writeSECSHPublicKey(string name, string comment)
  179. {
  180. FileStream fos = new FileStream(name, FileMode.OpenOrCreate);
  181. WriteSECSHPublicKey(fos, comment);
  182. fos.Close();
  183. }
  184. public void WritePrivateKey(string name)
  185. {
  186. FileStream fos = new FileStream(name, FileMode.OpenOrCreate);
  187. WritePrivateKey(fos);
  188. fos.Close();
  189. }
  190. public string GetFingerPrint()
  191. {
  192. if (m_hash == null) m_hash = GenHash();
  193. byte[] kblob = PublicKeyBlob;
  194. if (kblob == null) return null;
  195. return KeySize + " " + Util.GetFingerPrint(m_hash, kblob);
  196. }
  197. private byte[] Encrypt(byte[] plain, byte[][] _iv)
  198. {
  199. if (m_passphrase == null) return plain;
  200. if (m_cipher == null) m_cipher = GenCipher();
  201. byte[] iv = _iv[0] = new byte[m_cipher.IVSize];
  202. if (m_random == null) m_random = GenRandom();
  203. m_random.Fill(iv, 0, iv.Length);
  204. byte[] key = GenKey(m_passphrase, iv);
  205. byte[] encoded = plain;
  206. int bsize = m_cipher.BlockSize;
  207. if (encoded.Length % bsize != 0)
  208. {
  209. byte[] foo = new byte[(encoded.Length / bsize + 1) * bsize];
  210. Array.Copy(encoded, 0, foo, 0, encoded.Length);
  211. encoded = foo;
  212. }
  213. try
  214. {
  215. m_cipher.Init(Cipher.ENCRYPT_MODE, key, iv);
  216. m_cipher.Update(encoded, 0, encoded.Length, encoded, 0);
  217. }
  218. catch (Exception e)
  219. {
  220. Trace.TraceError("Error:\r\n{0}", e.ToString());
  221. }
  222. return encoded;
  223. }
  224. internal abstract bool Parse(byte[] data);
  225. private byte[] Decrypt(byte[] data, byte[] passphrase, byte[] iv)
  226. {
  227. /*
  228. if(iv==null){ // FSecure
  229. iv=new byte[8];
  230. for(int i=0; i<iv.Length; i++)iv[i]=0;
  231. }
  232. */
  233. try
  234. {
  235. byte[] key = GenKey(passphrase, iv);
  236. m_cipher.Init(Cipher.DECRYPT_MODE, key, iv);
  237. byte[] plain = new byte[data.Length];
  238. m_cipher.Update(data, 0, data.Length, plain, 0);
  239. return plain;
  240. }
  241. catch (Exception e)
  242. {
  243. Trace.TraceError("Error:\r\n{0}", e.ToString());
  244. }
  245. return null;
  246. }
  247. internal int WriteSEQUENCE(byte[] buf, int index, int len)
  248. {
  249. buf[index++] = 0x30;
  250. index = WriteLength(buf, index, len);
  251. return index;
  252. }
  253. internal int WriteINTEGER(byte[] buf, int index, byte[] data)
  254. {
  255. buf[index++] = 0x02;
  256. index = WriteLength(buf, index, data.Length);
  257. Array.Copy(data, 0, buf, index, data.Length);
  258. index += data.Length;
  259. return index;
  260. }
  261. internal int CountLength(int len)
  262. {
  263. int i = 1;
  264. if (len <= 0x7f) return i;
  265. while (len > 0)
  266. {
  267. len >>= 8;
  268. i++;
  269. }
  270. return i;
  271. }
  272. internal int WriteLength(byte[] data, int index, int len)
  273. {
  274. int i = CountLength(len) - 1;
  275. if (i == 0)
  276. {
  277. data[index++] = (byte)len;
  278. return index;
  279. }
  280. data[index++] = (byte)(0x80 | i);
  281. int j = index + i;
  282. while (i > 0)
  283. {
  284. data[index + i - 1] = (byte)(len & 0xff);
  285. len >>= 8;
  286. i--;
  287. }
  288. return j;
  289. }
  290. private IRandom GenRandom()
  291. {
  292. if (m_random == null)
  293. {
  294. try
  295. {
  296. Type t = Type.GetType(m_jsch.GetConfig("random"));
  297. m_random = (IRandom)Activator.CreateInstance(t);
  298. }
  299. catch (Exception e)
  300. {
  301. Trace.TraceError("connect: random " + e);
  302. }
  303. }
  304. return m_random;
  305. }
  306. private HashAlgorithm GenHash()
  307. {
  308. try
  309. {
  310. Type t = Type.GetType(m_jsch.GetConfig("md5"));
  311. m_hash = (HashAlgorithm)Activator.CreateInstance(t);
  312. m_hash.Init();
  313. }
  314. catch//(Exception e)
  315. {
  316. }
  317. return m_hash;
  318. }
  319. private Cipher GenCipher()
  320. {
  321. try
  322. {
  323. Type t;
  324. t = Type.GetType(m_jsch.GetConfig("3des-cbc"));
  325. m_cipher = (Cipher)(Activator.CreateInstance(t));
  326. }
  327. catch//(Exception e)
  328. {
  329. }
  330. return m_cipher;
  331. }
  332. /*
  333. hash is MD5
  334. h(0) <- hash(passphrase, iv);
  335. h(n) <- hash(h(n-1), passphrase, iv);
  336. key <- (h(0),...,h(n))[0,..,key.Length];
  337. */
  338. [MethodImpl(MethodImplOptions.Synchronized)]
  339. internal byte[] GenKey(byte[] passphrase, byte[] iv)
  340. {
  341. if (m_cipher == null) m_cipher = GenCipher();
  342. if (m_hash == null) m_hash = GenHash();
  343. byte[] key = new byte[m_cipher.BlockSize];
  344. int hsize = m_hash.BlockSize;
  345. byte[] hn = new byte[key.Length / hsize * hsize +
  346. (key.Length % hsize == 0 ? 0 : hsize)];
  347. try
  348. {
  349. byte[] tmp = null;
  350. if (vendor == VENDOR_OPENSSH)
  351. {
  352. for (int index = 0; index + hsize <= hn.Length; )
  353. {
  354. if (tmp != null) { m_hash.Update(tmp, 0, tmp.Length); }
  355. m_hash.Update(passphrase, 0, passphrase.Length);
  356. m_hash.Update(iv, 0, iv.Length);
  357. tmp = m_hash.Digest();
  358. Array.Copy(tmp, 0, hn, index, tmp.Length);
  359. index += tmp.Length;
  360. }
  361. Array.Copy(hn, 0, key, 0, key.Length);
  362. }
  363. else if (vendor == VENDOR_FSECURE)
  364. {
  365. for (int index = 0; index + hsize <= hn.Length; )
  366. {
  367. if (tmp != null) { m_hash.Update(tmp, 0, tmp.Length); }
  368. m_hash.Update(passphrase, 0, passphrase.Length);
  369. tmp = m_hash.Digest();
  370. Array.Copy(tmp, 0, hn, index, tmp.Length);
  371. index += tmp.Length;
  372. }
  373. Array.Copy(hn, 0, key, 0, key.Length);
  374. }
  375. }
  376. catch (Exception e)
  377. {
  378. Trace.TraceError("Error:\r\n{0}", e.ToString());
  379. }
  380. return key;
  381. }
  382. public void SetPassphrase(string passphrase)
  383. {
  384. if (passphrase == null || passphrase.Length == 0)
  385. {
  386. SetPassphrase((byte[])null);
  387. }
  388. else
  389. {
  390. SetPassphrase(Util.GetBytes(passphrase));
  391. }
  392. }
  393. public void SetPassphrase(byte[] passphrase)
  394. {
  395. if (passphrase != null && passphrase.Length == 0)
  396. passphrase = null;
  397. this.m_passphrase = passphrase;
  398. }
  399. public bool isEncrypted { get { return m_encrypted; } }
  400. public bool Decrypt(string _passphrase)
  401. {
  402. byte[] passphrase = Util.GetBytes(_passphrase);
  403. byte[] foo = Decrypt(m_data, passphrase, m_iv);
  404. if (Parse(foo))
  405. {
  406. m_encrypted = false;
  407. }
  408. return !m_encrypted;
  409. }
  410. public static KeyPair Load(JSch jsch, string prvkey)
  411. {
  412. string pubkey = prvkey + ".pub";
  413. // if(!new File(pubkey).exists())
  414. if (!File.Exists(pubkey))
  415. pubkey = null;
  416. return Load(jsch, prvkey, pubkey);
  417. }
  418. public static KeyPair Load(JSch jsch, string prvkey, string pubkey)
  419. {
  420. byte[] iv = new byte[8]; // 8
  421. bool encrypted = true;
  422. byte[] data = null;
  423. byte[] publickeyblob = null;
  424. int type = ERROR;
  425. int vendor = VENDOR_OPENSSH;
  426. try
  427. {
  428. //File file=new File(prvkey);
  429. FileStream fis = File.OpenRead(prvkey);
  430. byte[] buf = new byte[(int)(fis.Length)];
  431. int len = fis.Read(buf, 0, buf.Length);
  432. fis.Close();
  433. int i = 0;
  434. while (i < len)
  435. {
  436. if (buf[i] == 'B' && buf[i + 1] == 'E' && buf[i + 2] == 'G' && buf[i + 3] == 'I')
  437. {
  438. i += 6;
  439. if (buf[i] == 'D' &&
  440. buf[i + 1] == 'S' &&
  441. buf[i + 2] == 'A')
  442. {
  443. type = DSA;
  444. }
  445. else if (buf[i] == 'R' &&
  446. buf[i + 1] == 'S' &&
  447. buf[i + 2] == 'A')
  448. {
  449. type = RSA;
  450. }
  451. else if (buf[i] == 'S' &&
  452. buf[i + 1] == 'S' &&
  453. buf[i + 2] == 'H')
  454. { // FSecure
  455. type = UNKNOWN;
  456. vendor = VENDOR_FSECURE;
  457. }
  458. else
  459. {
  460. throw new JSchException("invaid privatekey: " + prvkey);
  461. }
  462. i += 3;
  463. continue;
  464. }
  465. if (buf[i] == 'C' &&
  466. buf[i + 1] == 'B' &&
  467. buf[i + 2] == 'C' &&
  468. buf[i + 3] == ',')
  469. {
  470. i += 4;
  471. for (int ii = 0; ii < iv.Length; ii++)
  472. {
  473. iv[ii] = (byte)(((a2b(buf[i++]) << 4) & 0xf0) + (a2b(buf[i++]) & 0xf));
  474. }
  475. continue;
  476. }
  477. if (buf[i] == 0x0d &&
  478. i + 1 < buf.Length &&
  479. buf[i + 1] == 0x0a)
  480. {
  481. i++;
  482. continue;
  483. }
  484. if (buf[i] == 0x0a && i + 1 < buf.Length)
  485. {
  486. if (buf[i + 1] == 0x0a)
  487. {
  488. i += 2;
  489. break;
  490. }
  491. if (buf[i + 1] == 0x0d &&
  492. i + 2 < buf.Length &&
  493. buf[i + 2] == 0x0a)
  494. {
  495. i += 3;
  496. break;
  497. }
  498. bool inheader = false;
  499. for (int j = i + 1; j < buf.Length; j++)
  500. {
  501. if (buf[j] == 0x0a) break;
  502. //if(buf[j]==0x0d) break;
  503. if (buf[j] == ':')
  504. {
  505. inheader = true; break;
  506. }
  507. }
  508. if (!inheader)
  509. {
  510. i++;
  511. encrypted = false; // no passphrase
  512. break;
  513. }
  514. }
  515. i++;
  516. }
  517. if (type == ERROR)
  518. throw new JSchException("invaid privatekey: " + prvkey);
  519. int start = i;
  520. while (i < len)
  521. {
  522. if (buf[i] == 0x0a)
  523. {
  524. bool xd = (buf[i - 1] == 0x0d);
  525. Array.Copy(buf, i + 1,
  526. buf,
  527. i - (xd ? 1 : 0),
  528. len - i - 1 - (xd ? 1 : 0)
  529. );
  530. if (xd) len--;
  531. len--;
  532. continue;
  533. }
  534. if (buf[i] == '-') { break; }
  535. i++;
  536. }
  537. data = Util.FromBase64(buf, start, i - start);
  538. if (data.Length > 4 && // FSecure
  539. data[0] == (byte)0x3f &&
  540. data[1] == (byte)0x6f &&
  541. data[2] == (byte)0xf9 &&
  542. data[3] == (byte)0xeb)
  543. {
  544. ByteBuffer _buf = new ByteBuffer(data);
  545. _buf.GetInt(); // 0x3f6ff9be
  546. _buf.GetInt();
  547. byte[] _type = _buf.GetString();
  548. //System.outs.println("type: "+new string(_type));
  549. byte[] _cipher = _buf.GetString();
  550. string cipher = Util.GetString(_cipher);
  551. //System.outs.println("cipher: "+cipher);
  552. if (cipher.Equals("3des-cbc"))
  553. {
  554. _buf.GetInt();
  555. byte[] foo = new byte[data.Length - _buf.Offset];
  556. _buf.GetByte(foo);
  557. data = foo;
  558. encrypted = true;
  559. throw new JSchException("unknown privatekey format: " + prvkey);
  560. }
  561. else if (cipher.Equals("none"))
  562. {
  563. _buf.GetInt();
  564. _buf.GetInt();
  565. encrypted = false;
  566. byte[] foo = new byte[data.Length - _buf.Offset];
  567. _buf.GetByte(foo);
  568. data = foo;
  569. }
  570. }
  571. if (pubkey != null)
  572. {
  573. try
  574. {
  575. //file=new File(pubkey);
  576. fis = File.OpenRead(pubkey);
  577. buf = new byte[(int)(fis.Length)];
  578. len = fis.Read(buf, 0, buf.Length);
  579. fis.Close();
  580. if (buf.Length > 4 && // FSecure's public key
  581. buf[0] == '-' &&
  582. buf[1] == '-' &&
  583. buf[2] == '-' &&
  584. buf[3] == '-')
  585. {
  586. bool valid = true;
  587. i = 0;
  588. do { i++; } while (buf.Length > i && buf[i] != 0x0a);
  589. if (buf.Length <= i)
  590. valid = false;
  591. while (valid)
  592. {
  593. if (buf[i] == 0x0a)
  594. {
  595. bool inheader = false;
  596. for (int j = i + 1; j < buf.Length; j++)
  597. {
  598. if (buf[j] == 0x0a)
  599. break;
  600. if (buf[j] == ':') { inheader = true; break; }
  601. }
  602. if (!inheader)
  603. {
  604. i++;
  605. break;
  606. }
  607. }
  608. i++;
  609. }
  610. if (buf.Length <= i)
  611. valid = false;
  612. start = i;
  613. while (valid && i < len)
  614. {
  615. if (buf[i] == 0x0a)
  616. {
  617. Array.Copy(buf, i + 1, buf, i, len - i - 1);
  618. len--;
  619. continue;
  620. }
  621. if (buf[i] == '-')
  622. break;
  623. i++;
  624. }
  625. if (valid)
  626. {
  627. publickeyblob = Util.FromBase64(buf, start, i - start);
  628. if (type == UNKNOWN)
  629. {
  630. if (publickeyblob[8] == 'd')
  631. type = DSA;
  632. else if (publickeyblob[8] == 'r')
  633. type = RSA;
  634. }
  635. }
  636. }
  637. else
  638. {
  639. if (buf[0] == 's' &&
  640. buf[1] == 's' &&
  641. buf[2] == 'h' &&
  642. buf[3] == '-')
  643. {
  644. i = 0;
  645. while (i < len) { if (buf[i] == ' ')break; i++; } i++;
  646. if (i < len)
  647. {
  648. start = i;
  649. while (i < len)
  650. {
  651. if (buf[i] == ' ')
  652. break; i++;
  653. }
  654. publickeyblob = Util.FromBase64(buf, start, i - start);
  655. }
  656. }
  657. }
  658. }
  659. catch//(Exception ee)
  660. {
  661. }
  662. }
  663. }
  664. catch (Exception e)
  665. {
  666. if (e is JSchException)
  667. throw (JSchException)e;
  668. throw new JSchException(e.ToString());
  669. }
  670. KeyPair kpair = null;
  671. if (type == DSA)
  672. kpair = new KeyPairDSA(jsch);
  673. else if (type == RSA)
  674. kpair = new KeyPairRSA(jsch);
  675. if (kpair != null)
  676. {
  677. kpair.m_encrypted = encrypted;
  678. kpair.m_publickeyblob = publickeyblob;
  679. kpair.vendor = vendor;
  680. if (encrypted)
  681. {
  682. kpair.m_iv = iv;
  683. kpair.m_data = data;
  684. }
  685. else
  686. {
  687. if (kpair.Parse(data))
  688. return kpair;
  689. else
  690. throw new JSchException("invaid privatekey: " + prvkey);
  691. }
  692. }
  693. return kpair;
  694. }
  695. static private byte a2b(byte c)
  696. {
  697. if ('0' <= c && c <= '9')
  698. return (byte)(c - '0');
  699. return (byte)(c - 'a' + 10);
  700. }
  701. static private byte b2a(byte c)
  702. {
  703. if (0 <= c && c <= 9)
  704. return (byte)(c + '0');
  705. return (byte)(c - 10 + 'A');
  706. }
  707. public virtual void Dispose()
  708. {
  709. m_passphrase = null;
  710. }
  711. }
  712. }