-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #176 from yg3630536/feature/add-rsa-ecrypt
* add code for rsa
- Loading branch information
Showing
7 changed files
with
175 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
src/main/java/com/webank/weid/suite/crypto/RsaCryptService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.webank.weid.suite.crypto; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.security.KeyFactory; | ||
import java.security.PrivateKey; | ||
import java.security.PublicKey; | ||
import java.security.spec.PKCS8EncodedKeySpec; | ||
import java.security.spec.X509EncodedKeySpec; | ||
|
||
import javax.crypto.Cipher; | ||
|
||
import org.bouncycastle.util.encoders.Base64; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import com.webank.weid.exception.EncodeSuiteException; | ||
import com.webank.weid.suite.entity.CryptType; | ||
|
||
public class RsaCryptService implements CryptService { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(RsaCryptService.class); | ||
|
||
private static final String KEY_ALGORITHM = CryptType.RSA.name(); | ||
|
||
@Override | ||
public String encrypt(String data, String publicKey) throws EncodeSuiteException { | ||
try { | ||
byte[] pubByte = Base64.decode(publicKey); | ||
PublicKey pub = KeyFactory.getInstance(KEY_ALGORITHM) | ||
.generatePublic(new X509EncodedKeySpec(pubByte)); | ||
Cipher cipher = Cipher.getInstance(KEY_ALGORITHM); | ||
cipher.init(Cipher.ENCRYPT_MODE, pub); | ||
byte[] encrypted = cipher.doFinal(data.getBytes(StandardCharsets.UTF_8)); | ||
return new String(Base64.encode(encrypted), StandardCharsets.UTF_8); | ||
} catch (Exception e) { | ||
logger.error("RSA encrypt error, please check the log.", e); | ||
throw new EncodeSuiteException(); | ||
} | ||
} | ||
|
||
@Override | ||
public String decrypt(String data, String privateKey) throws EncodeSuiteException { | ||
try { | ||
// 64位解码加密后的字符串 | ||
byte[] inputByte = Base64.decode(data.getBytes(StandardCharsets.UTF_8)); | ||
byte[] priByte = Base64.decode(privateKey); | ||
PrivateKey priKey = KeyFactory.getInstance(KEY_ALGORITHM) | ||
.generatePrivate(new PKCS8EncodedKeySpec(priByte)); | ||
// RSA解密 | ||
Cipher cipher = Cipher.getInstance(KEY_ALGORITHM); | ||
cipher.init(Cipher.DECRYPT_MODE, priKey); | ||
return new String(cipher.doFinal(inputByte), StandardCharsets.UTF_8); | ||
} catch (Exception e) { | ||
logger.error("RAS decrypt error, please check the log.", e); | ||
throw new EncodeSuiteException(); | ||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/main/java/com/webank/weid/suite/entity/Asymmetrickey.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* Copyright© (2018-2020) WeBank Co., Ltd. | ||
* | ||
* This file is part of weid-java-sdk. | ||
* | ||
* weid-java-sdk is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* weid-java-sdk is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with weid-java-sdk. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package com.webank.weid.suite.entity; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class Asymmetrickey { | ||
|
||
private String privavteKey; | ||
private String publicKey; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,5 +25,5 @@ | |
* | ||
*/ | ||
public enum CryptType { | ||
AES; | ||
AES, RSA; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.webank.weid.util; | ||
|
||
import java.security.NoSuchAlgorithmException; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import com.webank.weid.suite.crypto.CryptServiceFactory; | ||
import com.webank.weid.suite.crypto.KeyGenerator; | ||
import com.webank.weid.suite.entity.Asymmetrickey; | ||
import com.webank.weid.suite.entity.CryptType; | ||
|
||
public class TestCrypt { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(TestCrypt.class); | ||
|
||
@Test | ||
public void testAes() { | ||
String key = KeyGenerator.getKey(); | ||
logger.info("key: {}", key); | ||
String original = "{\"name\":\"zhangsan\",age:12}"; | ||
logger.info("original: {}", original); | ||
String encrypt = CryptServiceFactory.getCryptService(CryptType.AES).encrypt(original, key); | ||
logger.info("encrypt: {}", encrypt); | ||
String decrypt = CryptServiceFactory.getCryptService(CryptType.AES).decrypt(encrypt, key); | ||
logger.info("decrypt: {}", decrypt); | ||
Assert.assertEquals(original, decrypt); | ||
} | ||
|
||
@Test | ||
public void testRsa() throws NoSuchAlgorithmException { | ||
Asymmetrickey key = KeyGenerator.getKeyForRsa(); | ||
logger.info("pub key: {}", key.getPublicKey()); | ||
logger.info("pri key: {}", key.getPrivavteKey()); | ||
String original = "{\"name\":\"zhangsan\",age:12}"; | ||
logger.info("original: {}", original); | ||
String encrypt = CryptServiceFactory.getCryptService(CryptType.RSA) | ||
.encrypt(original, key.getPublicKey()); | ||
logger.info("encrypt: {}", encrypt); | ||
String decrypt = CryptServiceFactory.getCryptService(CryptType.RSA) | ||
.decrypt(encrypt, key.getPrivavteKey()); | ||
logger.info("decrypt: {}", decrypt); | ||
Assert.assertEquals(original, decrypt); | ||
} | ||
} |