Skip to content

Commit

Permalink
TKSS-536: Update READMEs for ECKeyPairGenerator
Browse files Browse the repository at this point in the history
  • Loading branch information
johnshajiang committed Nov 5, 2023
1 parent 330e1cc commit 56928eb
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 19 deletions.
20 changes: 16 additions & 4 deletions kona-crypto/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,24 @@ the less the position is, the higher the priority is. The minimum value is 1.
### SM2

#### Key pair
Generating SM2 key pair is the same as generating the key pairs on other EC curves. It just needs to invoke the standard JDK APIs.
Generating SM2 key pair is the same as generating the key pairs on other EC curves. It just needs to invoke the standard JDK APIs. `KonaCrypto` provides two `KeyPairGenerator` implementations for generating SM2 key pair:

Create KeyPairGenerator instance.
- The JDK builtin `ECKeyPairGenerator`. In the key pair generated by this implementations, the format of private key is `PKCS#8` and the format of public key is `X.509`.
- A new introduced `SM2KeyPairGenerator`. In the key pair generated by this implementations, the format of the both keys is `RAW`. The length of private key is 32-bytes. The length of public key is 65-bytes, The format is `04||x||y`. `04` represents the uncompressed format; `x` and `y` are the coordinates of the public point in the curve, and both are 32-bytes.

Create `KeyPairGenerator` implementation on `ECKeyPairGenerator`.

```
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("EC);
keyPairGenerator.initialize(spec);
```

`spec` can be `SM2ParameterSpec` (use `SM2ParameterSpec.instance()` to create the instance) or `ECGenParameterSpec` (use `new ECGenParameterSpec("curveSM2")` to create the instance).

If create `KeyPairGenerator` implementation on `SM2KeyPairGenerator`, the codes are the belows,

```
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("SM2");
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("SM2);
```

Generate key pair.
Expand All @@ -57,7 +69,7 @@ ECPrivateKey privateKey = (ECPrivateKey) keyPair.getPrivate();

SM2 key pair is also EC key pair, so the public key and private key are also ECPublicKey and ECPrivateKey respectively.

SM2 public key is 65-bytes length. The format is `04|x|y`. `04` represents the uncompressed format; `x` and `y` are the coordinates of the public point in the curve.
SM2 public key is 65-bytes length.

```
byte[] encodedPublicKey = publicKey.getEncoded();
Expand Down
29 changes: 14 additions & 15 deletions kona-crypto/README_cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,34 +39,33 @@ position的值越小,代表的优先级越高,最小可为1。
### SM2

#### 密钥对
生成SM2密钥对与生成JDK自带的其它算法(如EC)密钥对的方式是完全相同的,仅需要调用标准的JDK API就可以生成密钥对。
生成SM2密钥对与生成JDK自带的其它算法(如EC)密钥对的方式是完全相同的,仅需要调用标准的JDK API就可以了。`KonaCrypto`提供了两个`KeyPairGenerator`实现去
生成SM2密钥对:

创建KeyPairGenerator实例。
- JDK自带的`ECKeyPairGenerator`。它生成的密钥对中,私钥格式为`PKCS#8`,公钥格式为`X.509`
- 新引入的`SM2KeyPairGenerator`。它生成的密钥对中,私钥和公钥格式均为`RAW`。私钥长度为32字节。公钥为长度为65字节,格式为`04||x||y`,其中`04`表示非压缩格式,`x``y`分别为该公钥点在椭圆曲线上的仿射横坐标和纵坐标的值。

```
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("SM2");
```

生成密钥对。
创建使用`ECKeyPairGenerator``KeyPairGenerator`实例。

```
KeyPair keyPair = keyPairGenerator.generateKeyPair();
ECPublicKey publicKey = (ECPublicKey) keyPair.getPublic();
ECPrivateKey privateKey = (ECPrivateKey) keyPair.getPrivate();
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("EC);
keyPairGenerator.initialize(spec);
```

SM2的密钥对本质还是EC密钥对,所以其中的公钥与私钥也分别符合ECPublicKey与ECPrivateKey的属性
其中,`spec`可以为`SM2ParameterSpec`(使用`SM2ParameterSpec.instance()`创建它的实例)或者是`ECGenParameterSpec`(使用`new ECGenParameterSpec("curveSM2")`创建它的实例)

SM2公钥的编码长度固定为65字节,其格式为`04|x|y`,其中`04`表示非压缩格式,`x``y`分别为该公钥点在椭圆曲线上的仿射横坐标和纵坐标的值
若创建使用`SM2KeyPairGenerator``KeyPairGenerator`实例,则代码如下

```
byte[] encodedPublicKey = publicKey.getEncoded();
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("SM2);
```

SM2私钥的编码长度固定为32字节,无编码格式
生成密钥对

```
byte[] encodedPrivateKey = privateKey.getEncoded();
KeyPair keyPair = keyPairGenerator.generateKeyPair();
ECPublicKey publicKey = (ECPublicKey) keyPair.getPublic();
ECPrivateKey privateKey = (ECPrivateKey) keyPair.getPrivate();
```

关于密钥对生成器API的更详细用法,请参考[KeyPairGenerator]的官方文档。
Expand Down

0 comments on commit 56928eb

Please sign in to comment.