Skip to content

Commit f74d7fc

Browse files
author
liuchen
committed
update code for asymDec
1 parent dc2eff3 commit f74d7fc

2 files changed

Lines changed: 30 additions & 34 deletions

File tree

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

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -722,8 +722,7 @@ private Chainsql createTable(String name, List<String> rawList, JSONObject opera
722722
String strRaw = listRaw.toString();
723723
String token = "";
724724
if(confidential){
725-
726-
boolean bSM = ( Utils.getAlgType(this.connection.secret).equals("softGMAlg") );
725+
boolean bSM = Utils.getAlgType(this.connection.secret).equals(Define.algType.gmalg);
727726
int randomSize = bSM? PASSWORD_LENGTH /2 :PASSWORD_LENGTH ;
728727

729728
byte[] password = Util.getRandomBytes(randomSize);
@@ -1858,26 +1857,10 @@ public String asymDecrypt(String cipher, String privateKey, boolean bSM) {
18581857
* 非对称解密接口
18591858
* @param cipher 密文
18601859
* @param privateKey 加密密钥
1861-
* @param bSM 是否使用国密算法。如果使用国密算法,注意密钥是16位。
18621860
* @return 明文,解密失败返回""
18631861
*/
18641862
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);
1863+
return Util.asymDec(cipher, privateKey);
18811864
}
18821865

18831866

chainsql/src/main/java/com/peersafe/chainsql/util/Util.java

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -473,29 +473,42 @@ public static String encryptRaw(Connection connection,String token,String strRaw
473473
return strRaw;
474474
}
475475
try {
476-
byte[] seedBytes = null;
477-
478-
boolean bSoftGM = Utils.getAlgType(connection.secret).equals("softGMAlg");
479-
if(!connection.secret.isEmpty()){
480-
481-
if(bSoftGM){
482-
seedBytes = getB58IdentiferCodecs().decodeAccountPrivate(connection.secret);
483-
}else{
484-
seedBytes = getB58IdentiferCodecs().decodeFamilySeed(connection.secret);
485-
}
486-
487-
}
488-
489-
byte[] password = EncryptCommon.asymDecrypt(Util.hexToBytes(token), seedBytes,bSoftGM) ;
476+
String password = asymDec(token, connection.secret);
477+
boolean bSoftGM = Utils.getAlgType(connection.secret).equals(Define.algType.gmalg);
490478
if(password == null){
491479
throw new Exception("Exception: decrypt token failed");
492480
}
493-
byte[] rawBytes = EncryptCommon.symEncrypt( strRaw.getBytes(),password,bSoftGM);
481+
byte[] rawBytes = EncryptCommon.symEncrypt( strRaw.getBytes(), password.getBytes(), bSoftGM);
494482
strRaw = Util.bytesToHex(rawBytes);
495483
return strRaw;
496484
} catch (Exception e) {
497485
e.printStackTrace();
498486
return strRaw;
499487
}
500488
}
489+
490+
/**
491+
* 非对称解密接口
492+
* @param cipher 密文
493+
* @param privateKey 加密密钥
494+
* @return 明文,解密失败返回""
495+
*/
496+
public static String asymDec(String cipher, String privateKey) {
497+
byte[] cipherBytes = Util.hexToBytes(cipher);
498+
byte[] seedBytes = null;
499+
Define.algType priAlgType = Utils.getAlgType(privateKey);
500+
switch(priAlgType) {
501+
case gmalg:
502+
seedBytes = getB58IdentiferCodecs().decodeAccountPrivate(privateKey);
503+
break;
504+
case secp256k1:
505+
seedBytes = getB58IdentiferCodecs().decodeFamilySeed(privateKey);
506+
break;
507+
default:
508+
return new String("");
509+
}
510+
byte[] plainBytes = EncryptCommon.asymDecrypt(cipherBytes, seedBytes,
511+
priAlgType.equals(Define.algType.gmalg));
512+
return new String(plainBytes);
513+
}
501514
}

0 commit comments

Comments
 (0)