-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathSm9EncKey.java
More file actions
63 lines (53 loc) · 1.45 KB
/
Sm9EncKey.java
File metadata and controls
63 lines (53 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
* Copyright 2014-2023 The GmSSL Project. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the License); you may
* not use this file except in compliance with the License.
*
* http://www.apache.org/licenses/LICENSE-2.0
*/
package org.gmssl;
public class Sm9EncKey {
private long sm9_enc_key = 0;
private String id;
Sm9EncKey(long key, String id) {
this.sm9_enc_key = key;
this.id = id;
}
public Sm9EncKey(String id) {
this.sm9_enc_key = 0;
this.id = id;
}
public void importEncryptedPrivateKeyInfoPem(String pass, String file) {
if (this.sm9_enc_key != 0) {
GmSSLJNI.sm9_enc_key_free(this.sm9_enc_key);
}
if ((this.sm9_enc_key = GmSSLJNI.sm9_enc_key_info_decrypt_from_pem(pass, file)) == 0) {
throw new GmSSLException("");
}
}
public void exportEncryptedPrivateKeyInfoPem(String pass, String file) {
if (this.sm9_enc_key == 0) {
throw new GmSSLException("Key not initialized");
}
if (GmSSLJNI.sm9_enc_key_info_encrypt_to_pem(this.sm9_enc_key, pass, file) != 1) {
throw new GmSSLException("");
}
}
public String getId() {
return this.id;
}
public byte[] decrypt(byte[] ciphertext) {
if (this.sm9_enc_key == 0) {
throw new GmSSLException("");
}
if (ciphertext == null) {
throw new GmSSLException("");
}
byte[] plaintext;
if ((plaintext = GmSSLJNI.sm9_decrypt(this.sm9_enc_key, this.id, ciphertext)) == null) {
throw new GmSSLException("");
}
return plaintext;
}
}