1 /*
2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
178 0x01,
179 // length
180 0x04,
181 // privateKey contents
182 0x04, 0x03, 0x02,
183 // 4th (optional) element -- attributes [0] IMPLICIT Attributes
184 // OPTIONAL
185 // (Attributes = SET OF Attribute) Here, it will be empty.
186 0x01,
187 // length
188 0x01 };
189
190 static void raiseException(String expected, String received) {
191 throw new RuntimeException(
192 "Expected " + expected + "; Received " + received);
193 }
194
195 public static void main(String[] args)
196 throws IOException, InvalidKeyException {
197
198 BigInteger p = BigInteger.valueOf(1);
199 BigInteger q = BigInteger.valueOf(2);
200 BigInteger g = BigInteger.valueOf(3);
201 BigInteger x = BigInteger.valueOf(4);
202
203 DSAPrivateKey priv = new DSAPrivateKey(p, q, g, x);
204
205 byte[] encodedKey = priv.getEncoded();
206 byte[] expectedBytes = new byte[EXPECTED.length];
207 for (int i = 0; i < EXPECTED.length; i++) {
208 expectedBytes[i] = (byte) EXPECTED[i];
209 }
210
211 dumpByteArray("encodedKey :", encodedKey);
212 if (!Arrays.equals(encodedKey, expectedBytes)) {
213 raiseException(new String(expectedBytes), new String(encodedKey));
214 }
215
216 PKCS8Key decodedKey = PKCS8Key.parse(new DerValue(encodedKey));
217
218 String alg = decodedKey.getAlgorithm();
219 AlgorithmId algId = decodedKey.getAlgorithmId();
220 out.println("Algorithm :" + alg);
221 out.println("AlgorithmId: " + algId);
222
223 if (!ALGORITHM.equals(alg)) {
|
1 /*
2 * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
178 0x01,
179 // length
180 0x04,
181 // privateKey contents
182 0x04, 0x03, 0x02,
183 // 4th (optional) element -- attributes [0] IMPLICIT Attributes
184 // OPTIONAL
185 // (Attributes = SET OF Attribute) Here, it will be empty.
186 0x01,
187 // length
188 0x01 };
189
190 static void raiseException(String expected, String received) {
191 throw new RuntimeException(
192 "Expected " + expected + "; Received " + received);
193 }
194
195 public static void main(String[] args)
196 throws IOException, InvalidKeyException {
197
198 BigInteger x = BigInteger.valueOf(1);
199 BigInteger p = BigInteger.valueOf(2);
200 BigInteger q = BigInteger.valueOf(3);
201 BigInteger g = BigInteger.valueOf(4);
202
203 DSAPrivateKey priv = new DSAPrivateKey(x, p, q, g);
204
205 byte[] encodedKey = priv.getEncoded();
206 byte[] expectedBytes = new byte[EXPECTED.length];
207 for (int i = 0; i < EXPECTED.length; i++) {
208 expectedBytes[i] = (byte) EXPECTED[i];
209 }
210
211 dumpByteArray("encodedKey :", encodedKey);
212 if (!Arrays.equals(encodedKey, expectedBytes)) {
213 raiseException(new String(expectedBytes), new String(encodedKey));
214 }
215
216 PKCS8Key decodedKey = PKCS8Key.parse(new DerValue(encodedKey));
217
218 String alg = decodedKey.getAlgorithm();
219 AlgorithmId algId = decodedKey.getAlgorithmId();
220 out.println("Algorithm :" + alg);
221 out.println("AlgorithmId: " + algId);
222
223 if (!ALGORITHM.equals(alg)) {
|