Skip to content

Commit

Permalink
Merge pull request #188 from yg3630536/feature/add-ecies-crypto
Browse files Browse the repository at this point in the history
add ecies crypto
  • Loading branch information
junqizhang-dev committed Apr 29, 2020
2 parents 76fb005 + e413324 commit 6dc4f87
Show file tree
Hide file tree
Showing 26 changed files with 561 additions and 165 deletions.
1 change: 1 addition & 0 deletions .ci/script/build-ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ function modify_config()
cp ${java_source_code_dir}/.ci/ca.crt ${java_source_code_dir}/src/test/resources
cp ${java_source_code_dir}/.ci/node.crt ${java_source_code_dir}/src/test/resources
cp ${java_source_code_dir}/.ci/node.key ${java_source_code_dir}/src/test/resources
cp -r ${java_source_code_dir}/src/main/resources/WeDPR_dynamic_lib ${java_source_code_dir}/src/test/resources
echo "modify sdk config finished..."
}

Expand Down
10 changes: 8 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ dependencies {
exclude group: "org.slf4j", module: "slf4j-log4j12"
}
compile("org.fisco-bcos:web3sdk:2.4.0-0423-SNAPSHOT")
compile files("lib/WeDPR-Java-SDK.jar")
compile fileTree(dir: 'lib', include: '*.jar')
testCompile logger, lombok, apache_commons, json, junit, jmockit, rpc, pdfbox, protobuf, caffeine, oval
} else {
compile fileTree(dir: 'dist/lib', include: '*.jar')
Expand All @@ -163,7 +163,7 @@ dependencies {
exclude group: "org.slf4j", module: "slf4j-log4j12"
}
compile("org.fisco-bcos:web3sdk:2.4.0-0423-SNAPSHOT")
compile files("lib/WeDPR-Java-SDK.jar")
compile fileTree(dir: 'lib', include: '*.jar')
testCompile logger, apache_commons, json, junit, jmockit, rpc, pdfbox, protobuf, caffeine, oval
} else {
compileOnly files('dist/lib/lombok-1.18.10.jar')
Expand Down Expand Up @@ -211,6 +211,12 @@ task javadocJar(type: Jar, dependsOn: javadoc) {
test {
systemProperty "jdk.tls.namedGroups", "${jdkTlsNamedGroups}"
jvmArgs "-javaagent:${classpath.find { it.name.contains("jmockit") }.absolutePath}"
doFirst {
copy {
from file('src/main/resources/WeDPR_dynamic_lib')
into 'src/test/resources/WeDPR_dynamic_lib'
}
}
}

artifacts {
Expand Down
108 changes: 107 additions & 1 deletion docs/zh_CN/docs/weidentity-java-sdk-doc.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17113,4 +17113,110 @@ CacheManager
String cptValue = cptCahceNode.get("cptKey");
//移除缓存数据
cptCahceNode.remove("cptKey")
----
----

CryptoService
^^^^^^^^^^^^^^^^^

1. encrypt
~~~~~~~~~~~~~~~~~~~

**基本信息**

.. code-block:: text
接口名称: com.webank.weid.suite.api.crypto.inf.CryptoService.encrypt
接口定义: public String encrypt(String content, String key) throws EncodeSuiteException;
接口描述: 根据不同类型加密算法对数据进行加密
.. note::
注意:目前提供服务的加密算法有CryptoType.AES和CryptoType.ECIES, 加密返回数据为Base64字符串。ECIES加解密请通过build-tools获取libffi_ecies.so和WeDPR-ecies.jar


**接口入参**\ :

.. list-table::
:header-rows: 1

* - 名称
- 类型
- 非空
- 说明
- 备注
* - content
- String
- Y
- 需要加密的数据
-UTF-8格式数据
* - key
- String
- Y
- 加密使用的秘钥
-非对称秘钥请使用Base64处理


**接口返回**\ : String;

**调用示例**

.. code-block:: java
String key = "abc";
String original = "123";
// AES加密
String encrypt = CryptoServiceFactory.getCryptoService(CryptoType.AES).encrypt(original, key);
// ECIES加密
key = "APOsCflGTsr7ltZBRRA5WS7KL8FzJ8NquybVadp2GsRVmtzTSEYSgW1i76jLOCTJoUPlB+J0KFTG3WKYoltMll0=";// weid公钥BASE64
original = "123";
String encrypt = CryptoServiceFactory.getCryptoService(CryptoType.ECIES).encrypt(original, key);
----

2. decrypt
~~~~~~~~~~~~~~~~~~~

**基本信息**

.. code-block:: text
接口名称: com.webank.weid.suite.api.crypto.inf.CryptoService.decrypt
接口定义: public String decrypt(String content, String key) throws EncodeSuiteException;
接口描述: 根据加密的Base64字符串进行解密,并返回原字符串
**接口入参**\ :

.. list-table::
:header-rows: 1

* - 名称
- 类型
- 非空
- 说明
- 备注
* - content
- String
- Y
- 待解密字符串
-加密后并使用Base64处理的数据
* - key
- String
- Y
- 解密数据所使用的秘钥
-非对称秘钥请使用Base64处理

**接口返回**\ : String;

**调用示例**

.. code-block:: java
String key = "abc"; //AES秘钥
String encrypt = "xxxx";//密文数据
// AES解密
String decrypt = CryptoServiceFactory.getCryptoService(CryptoType.AES).decrypt(encrypt, key);
key = "AMcwy+851eDtxY/1vcTtxttwqTaBfczp7Q7fL41fGCag"; // weid私钥BASE64
encrypt = "xxxx";//密文数据
// AES解密
String decrypt = CryptoServiceFactory.getCryptoService(CryptoType.ECIES).decrypt(encrypt, key);
----
Binary file added lib/WeDPR-ecies.jar
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ public EncodeSuiteException(ErrorCode errorCode) {
this.errorCode = errorCode;
}

public EncodeSuiteException(String message) {
super(message);
}

public EncodeSuiteException(ErrorCode errorCode, String message) {
super(message);
this.errorCode = errorCode;
}

public ErrorCode getErrorCode() {
return errorCode;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@
import com.webank.weid.protocol.base.WeIdDocument;
import com.webank.weid.protocol.response.GetEncryptKeyResponse;
import com.webank.weid.protocol.response.ResponseData;
import com.webank.weid.suite.api.crypto.CryptoServiceFactory;
import com.webank.weid.suite.api.crypto.params.CryptoType;
import com.webank.weid.suite.api.transportation.params.EncodeType;
import com.webank.weid.suite.crypto.CryptServiceFactory;
import com.webank.weid.suite.entity.CryptType;
import com.webank.weid.suite.entity.TransCodeBaseData;
import com.webank.weid.suite.transmission.TransmissionService;
import com.webank.weid.util.DataToolUtils;
Expand Down Expand Up @@ -115,8 +115,8 @@ private ResponseData<String> getBarCodeData(
}
logger.info("[getBarCodeData] begin decrypt the data");
String data = String.valueOf(codeData.getData());
String value = CryptServiceFactory
.getCryptService(CryptType.AES)
String value = CryptoServiceFactory
.getCryptoService(CryptoType.AES)
.decrypt(data, encryptKey.getEncryptKey());
codeData.setData(value);
barCodeRes.setResult(DataToolUtils.serialize(codeData));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,39 +17,44 @@
* along with weid-java-sdk. If not, see <https://www.gnu.org/licenses/>.
*/

package com.webank.weid.suite.crypto;
package com.webank.weid.suite.api.crypto;

import java.util.HashMap;
import java.util.Map;

import com.webank.weid.exception.EncodeSuiteException;
import com.webank.weid.suite.entity.CryptType;
import com.webank.weid.suite.api.crypto.inf.CryptoService;
import com.webank.weid.suite.api.crypto.params.CryptoType;
import com.webank.weid.suite.crypto.AesCryptoService;
import com.webank.weid.suite.crypto.EciesCryptoService;
import com.webank.weid.suite.crypto.RsaCryptoService;

/**
* 秘钥对象工厂, 根据不同类型秘钥得到相应的秘钥处理对象.
* @author v_wbgyang
*
*/
public class CryptServiceFactory {
public class CryptoServiceFactory {

/**
* 支持加密类型的配置Map,目前支持仅支持AES.
*/
private static final Map<String, CryptService> cryptServiceMap =
new HashMap<String, CryptService>();
private static final Map<String, CryptoService> cryptoServiceMap =
new HashMap<String, CryptoService>();

static {
cryptServiceMap.put(CryptType.AES.name(), new AesCryptService());
cryptServiceMap.put(CryptType.RSA.name(), new RsaCryptService());
cryptoServiceMap.put(CryptoType.AES.name(), new AesCryptoService());
cryptoServiceMap.put(CryptoType.RSA.name(), new RsaCryptoService());
cryptoServiceMap.put(CryptoType.ECIES.name(), new EciesCryptoService());
}

/**
* 通过秘钥枚举类型获取秘钥对象.
* @param cryptType 秘钥枚举类型
* @param cryptoType 秘钥枚举类型
* @return 秘钥加解密处理对象
*/
public static CryptService getCryptService(CryptType cryptType) {
CryptService service = cryptServiceMap.get(cryptType.name());
public static CryptoService getCryptoService(CryptoType cryptoType) {
CryptoService service = cryptoServiceMap.get(cryptoType.name());
if (service == null) {
throw new EncodeSuiteException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* along with weid-java-sdk. If not, see <https://www.gnu.org/licenses/>.
*/

package com.webank.weid.suite.crypto;
package com.webank.weid.suite.api.crypto.inf;

import com.webank.weid.exception.EncodeSuiteException;

Expand All @@ -26,21 +26,21 @@
* @author v_wbgyang
*
*/
public interface CryptService {
public interface CryptoService {

/**
* 加密方法.
* @param content 待加密字符串
* @param password 秘钥
* @param key 秘钥
* @return 返回加密后的字符串数据
*/
public String encrypt(String content, String password) throws EncodeSuiteException;
public String encrypt(String content, String key) throws EncodeSuiteException;

/**
* 解密方法.
* @param content 待解密字符串
* @param password 秘钥
* @param key 秘钥
* @return 返回解密后的字符串数据
*/
public String decrypt(String content, String password) throws EncodeSuiteException;
public String decrypt(String content, String key) throws EncodeSuiteException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* along with weid-java-sdk. If not, see <https://www.gnu.org/licenses/>.
*/

package com.webank.weid.suite.entity;
package com.webank.weid.suite.api.crypto.params;

import lombok.Data;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
* along with weid-java-sdk. If not, see <https://www.gnu.org/licenses/>.
*/

package com.webank.weid.suite.entity;
package com.webank.weid.suite.api.crypto.params;

/**
* 加解密类型枚举.
* @author v_wbgyang
*
*/
public enum CryptType {
AES, RSA;
public enum CryptoType {
AES, RSA, ECIES;
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,16 @@
* along with weid-java-sdk. If not, see <https://www.gnu.org/licenses/>.
*/

package com.webank.weid.suite.crypto;
package com.webank.weid.suite.api.crypto.params;

import java.nio.charset.StandardCharsets;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

import org.bouncycastle.util.encoders.Base64;
import org.apache.commons.codec.binary.Base64;

import com.webank.weid.suite.entity.Asymmetrickey;
import com.webank.weid.suite.entity.CryptType;
import com.webank.weid.util.DataToolUtils;

/**
Expand All @@ -54,18 +52,28 @@ public static String getKey() {
* @throws NoSuchAlgorithmException 找不到Algorithm异常
*/
public static Asymmetrickey getKeyForRsa() throws NoSuchAlgorithmException {
return getKeyForRsa(DEFAULT_KEY_SIZE);
}

/**
* 生成RSA非对称加密密钥.
* @param keySize 密钥对大小范围
* @return 返回Asymmetrickey 非对此秘钥
* @throws NoSuchAlgorithmException 找不到Algorithm异常
*/
public static Asymmetrickey getKeyForRsa(int keySize) throws NoSuchAlgorithmException {
// KeyPairGenerator类用于生成公钥和私钥对,基于RSA算法生成对象
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(CryptType.RSA.name());
// 初始化密钥对生成器,密钥大小为96-1024位
keyPairGen.initialize(DEFAULT_KEY_SIZE, new SecureRandom());
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(CryptoType.RSA.name());
// 初始化密钥对生成器,密钥大小单位为位
keyPairGen.initialize(keySize, new SecureRandom());
// 生成一个密钥对,保存在keyPair中
KeyPair keyPair = keyPairGen.generateKeyPair();
String pub = new String(
Base64.encode(keyPair.getPublic().getEncoded()),
Base64.encodeBase64(keyPair.getPublic().getEncoded()),
StandardCharsets.UTF_8
);
String pri = new String(
Base64.encode(keyPair.getPrivate().getEncoded()),
Base64.encodeBase64(keyPair.getPrivate().getEncoded()),
StandardCharsets.UTF_8
);
Asymmetrickey key = new Asymmetrickey();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.security.GeneralSecurityException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
Expand All @@ -34,18 +35,19 @@
import org.slf4j.LoggerFactory;

import com.webank.weid.exception.EncodeSuiteException;
import com.webank.weid.suite.entity.CryptType;
import com.webank.weid.suite.api.crypto.inf.CryptoService;
import com.webank.weid.suite.api.crypto.params.CryptoType;

/**
* AES加解密处理类.
* @author v_wbgyang
*
*/
public class AesCryptService implements CryptService {
public class AesCryptoService implements CryptoService {

private static final Logger logger = LoggerFactory.getLogger(AesCryptService.class);
private static final Logger logger = LoggerFactory.getLogger(AesCryptoService.class);

private static final String KEY_ALGORITHM = CryptType.AES.name();
private static final String KEY_ALGORITHM = CryptoType.AES.name();

private static final String DEFAULT_CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";

Expand Down

0 comments on commit 6dc4f87

Please sign in to comment.