PageRenderTime 46ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/SharpSSH/jsch/KeyPair.cs

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