Mercurial > 510Connectbot
comparison src/ch/ethz/ssh2/signature/RSAPrivateKey.java @ 273:91a31873c42a ganymed
start conversion from trilead to ganymed
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Fri, 18 Jul 2014 11:21:46 -0700 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
272:ce2f4e397703 | 273:91a31873c42a |
---|---|
1 /* | |
2 * Copyright (c) 2006-2013 Christian Plattner. All rights reserved. | |
3 * Please refer to the LICENSE.txt for licensing details. | |
4 */ | |
5 package ch.ethz.ssh2.signature; | |
6 | |
7 import java.math.BigInteger; | |
8 import java.security.SecureRandom; | |
9 | |
10 /** | |
11 * RSAPrivateKey. | |
12 * | |
13 * @author Christian Plattner | |
14 * @version 2.50, 03/15/10 | |
15 */ | |
16 public class RSAPrivateKey | |
17 { | |
18 private BigInteger d; | |
19 private BigInteger e; | |
20 private BigInteger n; | |
21 | |
22 public RSAPrivateKey(BigInteger d, BigInteger e, BigInteger n) | |
23 { | |
24 this.d = d; | |
25 this.e = e; | |
26 this.n = n; | |
27 } | |
28 | |
29 public BigInteger getD() | |
30 { | |
31 return d; | |
32 } | |
33 | |
34 public BigInteger getE() | |
35 { | |
36 return e; | |
37 } | |
38 | |
39 public BigInteger getN() | |
40 { | |
41 return n; | |
42 } | |
43 | |
44 public RSAPublicKey getPublicKey() | |
45 { | |
46 return new RSAPublicKey(e, n); | |
47 } | |
48 | |
49 /** | |
50 * Generate an RSA hostkey for testing purposes only. | |
51 * | |
52 * @param numbits Key length in bits | |
53 * @return | |
54 */ | |
55 public static RSAPrivateKey generateKey(int numbits) | |
56 { | |
57 return generateKey(new SecureRandom(), numbits); | |
58 } | |
59 | |
60 /** | |
61 * Generate an RSA hostkey for testing purposes only. | |
62 * | |
63 * @param rnd Source for random bits | |
64 * @param numbits Key length in bits | |
65 * @return | |
66 */ | |
67 public static RSAPrivateKey generateKey(SecureRandom rnd, int numbits) | |
68 { | |
69 BigInteger p = BigInteger.probablePrime(numbits / 2, rnd); | |
70 BigInteger q = BigInteger.probablePrime(numbits / 2, rnd); | |
71 BigInteger phi = (p.subtract(BigInteger.ONE)).multiply(q.subtract(BigInteger.ONE)); | |
72 | |
73 BigInteger n = p.multiply(q); | |
74 BigInteger e = new BigInteger("65537"); | |
75 BigInteger d = e.modInverse(phi); | |
76 | |
77 return new RSAPrivateKey(d, e, n); | |
78 } | |
79 } |