author | weijun |
Sun, 08 Apr 2018 17:01:20 +0800 | |
changeset 49548 | 8bdf2b5f472d |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
49548
8bdf2b5f472d
8180570: Refactor sun/security/mscapi shell tests to plain java tests
weijun
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2006, 2018, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 22 |
*/ |
23 |
||
24 |
||
25 |
/** |
|
49548
8bdf2b5f472d
8180570: Refactor sun/security/mscapi shell tests to plain java tests
weijun
parents:
47216
diff
changeset
|
26 |
* @test |
8bdf2b5f472d
8180570: Refactor sun/security/mscapi shell tests to plain java tests
weijun
parents:
47216
diff
changeset
|
27 |
* @bug 6457422 6931562 8180570 |
8bdf2b5f472d
8180570: Refactor sun/security/mscapi shell tests to plain java tests
weijun
parents:
47216
diff
changeset
|
28 |
* @summary Confirm that plaintext can be encrypted and then decrypted using the |
8bdf2b5f472d
8180570: Refactor sun/security/mscapi shell tests to plain java tests
weijun
parents:
47216
diff
changeset
|
29 |
* RSA cipher in the SunMSCAPI crypto provider. NOTE: The RSA cipher is |
8bdf2b5f472d
8180570: Refactor sun/security/mscapi shell tests to plain java tests
weijun
parents:
47216
diff
changeset
|
30 |
* absent from the SunMSCAPI provider in OpenJDK builds. |
8bdf2b5f472d
8180570: Refactor sun/security/mscapi shell tests to plain java tests
weijun
parents:
47216
diff
changeset
|
31 |
* @requires os.family == "windows" |
2 | 32 |
*/ |
33 |
||
34 |
import javax.crypto.Cipher; |
|
35 |
import java.security.GeneralSecurityException; |
|
36 |
import java.security.KeyPairGenerator; |
|
37 |
import java.security.KeyPair; |
|
38 |
import java.security.Key; |
|
39 |
||
40 |
public class RSAEncryptDecrypt { |
|
41 |
public static final byte[] PLAINTEXT = {1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6}; |
|
42 |
||
43 |
public static void main(String[] args) throws Exception { |
|
44 |
||
45 |
KeyPairGenerator generator = |
|
46 |
KeyPairGenerator.getInstance("RSA", "SunMSCAPI"); |
|
47 |
||
48 |
KeyPair keyPair = generator.generateKeyPair(); |
|
49 |
Key publicKey = keyPair.getPublic(); |
|
50 |
Key privateKey = keyPair.getPrivate(); |
|
51 |
||
52 |
Cipher cipher = null; |
|
53 |
||
54 |
try { |
|
55 |
cipher = Cipher.getInstance("RSA", "SunMSCAPI"); |
|
56 |
||
57 |
} catch (GeneralSecurityException e) { |
|
58 |
System.out.println("Cipher not supported by provider, skipping..."); |
|
59 |
return; |
|
60 |
} |
|
61 |
||
62 |
cipher.init(Cipher.ENCRYPT_MODE, publicKey); |
|
63 |
displayBytes("Plaintext data:", PLAINTEXT); |
|
64 |
byte[] data = cipher.doFinal(PLAINTEXT); |
|
65 |
displayBytes("Encrypted data:", data); |
|
66 |
||
67 |
cipher.init(Cipher.DECRYPT_MODE, privateKey); |
|
68 |
data = cipher.doFinal(data); |
|
69 |
displayBytes("Decrypted data:", data); |
|
70 |
} |
|
71 |
||
72 |
private static void displayBytes(String label, byte[] bytes) { |
|
73 |
System.out.println(label + " [length=" + bytes.length + "]"); |
|
74 |
for (byte b : bytes) { |
|
75 |
System.out.print("0x" + Integer.toHexString(b & 0xFF) + " "); |
|
76 |
} |
|
77 |
System.out.println(); |
|
78 |
} |
|
79 |
} |