Skip to content

Commit dc2eff3

Browse files
author
liuchen
committed
modify asymDecrypt in C cd /Users/lascion/lcworkspace/gitrepo/java-chainsql-api ; /usr/bin/env /Library/Java/JavaVirtualMachines/jdk1.8.0_321.jdk/Contents/Home/bin/java -agentlib:jdwp=transport=dt_socket,server=n,suspend=y,address=localhost:60798 -cp /var/folders/5q/_0kvhxls1px1q_61zmw0rcth0000gn/T/cp_ehqfkhjnzhnajw7bysrct33ux.jar com.peersafe.example.chainsql.TestChainsql hainsql.java
1 parent 0587181 commit dc2eff3

4 files changed

Lines changed: 53 additions & 33 deletions

File tree

chainsql/src/main/java/com/peersafe/base/client/Client.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.util.logging.Level;
2020
import java.util.logging.Logger;
2121

22-
import com.peersafe.chainsql.crypto.EncryptCommon;
2322
import com.peersafe.chainsql.manager.CallbackManager;
2423
import org.json.JSONArray;
2524
import org.json.JSONException;
@@ -2189,7 +2188,6 @@ public JSONObject getTransaction(String hash) {
21892188
return getResult(request);
21902189
}
21912190

2192-
21932191
/**
21942192
* Request for a transaction's information.
21952193
* @param hash Transaction hash.

chainsql/src/main/java/com/peersafe/base/utils/Utils.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.peersafe.base.encodings.common.B16;
99
import org.bouncycastle.crypto.digests.RIPEMD160Digest;
1010
import org.bouncycastle.crypto.digests.SHA256Digest;
11+
import com.peersafe.chainsql.util.Define.algType;
1112

1213
import static com.peersafe.base.config.Config.getB58IdentiferCodecs;
1314

@@ -66,17 +67,18 @@ public static String deriveAddressFromBytes(byte[] pubBytes){
6667
}
6768

6869

69-
public static String getAlgType(String secret){
70-
70+
public static algType getAlgType(String secret){
71+
if(secret.isEmpty()) {
72+
return algType.unknown;
73+
}
7174
String regEx = "^[a-zA-Z1-9]{51,51}";
7275
Pattern pattern = Pattern.compile(regEx);
7376
Matcher matcher = pattern.matcher(secret);
7477

7578
if(matcher.matches()){
76-
77-
return "softGMAlg";
79+
return algType.gmalg;
7880
}else{
79-
return "secp256k1";
81+
return algType.secp256k1;
8082
}
8183
}
8284
}

chainsql/src/main/java/com/peersafe/chainsql/core/Chainsql.java

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
import java.util.List;
1010
import java.util.logging.Level;
1111
import java.util.logging.Logger;
12-
import java.util.regex.Matcher;
13-
import java.util.regex.Pattern;
1412

1513
import com.peersafe.chainsql.pool.ChainsqlPool;
1614
import org.bouncycastle.crypto.digests.RIPEMD160Digest;
@@ -42,6 +40,7 @@
4240
import com.peersafe.chainsql.util.GenericPair;
4341
import com.peersafe.chainsql.util.Util;
4442
import com.peersafe.chainsql.util.Validate;
43+
import com.peersafe.chainsql.util.Define;
4544

4645
public class Chainsql extends Submit {
4746

@@ -960,19 +959,7 @@ public Chainsql grant(String name, String user,String userPublicKey,String flag)
960959
String newToken = "";
961960
if(token.length() != 0){
962961
try {
963-
byte[] seedBytes = null;
964-
965-
boolean bSoftGM = Utils.getAlgType(this.connection.secret).equals("softGMAlg");
966-
if(!this.connection.secret.isEmpty()){
967-
968-
if(bSoftGM){
969-
seedBytes = getB58IdentiferCodecs().decodeAccountPrivate(this.connection.secret);
970-
}else{
971-
seedBytes = getB58IdentiferCodecs().decodeFamilySeed(this.connection.secret);
972-
}
973-
974-
}
975-
byte[] password = EncryptCommon.asymDecrypt(Util.hexToBytes(token), seedBytes,bSoftGM) ;
962+
byte[] password = this.asymDecrypt(token, this.connection.secret).getBytes();
976963
if(password == null){
977964
return null;
978965
}
@@ -1434,7 +1421,7 @@ public JSONObject getExplicitTransaction(String hash, String privateKey){
14341421
try {
14351422
//token = Util.getUserToken(this.connection, this.connection.address, name);
14361423
if(token != "") {
1437-
String password = this.asymDecrypt(token, privateKey, false);
1424+
String password = this.asymDecrypt(token, privateKey);
14381425
Util.decryptData(password.getBytes(), tx);
14391426
}
14401427
} catch (Exception e) {
@@ -1856,20 +1843,42 @@ public String asymEncrypt(String plainText, String publicKey, boolean bSM) {
18561843
public String asymDecrypt(String cipher, String privateKey, boolean bSM) {
18571844
byte[] cipherBytes = Util.hexToBytes(cipher);
18581845
byte[] seedBytes = null;
1859-
1860-
bSM = Utils.getAlgType(connection.secret).equals("softGMAlg");
1861-
if(bSM){
1862-
seedBytes = getB58IdentiferCodecs().decodeAccountPrivate(privateKey);
1863-
bSM = true;
1864-
}
1865-
else{
1866-
seedBytes = getB58IdentiferCodecs().decodeFamilySeed(privateKey);
1867-
bSM = false;
1868-
}
1846+
1847+
if(bSM) {
1848+
seedBytes = getB58IdentiferCodecs().decodeAccountPrivate(privateKey);
1849+
}else {
1850+
seedBytes = getB58IdentiferCodecs().decodeFamilySeed(privateKey);
1851+
}
18691852

18701853
byte[] newBytes = EncryptCommon.asymDecrypt(cipherBytes, seedBytes, bSM);
18711854
return new String(newBytes);
18721855
}
1856+
1857+
/**
1858+
* 非对称解密接口
1859+
* @param cipher 密文
1860+
* @param privateKey 加密密钥
1861+
* @param bSM 是否使用国密算法。如果使用国密算法,注意密钥是16位。
1862+
* @return 明文,解密失败返回""
1863+
*/
1864+
public String asymDecrypt(String cipher, String privateKey) {
1865+
byte[] cipherBytes = Util.hexToBytes(cipher);
1866+
byte[] seedBytes = null;
1867+
Define.algType priAlgType = Utils.getAlgType(privateKey);
1868+
switch(priAlgType) {
1869+
case gmalg:
1870+
seedBytes = getB58IdentiferCodecs().decodeAccountPrivate(privateKey);
1871+
break;
1872+
case secp256k1:
1873+
seedBytes = getB58IdentiferCodecs().decodeFamilySeed(privateKey);
1874+
break;
1875+
default:
1876+
return new String("");
1877+
}
1878+
byte[] plainBytes = EncryptCommon.asymDecrypt(cipherBytes, seedBytes,
1879+
priAlgType.equals(Define.algType.gmalg));
1880+
return new String(plainBytes);
1881+
}
18731882

18741883

18751884
/**
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.peersafe.chainsql.util;
2+
3+
public class Define {
4+
public static enum algType {
5+
unknown,
6+
gmalg,
7+
secp256k1,
8+
ed25519,
9+
hdGmAlg;
10+
}
11+
}

0 commit comments

Comments
 (0)