Browse Source

providing junit test for symmetric encryption functions, including AESEncryptionFunction and SM4EncryptionFunction.

tags/1.0.0
zhanglin33 5 years ago
parent
commit
b83e59ad5f
13 changed files with 573 additions and 42 deletions
  1. +2
    -2
      source/crypto/crypto-classic/src/main/java/com/jd/blockchain/crypto/service/classic/AESEncryptionFunction.java
  2. +4
    -4
      source/crypto/crypto-classic/src/main/java/com/jd/blockchain/crypto/service/classic/ED25519SignatureFunction.java
  3. +12
    -12
      source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/asymmetric/AsymmtricCryptographyImplTest.java
  4. +268
    -0
      source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/service/classic/AESEncryptionFunctionTest.java
  5. +7
    -7
      source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/symmetric/SymmetricCryptographyImplTest.java
  6. +3
    -3
      source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/CryptoKeyType.java
  7. +2
    -2
      source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/PrivKey.java
  8. +2
    -2
      source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/PubKey.java
  9. +3
    -3
      source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/SymmetricKey.java
  10. +4
    -4
      source/crypto/crypto-sm/src/main/java/com/jd/blockchain/crypto/service/sm/SM2CryptoFunction.java
  11. +2
    -2
      source/crypto/crypto-sm/src/main/java/com/jd/blockchain/crypto/service/sm/SM4EncryptionFunction.java
  12. +263
    -0
      source/crypto/crypto-sm/src/test/java/test/com/jd/blockchain/crypto/service/sm/SM4EncryptionFunctionTest.java
  13. +1
    -1
      source/sdk/sdk-samples/src/main/java/com/jd/blockchain/sdk/samples/SDKDemo_User.java

+ 2
- 2
source/crypto/crypto-classic/src/main/java/com/jd/blockchain/crypto/service/classic/AESEncryptionFunction.java View File

@@ -2,7 +2,7 @@ package com.jd.blockchain.crypto.service.classic;


import static com.jd.blockchain.crypto.BaseCryptoKey.KEY_TYPE_BYTES; import static com.jd.blockchain.crypto.BaseCryptoKey.KEY_TYPE_BYTES;
import static com.jd.blockchain.crypto.CryptoBytes.ALGORYTHM_CODE_SIZE; import static com.jd.blockchain.crypto.CryptoBytes.ALGORYTHM_CODE_SIZE;
import static com.jd.blockchain.crypto.CryptoKeyType.SYMMETRIC_KEY;
import static com.jd.blockchain.crypto.CryptoKeyType.SYMMETRIC;


import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
@@ -184,7 +184,7 @@ public class AESEncryptionFunction implements SymmetricEncryptionFunction {
public boolean supportSymmetricKey(byte[] symmetricKeyBytes) { public boolean supportSymmetricKey(byte[] symmetricKeyBytes) {
// 验证输入字节数组长度=算法标识长度+密钥类型长度+密钥长度,字节数组的算法标识对应AES算法且密钥密钥类型是对称密钥 // 验证输入字节数组长度=算法标识长度+密钥类型长度+密钥长度,字节数组的算法标识对应AES算法且密钥密钥类型是对称密钥
return symmetricKeyBytes.length == SYMMETRICKEY_LENGTH && CryptoAlgorithm.match(AES, symmetricKeyBytes) return symmetricKeyBytes.length == SYMMETRICKEY_LENGTH && CryptoAlgorithm.match(AES, symmetricKeyBytes)
&& symmetricKeyBytes[ALGORYTHM_CODE_SIZE] == SYMMETRIC_KEY.CODE;
&& symmetricKeyBytes[ALGORYTHM_CODE_SIZE] == SYMMETRIC.CODE;
} }


@Override @Override


+ 4
- 4
source/crypto/crypto-classic/src/main/java/com/jd/blockchain/crypto/service/classic/ED25519SignatureFunction.java View File

@@ -2,8 +2,8 @@ package com.jd.blockchain.crypto.service.classic;


import static com.jd.blockchain.crypto.BaseCryptoKey.KEY_TYPE_BYTES; import static com.jd.blockchain.crypto.BaseCryptoKey.KEY_TYPE_BYTES;
import static com.jd.blockchain.crypto.CryptoBytes.ALGORYTHM_CODE_SIZE; import static com.jd.blockchain.crypto.CryptoBytes.ALGORYTHM_CODE_SIZE;
import static com.jd.blockchain.crypto.CryptoKeyType.PRIV_KEY;
import static com.jd.blockchain.crypto.CryptoKeyType.PUB_KEY;
import static com.jd.blockchain.crypto.CryptoKeyType.PRIVATE;
import static com.jd.blockchain.crypto.CryptoKeyType.PUBLIC;


import java.security.KeyPair; import java.security.KeyPair;


@@ -96,7 +96,7 @@ public class ED25519SignatureFunction implements SignatureFunction {
public boolean supportPrivKey(byte[] privKeyBytes) { public boolean supportPrivKey(byte[] privKeyBytes) {
// 验证输入字节数组长度=算法标识长度+密钥类型长度+密钥长度,密钥数据的算法标识对应ED25519签名算法,并且密钥类型是私钥 // 验证输入字节数组长度=算法标识长度+密钥类型长度+密钥长度,密钥数据的算法标识对应ED25519签名算法,并且密钥类型是私钥
return privKeyBytes.length == PRIVKEY_LENGTH && CryptoAlgorithm.match(ED25519, privKeyBytes) return privKeyBytes.length == PRIVKEY_LENGTH && CryptoAlgorithm.match(ED25519, privKeyBytes)
&& privKeyBytes[ALGORYTHM_CODE_SIZE] == PRIV_KEY.CODE;
&& privKeyBytes[ALGORYTHM_CODE_SIZE] == PRIVATE.CODE;
} }


@Override @Override
@@ -109,7 +109,7 @@ public class ED25519SignatureFunction implements SignatureFunction {
public boolean supportPubKey(byte[] pubKeyBytes) { public boolean supportPubKey(byte[] pubKeyBytes) {
// 验证输入字节数组长度=算法标识长度+密钥类型长度+密钥长度,密钥数据的算法标识对应ED25519签名算法,并且密钥类型是公钥 // 验证输入字节数组长度=算法标识长度+密钥类型长度+密钥长度,密钥数据的算法标识对应ED25519签名算法,并且密钥类型是公钥
return pubKeyBytes.length == PUBKEY_LENGTH && CryptoAlgorithm.match(ED25519, pubKeyBytes) return pubKeyBytes.length == PUBKEY_LENGTH && CryptoAlgorithm.match(ED25519, pubKeyBytes)
&& pubKeyBytes[ALGORYTHM_CODE_SIZE] == PUB_KEY.CODE;
&& pubKeyBytes[ALGORYTHM_CODE_SIZE] == PUBLIC.CODE;


} }




+ 12
- 12
source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/asymmetric/AsymmtricCryptographyImplTest.java View File

@@ -1,7 +1,7 @@
//package test.com.jd.blockchain.crypto.asymmetric; //package test.com.jd.blockchain.crypto.asymmetric;
// //
//import static com.jd.blockchain.crypto.CryptoKeyType.PRIV_KEY;
//import static com.jd.blockchain.crypto.CryptoKeyType.PUB_KEY;
//import static com.jd.blockchain.crypto.CryptoKeyType.PRIVATE;
//import static com.jd.blockchain.crypto.CryptoKeyType.PUBLIC;
//import static org.junit.Assert.assertArrayEquals; //import static org.junit.Assert.assertArrayEquals;
//import static org.junit.Assert.assertEquals; //import static org.junit.Assert.assertEquals;
//import static org.junit.Assert.assertNotNull; //import static org.junit.Assert.assertNotNull;
@@ -207,8 +207,8 @@
// assertEquals(expectedPubKeyLength,rawPubKeyBytes.length); // assertEquals(expectedPubKeyLength,rawPubKeyBytes.length);
// assertEquals(expectedPrivKeyLength,rawPrivKeyBytes.length); // assertEquals(expectedPrivKeyLength,rawPrivKeyBytes.length);
// //
// assertArrayEquals(BytesUtils.concat(new byte[]{algorithm.CODE},new byte[]{CryptoKeyType.PUB_KEY.CODE},rawPubKeyBytes), pubKeyBytes);
// assertArrayEquals(BytesUtils.concat(new byte[]{algorithm.CODE},new byte[]{CryptoKeyType.PRIV_KEY.CODE},rawPrivKeyBytes), privKeyBytes);
// assertArrayEquals(BytesUtils.concat(new byte[]{algorithm.CODE},new byte[]{CryptoKeyType.PUBLIC.CODE},rawPubKeyBytes), pubKeyBytes);
// assertArrayEquals(BytesUtils.concat(new byte[]{algorithm.CODE},new byte[]{CryptoKeyType.PRIVATE.CODE},rawPrivKeyBytes), privKeyBytes);
// //
// SignatureDigest signatureDigest = sf.sign(privKey,data); // SignatureDigest signatureDigest = sf.sign(privKey,data);
// byte[] rawDigest = signatureDigest.getRawDigest(); // byte[] rawDigest = signatureDigest.getRawDigest();
@@ -414,8 +414,8 @@
// assertEquals(expectedPubKeyLength,rawPubKeyBytes.length); // assertEquals(expectedPubKeyLength,rawPubKeyBytes.length);
// assertEquals(expectedPrivKeyLength,rawPrivKeyBytes.length); // assertEquals(expectedPrivKeyLength,rawPrivKeyBytes.length);
// //
// assertArrayEquals(BytesUtils.concat(new byte[]{algorithm.CODE},new byte[]{CryptoKeyType.PUB_KEY.CODE},rawPubKeyBytes), pubKeyBytes);
// assertArrayEquals(BytesUtils.concat(new byte[]{algorithm.CODE},new byte[]{CryptoKeyType.PRIV_KEY.CODE},rawPrivKeyBytes), privKeyBytes);
// assertArrayEquals(BytesUtils.concat(new byte[]{algorithm.CODE},new byte[]{CryptoKeyType.PUBLIC.CODE},rawPubKeyBytes), pubKeyBytes);
// assertArrayEquals(BytesUtils.concat(new byte[]{algorithm.CODE},new byte[]{CryptoKeyType.PRIVATE.CODE},rawPrivKeyBytes), privKeyBytes);
// //
// Ciphertext ciphertext = aef.encrypt(pubKey,data); // Ciphertext ciphertext = aef.encrypt(pubKey,data);
// byte[] rawCiphertextBytes = ciphertext.getRawCiphertext(); // byte[] rawCiphertextBytes = ciphertext.getRawCiphertext();
@@ -753,7 +753,7 @@
// verifyResolvePubKey(asymmetricCrypto,algorithm,32,pubKeyBytesWithWrongAlgCode,IllegalArgumentException.class); // verifyResolvePubKey(asymmetricCrypto,algorithm,32,pubKeyBytesWithWrongAlgCode,IllegalArgumentException.class);
// //
// byte[] pubKeyBytesWithWrongKeyType= pubKeyBytes; // byte[] pubKeyBytesWithWrongKeyType= pubKeyBytes;
// pubKeyBytesWithWrongKeyType[1] = PRIV_KEY.CODE;
// pubKeyBytesWithWrongKeyType[1] = PRIVATE.CODE;
// verifyResolvePubKey(asymmetricCrypto,algorithm,32,pubKeyBytesWithWrongKeyType,IllegalArgumentException.class); // verifyResolvePubKey(asymmetricCrypto,algorithm,32,pubKeyBytesWithWrongKeyType,IllegalArgumentException.class);
// //
// pubKeyBytes = null; // pubKeyBytes = null;
@@ -777,7 +777,7 @@
// verifyResolvePubKey(asymmetricCrypto,algorithm,65,pubKeyBytesWithWrongAlgCode,IllegalArgumentException.class); // verifyResolvePubKey(asymmetricCrypto,algorithm,65,pubKeyBytesWithWrongAlgCode,IllegalArgumentException.class);
// //
// pubKeyBytesWithWrongKeyType= pubKeyBytes; // pubKeyBytesWithWrongKeyType= pubKeyBytes;
// pubKeyBytesWithWrongKeyType[1] = PRIV_KEY.CODE;
// pubKeyBytesWithWrongKeyType[1] = PRIVATE.CODE;
// verifyResolvePubKey(asymmetricCrypto,algorithm,65,pubKeyBytesWithWrongKeyType,IllegalArgumentException.class); // verifyResolvePubKey(asymmetricCrypto,algorithm,65,pubKeyBytesWithWrongKeyType,IllegalArgumentException.class);
// //
// pubKeyBytes = null; // pubKeyBytes = null;
@@ -800,7 +800,7 @@
// verifyResolvePubKey(asymmetricCrypto,algorithm,32,pubKeyBytesWithWrongAlgCode,IllegalArgumentException.class); // verifyResolvePubKey(asymmetricCrypto,algorithm,32,pubKeyBytesWithWrongAlgCode,IllegalArgumentException.class);
// //
// pubKeyBytesWithWrongKeyType= pubKeyBytes; // pubKeyBytesWithWrongKeyType= pubKeyBytes;
// pubKeyBytesWithWrongKeyType[1] = PRIV_KEY.CODE;
// pubKeyBytesWithWrongKeyType[1] = PRIVATE.CODE;
// verifyResolvePubKey(asymmetricCrypto,algorithm,32,pubKeyBytesWithWrongKeyType,IllegalArgumentException.class); // verifyResolvePubKey(asymmetricCrypto,algorithm,32,pubKeyBytesWithWrongKeyType,IllegalArgumentException.class);
// //
// pubKeyBytes = null; // pubKeyBytes = null;
@@ -863,7 +863,7 @@
// verifyResolvePrivKey(asymmetricCrypto,algorithm,32,privKeyBytesWithWrongAlgCode,IllegalArgumentException.class); // verifyResolvePrivKey(asymmetricCrypto,algorithm,32,privKeyBytesWithWrongAlgCode,IllegalArgumentException.class);
// //
// byte[] privKeyBytesWithWrongKeyType = privKeyBytes; // byte[] privKeyBytesWithWrongKeyType = privKeyBytes;
// privKeyBytesWithWrongKeyType[1] = PUB_KEY.CODE;
// privKeyBytesWithWrongKeyType[1] = PUBLIC.CODE;
// verifyResolvePrivKey(asymmetricCrypto,algorithm,32,privKeyBytesWithWrongKeyType,IllegalArgumentException.class); // verifyResolvePrivKey(asymmetricCrypto,algorithm,32,privKeyBytesWithWrongKeyType,IllegalArgumentException.class);
// //
// privKeyBytes = null; // privKeyBytes = null;
@@ -887,7 +887,7 @@
// verifyResolvePrivKey(asymmetricCrypto,algorithm,32,privKeyBytesWithWrongAlgCode,IllegalArgumentException.class); // verifyResolvePrivKey(asymmetricCrypto,algorithm,32,privKeyBytesWithWrongAlgCode,IllegalArgumentException.class);
// //
// privKeyBytesWithWrongKeyType = privKeyBytes; // privKeyBytesWithWrongKeyType = privKeyBytes;
// privKeyBytesWithWrongKeyType[1] = PUB_KEY.CODE;
// privKeyBytesWithWrongKeyType[1] = PUBLIC.CODE;
// verifyResolvePrivKey(asymmetricCrypto,algorithm,32,privKeyBytesWithWrongKeyType,IllegalArgumentException.class); // verifyResolvePrivKey(asymmetricCrypto,algorithm,32,privKeyBytesWithWrongKeyType,IllegalArgumentException.class);
// //
// privKeyBytes = null; // privKeyBytes = null;
@@ -910,7 +910,7 @@
// verifyResolvePrivKey(asymmetricCrypto,algorithm,32,privKeyBytesWithWrongAlgCode,IllegalArgumentException.class); // verifyResolvePrivKey(asymmetricCrypto,algorithm,32,privKeyBytesWithWrongAlgCode,IllegalArgumentException.class);
// //
// privKeyBytesWithWrongKeyType = privKeyBytes; // privKeyBytesWithWrongKeyType = privKeyBytes;
// privKeyBytesWithWrongKeyType[1] = PUB_KEY.CODE;
// privKeyBytesWithWrongKeyType[1] = PUBLIC.CODE;
// verifyResolvePrivKey(asymmetricCrypto,algorithm,32,privKeyBytesWithWrongKeyType,IllegalArgumentException.class); // verifyResolvePrivKey(asymmetricCrypto,algorithm,32,privKeyBytesWithWrongKeyType,IllegalArgumentException.class);
// //
// privKeyBytes = null; // privKeyBytes = null;


+ 268
- 0
source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/service/classic/AESEncryptionFunctionTest.java View File

@@ -1,5 +1,20 @@
package test.com.jd.blockchain.crypto.service.classic; package test.com.jd.blockchain.crypto.service.classic;


import com.jd.blockchain.crypto.*;
import com.jd.blockchain.crypto.symmetric.SymmetricEncryptionFunction;
import com.jd.blockchain.utils.io.BytesUtils;
import org.junit.Test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Random;

import static com.jd.blockchain.crypto.CryptoKeyType.PUBLIC;
import static com.jd.blockchain.crypto.CryptoKeyType.SYMMETRIC;
import static org.junit.Assert.*;

/** /**
* @author zhanglin33 * @author zhanglin33
* @title: AESEncryptionFunctionTest * @title: AESEncryptionFunctionTest
@@ -7,4 +22,257 @@ package test.com.jd.blockchain.crypto.service.classic;
* @date 2019-04-01, 13:57 * @date 2019-04-01, 13:57
*/ */
public class AESEncryptionFunctionTest { public class AESEncryptionFunctionTest {

@Test
public void getAlgorithmTest(){
CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("aes");
assertNotNull(algorithm);

SymmetricEncryptionFunction symmetricEncryptionFunction =
CryptoServiceProviders.getSymmetricEncryptionFunction(algorithm);

assertEquals(symmetricEncryptionFunction.getAlgorithm().name(), algorithm.name());
assertEquals(symmetricEncryptionFunction.getAlgorithm().code(), algorithm.code());

algorithm = CryptoServiceProviders.getAlgorithm("AES");
assertNotNull(algorithm);

assertEquals(symmetricEncryptionFunction.getAlgorithm().name(), algorithm.name());
assertEquals(symmetricEncryptionFunction.getAlgorithm().code(), algorithm.code());

algorithm = CryptoServiceProviders.getAlgorithm("aess");
assertNull(algorithm);
}

@Test
public void generateSymmetricKeyTest(){
CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("aes");
assertNotNull(algorithm);

SymmetricEncryptionFunction symmetricEncryptionFunction =
CryptoServiceProviders.getSymmetricEncryptionFunction(algorithm);

SymmetricKey symmetricKey = (SymmetricKey) symmetricEncryptionFunction.generateSymmetricKey();

assertEquals(SYMMETRIC.CODE,symmetricKey.getKeyType().CODE);
assertEquals(128 / 8, symmetricKey.getRawKeyBytes().length);

assertEquals(algorithm.name(),symmetricKey.getAlgorithm().name());
assertEquals(algorithm.toString(),symmetricKey.getAlgorithm().toString());

assertEquals(2 + 1 + 128 / 8, symmetricKey.toBytes().length);
byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm);
byte[] keyTypeBytes = new byte[] {SYMMETRIC.CODE};
byte[] rawKeyBytes = symmetricKey.getRawKeyBytes();
assertArrayEquals(BytesUtils.concat(algoBytes,keyTypeBytes,rawKeyBytes),symmetricKey.toBytes());
}



@Test
public void encryptTest(){

byte[] data = new byte[1024];
Random random = new Random();
random.nextBytes(data);

CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("aes");
assertNotNull(algorithm);

SymmetricEncryptionFunction symmetricEncryptionFunction =
CryptoServiceProviders.getSymmetricEncryptionFunction(algorithm);

SymmetricKey symmetricKey = (SymmetricKey) symmetricEncryptionFunction.generateSymmetricKey();

Ciphertext ciphertext = symmetricEncryptionFunction.encrypt(symmetricKey,data);

byte[] ciphertextBytes = ciphertext.toBytes();
assertEquals(2 + 16 + 1024 , ciphertextBytes.length);
CryptoAlgorithm ciphertextAlgo = ciphertext.getAlgorithm();
assertEquals(algorithm.name(),symmetricKey.getAlgorithm().name());
assertEquals(algorithm.toString(),symmetricKey.getAlgorithm().toString());

byte[] algoBytes = CryptoAlgorithm.toBytes(ciphertextAlgo);
byte[] rawCiphertextBytes = ciphertext.getRawCiphertext();
assertArrayEquals(BytesUtils.concat(algoBytes,rawCiphertextBytes),ciphertextBytes);
}


@Test
public void decryptTest(){

byte[] data = new byte[1024];
Random random = new Random();
random.nextBytes(data);

CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("aes");
assertNotNull(algorithm);

SymmetricEncryptionFunction symmetricEncryptionFunction =
CryptoServiceProviders.getSymmetricEncryptionFunction(algorithm);

SymmetricKey symmetricKey = (SymmetricKey) symmetricEncryptionFunction.generateSymmetricKey();

Ciphertext ciphertext = symmetricEncryptionFunction.encrypt(symmetricKey,data);

byte[] decryptedPlaintext = symmetricEncryptionFunction.decrypt(symmetricKey,ciphertext);

assertArrayEquals(data,decryptedPlaintext);
}

// @Test
// public void streamEncryptTest(){
//
// byte[] data = new byte[1024];
// Random random = new Random();
// random.nextBytes(data);
//
//
// InputStream inputStream = new ByteArrayInputStream(data);
// OutputStream outputStream = new ByteArrayOutputStream();
//
// CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("aes");
// assertNotNull(algorithm);
//
// SymmetricEncryptionFunction symmetricEncryptionFunction =
// CryptoServiceProviders.getSymmetricEncryptionFunction(algorithm);
//
// SymmetricKey symmetricKey = (SymmetricKey) symmetricEncryptionFunction.generateSymmetricKey();
//
// symmetricEncryptionFunction.encrypt(symmetricKey,inputStream,outputStream);
//
// assertNotNull(outputStream);
//
//
// }



@Test
public void supportSymmetricKeyTest(){
CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("aes");
assertNotNull(algorithm);

SymmetricEncryptionFunction symmetricEncryptionFunction =
CryptoServiceProviders.getSymmetricEncryptionFunction(algorithm);

SymmetricKey symmetricKey = (SymmetricKey) symmetricEncryptionFunction.generateSymmetricKey();
byte[] symmetricKeyBytes = symmetricKey.toBytes();

assertTrue(symmetricEncryptionFunction.supportSymmetricKey(symmetricKeyBytes));

algorithm = CryptoServiceProviders.getAlgorithm("ripemd160");
assertNotNull(algorithm);
byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm);
byte[] pubKeyTypeBytes = new byte[] {PUBLIC.CODE};
byte[] rawKeyBytes = symmetricKey.getRawKeyBytes();
byte[] ripemd160KeyBytes = BytesUtils.concat(algoBytes,pubKeyTypeBytes,rawKeyBytes);

assertFalse(symmetricEncryptionFunction.supportSymmetricKey(ripemd160KeyBytes));
}

@Test
public void resolveSymmetricKeyTest(){
CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("aes");
assertNotNull(algorithm);

SymmetricEncryptionFunction symmetricEncryptionFunction =
CryptoServiceProviders.getSymmetricEncryptionFunction(algorithm);

SymmetricKey symmetricKey = (SymmetricKey) symmetricEncryptionFunction.generateSymmetricKey();
byte[] symmetricKeyBytes = symmetricKey.toBytes();

SymmetricKey resolvedKey = symmetricEncryptionFunction.resolveSymmetricKey(symmetricKeyBytes);

assertEquals(SYMMETRIC.CODE,resolvedKey.getKeyType().CODE);
assertEquals(128 / 8, resolvedKey.getRawKeyBytes().length);

assertEquals(algorithm.name(),resolvedKey.getAlgorithm().name());
assertEquals(algorithm.toString(),resolvedKey.getAlgorithm().toString());


algorithm = CryptoServiceProviders.getAlgorithm("ripemd160");
assertNotNull(algorithm);
byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm);
byte[] pubKeyTypeBytes = new byte[] {PUBLIC.CODE};
byte[] rawKeyBytes = symmetricKey.getRawKeyBytes();
byte[] ripemd160KeyBytes = BytesUtils.concat(algoBytes,pubKeyTypeBytes,rawKeyBytes);


Class<?> expectedException = CryptoException.class;
Exception actualEx = null;
try {
symmetricEncryptionFunction.resolveSymmetricKey(ripemd160KeyBytes);
} catch (Exception e) {
actualEx = e;
}
assertNotNull(actualEx);
assertTrue(expectedException.isAssignableFrom(actualEx.getClass()));
}

@Test
public void supportCiphertextTest(){

byte[] data = new byte[1024];
Random random = new Random();
random.nextBytes(data);

CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("aes");
assertNotNull(algorithm);

SymmetricEncryptionFunction symmetricEncryptionFunction =
CryptoServiceProviders.getSymmetricEncryptionFunction(algorithm);

SymmetricKey symmetricKey = (SymmetricKey) symmetricEncryptionFunction.generateSymmetricKey();

Ciphertext ciphertext = symmetricEncryptionFunction.encrypt(symmetricKey,data);

byte[] ciphertextBytes = ciphertext.toBytes();
assertTrue(symmetricEncryptionFunction.supportCiphertext(ciphertextBytes));

algorithm = CryptoServiceProviders.getAlgorithm("ripemd160");
assertNotNull(algorithm);
byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm);
byte[] rawCiphertextBytes = ciphertext.toBytes();
byte[] ripemd160CiphertextBytes = BytesUtils.concat(algoBytes,rawCiphertextBytes);

assertFalse(symmetricEncryptionFunction.supportCiphertext(ripemd160CiphertextBytes));
}

@Test
public void resolveCiphertextTest(){

byte[] data = new byte[1024];
Random random = new Random();
random.nextBytes(data);

CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("aes");
assertNotNull(algorithm);

SymmetricEncryptionFunction symmetricEncryptionFunction =
CryptoServiceProviders.getSymmetricEncryptionFunction(algorithm);

SymmetricKey symmetricKey = (SymmetricKey) symmetricEncryptionFunction.generateSymmetricKey();

Ciphertext ciphertext = symmetricEncryptionFunction.encrypt(symmetricKey,data);

byte[] ciphertextBytes = ciphertext.toBytes();
assertTrue(symmetricEncryptionFunction.supportCiphertext(ciphertextBytes));

algorithm = CryptoServiceProviders.getAlgorithm("ripemd160");
assertNotNull(algorithm);
byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm);
byte[] rawCiphertextBytes = ciphertext.toBytes();
byte[] ripemd160CiphertextBytes = BytesUtils.concat(algoBytes,rawCiphertextBytes);

Class<?> expectedException = CryptoException.class;
Exception actualEx = null;
try {
symmetricEncryptionFunction.resolveSymmetricKey(ripemd160CiphertextBytes);
} catch (Exception e) {
actualEx = e;
}
assertNotNull(actualEx);
assertTrue(expectedException.isAssignableFrom(actualEx.getClass()));
}
} }

+ 7
- 7
source/crypto/crypto-classic/src/test/java/test/com/jd/blockchain/crypto/symmetric/SymmetricCryptographyImplTest.java View File

@@ -16,8 +16,8 @@
//import java.io.OutputStream; //import java.io.OutputStream;
//import java.util.Random; //import java.util.Random;
// //
//import static com.jd.blockchain.crypto.CryptoKeyType.PRIV_KEY;
//import static com.jd.blockchain.crypto.CryptoKeyType.SYMMETRIC_KEY;
//import static com.jd.blockchain.crypto.CryptoKeyType.PRIVATE;
//import static com.jd.blockchain.crypto.CryptoKeyType.SYMMETRIC;
//import static org.junit.Assert.*; //import static org.junit.Assert.*;
// //
//public class SymmetricCryptographyImplTest { //public class SymmetricCryptographyImplTest {
@@ -147,7 +147,7 @@
// //验证SymmetricKey的getRawKeyBytes方法 // //验证SymmetricKey的getRawKeyBytes方法
// assertEquals(16, symmetricKey.getRawKeyBytes().length); // assertEquals(16, symmetricKey.getRawKeyBytes().length);
// //验证SymmetricKey的toBytes方法 // //验证SymmetricKey的toBytes方法
// assertArrayEquals(BytesUtils.concat(new byte[]{algorithm.CODE},new byte[]{SYMMETRIC_KEY.CODE},symmetricKey.getRawKeyBytes()), symmetricKey.toBytes());
// assertArrayEquals(BytesUtils.concat(new byte[]{algorithm.CODE},new byte[]{SYMMETRIC.CODE},symmetricKey.getRawKeyBytes()), symmetricKey.toBytes());
// //
// //
// Ciphertext ciphertext = sef.encrypt(symmetricKey,data); // Ciphertext ciphertext = sef.encrypt(symmetricKey,data);
@@ -393,7 +393,7 @@
// byte[] key = new byte[16]; // byte[] key = new byte[16];
// randomKey.nextBytes(key); // randomKey.nextBytes(key);
// //
// byte[] symmetricKeyBytes = BytesUtils.concat(new byte[]{algorithm.CODE},new byte[]{SYMMETRIC_KEY.CODE},key);
// byte[] symmetricKeyBytes = BytesUtils.concat(new byte[]{algorithm.CODE},new byte[]{SYMMETRIC.CODE},key);
// verifyResolveSymmetricKey(symmetricCrypto,algorithm,symmetricKeyBytes,null); // verifyResolveSymmetricKey(symmetricCrypto,algorithm,symmetricKeyBytes,null);
// //
// byte[] truncatedSymmetricKeyBytes = new byte[symmetricKeyBytes.length-2]; // byte[] truncatedSymmetricKeyBytes = new byte[symmetricKeyBytes.length-2];
@@ -406,7 +406,7 @@
// //
// byte[] symmetricKeyBytesWithWrongKeyType= symmetricKeyBytes; // byte[] symmetricKeyBytesWithWrongKeyType= symmetricKeyBytes;
// System.arraycopy(symmetricKeyBytes,0,symmetricKeyBytesWithWrongKeyType,0,symmetricKeyBytesWithWrongKeyType.length); // System.arraycopy(symmetricKeyBytes,0,symmetricKeyBytesWithWrongKeyType,0,symmetricKeyBytesWithWrongKeyType.length);
// symmetricKeyBytesWithWrongKeyType[1] = PRIV_KEY.CODE;
// symmetricKeyBytesWithWrongKeyType[1] = PRIVATE.CODE;
// verifyResolveSymmetricKey(symmetricCrypto,algorithm,symmetricKeyBytesWithWrongKeyType,IllegalArgumentException.class); // verifyResolveSymmetricKey(symmetricCrypto,algorithm,symmetricKeyBytesWithWrongKeyType,IllegalArgumentException.class);
// //
// symmetricKeyBytes = null; // symmetricKeyBytes = null;
@@ -415,7 +415,7 @@
// //
// //test SM4 // //test SM4
// algorithm = CryptoAlgorithm.SM4; // algorithm = CryptoAlgorithm.SM4;
// symmetricKeyBytes = BytesUtils.concat(new byte[]{algorithm.CODE},new byte[]{SYMMETRIC_KEY.CODE},key);
// symmetricKeyBytes = BytesUtils.concat(new byte[]{algorithm.CODE},new byte[]{SYMMETRIC.CODE},key);
// //
// verifyResolveSymmetricKey(symmetricCrypto,algorithm,symmetricKeyBytes,null); // verifyResolveSymmetricKey(symmetricCrypto,algorithm,symmetricKeyBytes,null);
// //
@@ -429,7 +429,7 @@
// //
// symmetricKeyBytesWithWrongKeyType= symmetricKeyBytes; // symmetricKeyBytesWithWrongKeyType= symmetricKeyBytes;
// System.arraycopy(symmetricKeyBytes,0,symmetricKeyBytesWithWrongKeyType,0,symmetricKeyBytesWithWrongKeyType.length); // System.arraycopy(symmetricKeyBytes,0,symmetricKeyBytesWithWrongKeyType,0,symmetricKeyBytesWithWrongKeyType.length);
// symmetricKeyBytesWithWrongKeyType[1] = PRIV_KEY.CODE;
// symmetricKeyBytesWithWrongKeyType[1] = PRIVATE.CODE;
// verifyResolveSymmetricKey(symmetricCrypto,algorithm,symmetricKeyBytesWithWrongKeyType,IllegalArgumentException.class); // verifyResolveSymmetricKey(symmetricCrypto,algorithm,symmetricKeyBytesWithWrongKeyType,IllegalArgumentException.class);
// //
// symmetricKeyBytes = null; // symmetricKeyBytes = null;


+ 3
- 3
source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/CryptoKeyType.java View File

@@ -5,17 +5,17 @@ public enum CryptoKeyType {
/** /**
* 非对称密钥的公钥 * 非对称密钥的公钥
*/ */
PUB_KEY((byte)0x01),
PUBLIC((byte)0x01),


/** /**
* 非对称密钥的私钥; * 非对称密钥的私钥;
*/ */
PRIV_KEY((byte)0x02),
PRIVATE((byte)0x02),


/** /**
* 对称密钥; * 对称密钥;
*/ */
SYMMETRIC_KEY((byte)0x03);
SYMMETRIC((byte)0x03);


public final byte CODE; public final byte CODE;




+ 2
- 2
source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/PrivKey.java View File

@@ -10,7 +10,7 @@ public class PrivKey extends BaseCryptoKey {
private static final long serialVersionUID = 6265440395252295646L; private static final long serialVersionUID = 6265440395252295646L;
public PrivKey(CryptoAlgorithm algorithm, byte[] rawCryptoBytes) { public PrivKey(CryptoAlgorithm algorithm, byte[] rawCryptoBytes) {
super(algorithm, rawCryptoBytes, CryptoKeyType.PRIV_KEY);
super(algorithm, rawCryptoBytes, CryptoKeyType.PRIVATE);
} }
public PrivKey(byte[] cryptoBytes) { public PrivKey(byte[] cryptoBytes) {
@@ -19,6 +19,6 @@ public class PrivKey extends BaseCryptoKey {
@Override @Override
public CryptoKeyType getKeyType() { public CryptoKeyType getKeyType() {
return CryptoKeyType.PRIV_KEY;
return CryptoKeyType.PRIVATE;
} }
} }

+ 2
- 2
source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/PubKey.java View File

@@ -11,7 +11,7 @@ public class PubKey extends BaseCryptoKey {
private static final long serialVersionUID = -2055071197736385328L; private static final long serialVersionUID = -2055071197736385328L;


public PubKey(CryptoAlgorithm algorithm, byte[] rawCryptoBytes) { public PubKey(CryptoAlgorithm algorithm, byte[] rawCryptoBytes) {
super(algorithm, rawCryptoBytes, CryptoKeyType.PUB_KEY);
super(algorithm, rawCryptoBytes, CryptoKeyType.PUBLIC);
} }


public PubKey(byte[] cryptoBytes) { public PubKey(byte[] cryptoBytes) {
@@ -20,6 +20,6 @@ public class PubKey extends BaseCryptoKey {
@Override @Override
public CryptoKeyType getKeyType() { public CryptoKeyType getKeyType() {
return CryptoKeyType.PUB_KEY;
return CryptoKeyType.PUBLIC;
} }
} }

+ 3
- 3
source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/SymmetricKey.java View File

@@ -1,6 +1,6 @@
package com.jd.blockchain.crypto; package com.jd.blockchain.crypto;


import static com.jd.blockchain.crypto.CryptoKeyType.SYMMETRIC_KEY;
import static com.jd.blockchain.crypto.CryptoKeyType.SYMMETRIC;


/** /**
* 单密钥; * 单密钥;
@@ -13,7 +13,7 @@ public class SymmetricKey extends BaseCryptoKey {
private static final long serialVersionUID = 5055547663903904933L; private static final long serialVersionUID = 5055547663903904933L;


public SymmetricKey(CryptoAlgorithm algorithm, byte[] rawKeyBytes) { public SymmetricKey(CryptoAlgorithm algorithm, byte[] rawKeyBytes) {
super(algorithm, rawKeyBytes, SYMMETRIC_KEY);
super(algorithm, rawKeyBytes, SYMMETRIC);
} }


public SymmetricKey(byte[] keyBytes) { public SymmetricKey(byte[] keyBytes) {
@@ -22,7 +22,7 @@ public class SymmetricKey extends BaseCryptoKey {


@Override @Override
public CryptoKeyType getKeyType() { public CryptoKeyType getKeyType() {
return SYMMETRIC_KEY;
return SYMMETRIC;
} }


} }

+ 4
- 4
source/crypto/crypto-sm/src/main/java/com/jd/blockchain/crypto/service/sm/SM2CryptoFunction.java View File

@@ -2,8 +2,8 @@ package com.jd.blockchain.crypto.service.sm;


import static com.jd.blockchain.crypto.BaseCryptoKey.KEY_TYPE_BYTES; import static com.jd.blockchain.crypto.BaseCryptoKey.KEY_TYPE_BYTES;
import static com.jd.blockchain.crypto.CryptoBytes.ALGORYTHM_CODE_SIZE; import static com.jd.blockchain.crypto.CryptoBytes.ALGORYTHM_CODE_SIZE;
import static com.jd.blockchain.crypto.CryptoKeyType.PRIV_KEY;
import static com.jd.blockchain.crypto.CryptoKeyType.PUB_KEY;
import static com.jd.blockchain.crypto.CryptoKeyType.PRIVATE;
import static com.jd.blockchain.crypto.CryptoKeyType.PUBLIC;


import com.jd.blockchain.crypto.*; import com.jd.blockchain.crypto.*;
import org.bouncycastle.crypto.AsymmetricCipherKeyPair; import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
@@ -90,7 +90,7 @@ public class SM2CryptoFunction implements AsymmetricEncryptionFunction, Signatur
public boolean supportPrivKey(byte[] privKeyBytes) { public boolean supportPrivKey(byte[] privKeyBytes) {
// 验证输入字节数组长度=算法标识长度+密钥类型长度+密钥长度,密钥数据的算法标识对应SM2算法,并且密钥类型是私钥 // 验证输入字节数组长度=算法标识长度+密钥类型长度+密钥长度,密钥数据的算法标识对应SM2算法,并且密钥类型是私钥
return privKeyBytes.length == PRIVKEY_LENGTH && CryptoAlgorithm.match(SM2, privKeyBytes) return privKeyBytes.length == PRIVKEY_LENGTH && CryptoAlgorithm.match(SM2, privKeyBytes)
&& privKeyBytes[ALGORYTHM_CODE_SIZE] == PRIV_KEY.CODE;
&& privKeyBytes[ALGORYTHM_CODE_SIZE] == PRIVATE.CODE;
} }


@Override @Override
@@ -103,7 +103,7 @@ public class SM2CryptoFunction implements AsymmetricEncryptionFunction, Signatur
public boolean supportPubKey(byte[] pubKeyBytes) { public boolean supportPubKey(byte[] pubKeyBytes) {
// 验证输入字节数组长度=算法标识长度+密钥类型长度+椭圆曲线点长度,密钥数据的算法标识对应SM2算法,并且密钥类型是公钥 // 验证输入字节数组长度=算法标识长度+密钥类型长度+椭圆曲线点长度,密钥数据的算法标识对应SM2算法,并且密钥类型是公钥
return pubKeyBytes.length == PUBKEY_LENGTH && CryptoAlgorithm.match(SM2, pubKeyBytes) return pubKeyBytes.length == PUBKEY_LENGTH && CryptoAlgorithm.match(SM2, pubKeyBytes)
&& pubKeyBytes[ALGORYTHM_CODE_SIZE] == PUB_KEY.CODE;
&& pubKeyBytes[ALGORYTHM_CODE_SIZE] == PUBLIC.CODE;
} }


@Override @Override


+ 2
- 2
source/crypto/crypto-sm/src/main/java/com/jd/blockchain/crypto/service/sm/SM4EncryptionFunction.java View File

@@ -2,7 +2,7 @@ package com.jd.blockchain.crypto.service.sm;


import static com.jd.blockchain.crypto.BaseCryptoKey.KEY_TYPE_BYTES; import static com.jd.blockchain.crypto.BaseCryptoKey.KEY_TYPE_BYTES;
import static com.jd.blockchain.crypto.CryptoBytes.ALGORYTHM_CODE_SIZE; import static com.jd.blockchain.crypto.CryptoBytes.ALGORYTHM_CODE_SIZE;
import static com.jd.blockchain.crypto.CryptoKeyType.SYMMETRIC_KEY;
import static com.jd.blockchain.crypto.CryptoKeyType.SYMMETRIC;


import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
@@ -172,7 +172,7 @@ public class SM4EncryptionFunction implements SymmetricEncryptionFunction {
public boolean supportSymmetricKey(byte[] symmetricKeyBytes) { public boolean supportSymmetricKey(byte[] symmetricKeyBytes) {
// 验证输入字节数组长度=算法标识长度+密钥类型长度+密钥长度,字节数组的算法标识对应SM4算法且密钥密钥类型是对称密钥 // 验证输入字节数组长度=算法标识长度+密钥类型长度+密钥长度,字节数组的算法标识对应SM4算法且密钥密钥类型是对称密钥
return symmetricKeyBytes.length == SYMMETRICKEY_LENGTH && CryptoAlgorithm.match(SM4, symmetricKeyBytes) return symmetricKeyBytes.length == SYMMETRICKEY_LENGTH && CryptoAlgorithm.match(SM4, symmetricKeyBytes)
&& symmetricKeyBytes[ALGORYTHM_CODE_SIZE] == SYMMETRIC_KEY.CODE;
&& symmetricKeyBytes[ALGORYTHM_CODE_SIZE] == SYMMETRIC.CODE;
} }


@Override @Override


+ 263
- 0
source/crypto/crypto-sm/src/test/java/test/com/jd/blockchain/crypto/service/sm/SM4EncryptionFunctionTest.java View File

@@ -1,5 +1,16 @@
package test.com.jd.blockchain.crypto.service.sm; package test.com.jd.blockchain.crypto.service.sm;


import com.jd.blockchain.crypto.*;
import com.jd.blockchain.crypto.symmetric.SymmetricEncryptionFunction;
import com.jd.blockchain.utils.io.BytesUtils;
import org.junit.Test;

import java.util.Random;

import static com.jd.blockchain.crypto.CryptoKeyType.PUBLIC;
import static com.jd.blockchain.crypto.CryptoKeyType.SYMMETRIC;
import static org.junit.Assert.*;

/** /**
* @author zhanglin33 * @author zhanglin33
* @title: SM4EncryptionFunctionTest * @title: SM4EncryptionFunctionTest
@@ -7,4 +18,256 @@ package test.com.jd.blockchain.crypto.service.sm;
* @date 2019-04-03, 16:35 * @date 2019-04-03, 16:35
*/ */
public class SM4EncryptionFunctionTest { public class SM4EncryptionFunctionTest {
@Test
public void getAlgorithmTest(){
CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sm4");
assertNotNull(algorithm);

SymmetricEncryptionFunction symmetricEncryptionFunction =
CryptoServiceProviders.getSymmetricEncryptionFunction(algorithm);

assertEquals(symmetricEncryptionFunction.getAlgorithm().name(), algorithm.name());
assertEquals(symmetricEncryptionFunction.getAlgorithm().code(), algorithm.code());

algorithm = CryptoServiceProviders.getAlgorithm("sM4");
assertNotNull(algorithm);

assertEquals(symmetricEncryptionFunction.getAlgorithm().name(), algorithm.name());
assertEquals(symmetricEncryptionFunction.getAlgorithm().code(), algorithm.code());

algorithm = CryptoServiceProviders.getAlgorithm("smm4");
assertNull(algorithm);
}

@Test
public void generateSymmetricKeyTest(){
CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sm4");
assertNotNull(algorithm);

SymmetricEncryptionFunction symmetricEncryptionFunction =
CryptoServiceProviders.getSymmetricEncryptionFunction(algorithm);

SymmetricKey symmetricKey = (SymmetricKey) symmetricEncryptionFunction.generateSymmetricKey();

assertEquals(SYMMETRIC.CODE,symmetricKey.getKeyType().CODE);
assertEquals(128 / 8, symmetricKey.getRawKeyBytes().length);

assertEquals(algorithm.name(),symmetricKey.getAlgorithm().name());
assertEquals(algorithm.toString(),symmetricKey.getAlgorithm().toString());

assertEquals(2 + 1 + 128 / 8, symmetricKey.toBytes().length);
byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm);
byte[] keyTypeBytes = new byte[] {SYMMETRIC.CODE};
byte[] rawKeyBytes = symmetricKey.getRawKeyBytes();
assertArrayEquals(BytesUtils.concat(algoBytes,keyTypeBytes,rawKeyBytes),symmetricKey.toBytes());
}



@Test
public void encryptTest(){

byte[] data = new byte[1024];
Random random = new Random();
random.nextBytes(data);

CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sm4");
assertNotNull(algorithm);

SymmetricEncryptionFunction symmetricEncryptionFunction =
CryptoServiceProviders.getSymmetricEncryptionFunction(algorithm);

SymmetricKey symmetricKey = (SymmetricKey) symmetricEncryptionFunction.generateSymmetricKey();

Ciphertext ciphertext = symmetricEncryptionFunction.encrypt(symmetricKey,data);

byte[] ciphertextBytes = ciphertext.toBytes();
assertEquals(2 + 16 +16 + 1024 , ciphertextBytes.length);
CryptoAlgorithm ciphertextAlgo = ciphertext.getAlgorithm();
assertEquals(algorithm.name(),symmetricKey.getAlgorithm().name());
assertEquals(algorithm.toString(),symmetricKey.getAlgorithm().toString());

byte[] algoBytes = CryptoAlgorithm.toBytes(ciphertextAlgo);
byte[] rawCiphertextBytes = ciphertext.getRawCiphertext();
assertArrayEquals(BytesUtils.concat(algoBytes,rawCiphertextBytes),ciphertextBytes);
}


@Test
public void decryptTest(){

byte[] data = new byte[1024];
Random random = new Random();
random.nextBytes(data);

CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sm4");
assertNotNull(algorithm);

SymmetricEncryptionFunction symmetricEncryptionFunction =
CryptoServiceProviders.getSymmetricEncryptionFunction(algorithm);

SymmetricKey symmetricKey = (SymmetricKey) symmetricEncryptionFunction.generateSymmetricKey();

Ciphertext ciphertext = symmetricEncryptionFunction.encrypt(symmetricKey,data);

byte[] decryptedPlaintext = symmetricEncryptionFunction.decrypt(symmetricKey,ciphertext);

assertArrayEquals(data,decryptedPlaintext);
}

// @Test
// public void streamEncryptTest(){
//
// byte[] data = new byte[1024];
// Random random = new Random();
// random.nextBytes(data);
//
//
// InputStream inputStream = new ByteArrayInputStream(data);
// OutputStream outputStream = new ByteArrayOutputStream();
//
// CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("aes");
// assertNotNull(algorithm);
//
// SymmetricEncryptionFunction symmetricEncryptionFunction =
// CryptoServiceProviders.getSymmetricEncryptionFunction(algorithm);
//
// SymmetricKey symmetricKey = (SymmetricKey) symmetricEncryptionFunction.generateSymmetricKey();
//
// symmetricEncryptionFunction.encrypt(symmetricKey,inputStream,outputStream);
//
// assertNotNull(outputStream);
//
//
// }



@Test
public void supportSymmetricKeyTest(){
CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sm4");
assertNotNull(algorithm);

SymmetricEncryptionFunction symmetricEncryptionFunction =
CryptoServiceProviders.getSymmetricEncryptionFunction(algorithm);

SymmetricKey symmetricKey = (SymmetricKey) symmetricEncryptionFunction.generateSymmetricKey();
byte[] symmetricKeyBytes = symmetricKey.toBytes();

assertTrue(symmetricEncryptionFunction.supportSymmetricKey(symmetricKeyBytes));

algorithm = CryptoServiceProviders.getAlgorithm("sm3");
assertNotNull(algorithm);
byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm);
byte[] pubKeyTypeBytes = new byte[] {PUBLIC.CODE};
byte[] rawKeyBytes = symmetricKey.getRawKeyBytes();
byte[] ripemd160KeyBytes = BytesUtils.concat(algoBytes,pubKeyTypeBytes,rawKeyBytes);

assertFalse(symmetricEncryptionFunction.supportSymmetricKey(ripemd160KeyBytes));
}

@Test
public void resolveSymmetricKeyTest(){
CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sm4");
assertNotNull(algorithm);

SymmetricEncryptionFunction symmetricEncryptionFunction =
CryptoServiceProviders.getSymmetricEncryptionFunction(algorithm);

SymmetricKey symmetricKey = (SymmetricKey) symmetricEncryptionFunction.generateSymmetricKey();
byte[] symmetricKeyBytes = symmetricKey.toBytes();

SymmetricKey resolvedKey = symmetricEncryptionFunction.resolveSymmetricKey(symmetricKeyBytes);

assertEquals(SYMMETRIC.CODE,resolvedKey.getKeyType().CODE);
assertEquals(128 / 8, resolvedKey.getRawKeyBytes().length);

assertEquals(algorithm.name(),resolvedKey.getAlgorithm().name());
assertEquals(algorithm.toString(),resolvedKey.getAlgorithm().toString());


algorithm = CryptoServiceProviders.getAlgorithm("sm3");
assertNotNull(algorithm);
byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm);
byte[] pubKeyTypeBytes = new byte[] {PUBLIC.CODE};
byte[] rawKeyBytes = symmetricKey.getRawKeyBytes();
byte[] ripemd160KeyBytes = BytesUtils.concat(algoBytes,pubKeyTypeBytes,rawKeyBytes);


Class<?> expectedException = CryptoException.class;
Exception actualEx = null;
try {
symmetricEncryptionFunction.resolveSymmetricKey(ripemd160KeyBytes);
} catch (Exception e) {
actualEx = e;
}
assertNotNull(actualEx);
assertTrue(expectedException.isAssignableFrom(actualEx.getClass()));
}

@Test
public void supportCiphertextTest(){

byte[] data = new byte[1024];
Random random = new Random();
random.nextBytes(data);

CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sm4");
assertNotNull(algorithm);

SymmetricEncryptionFunction symmetricEncryptionFunction =
CryptoServiceProviders.getSymmetricEncryptionFunction(algorithm);

SymmetricKey symmetricKey = (SymmetricKey) symmetricEncryptionFunction.generateSymmetricKey();

Ciphertext ciphertext = symmetricEncryptionFunction.encrypt(symmetricKey,data);

byte[] ciphertextBytes = ciphertext.toBytes();
assertTrue(symmetricEncryptionFunction.supportCiphertext(ciphertextBytes));

algorithm = CryptoServiceProviders.getAlgorithm("sm3");
assertNotNull(algorithm);
byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm);
byte[] rawCiphertextBytes = ciphertext.toBytes();
byte[] ripemd160CiphertextBytes = BytesUtils.concat(algoBytes,rawCiphertextBytes);

assertFalse(symmetricEncryptionFunction.supportCiphertext(ripemd160CiphertextBytes));
}

@Test
public void resolveCiphertextTest(){

byte[] data = new byte[1024];
Random random = new Random();
random.nextBytes(data);

CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm("sm4");
assertNotNull(algorithm);

SymmetricEncryptionFunction symmetricEncryptionFunction =
CryptoServiceProviders.getSymmetricEncryptionFunction(algorithm);

SymmetricKey symmetricKey = (SymmetricKey) symmetricEncryptionFunction.generateSymmetricKey();

Ciphertext ciphertext = symmetricEncryptionFunction.encrypt(symmetricKey,data);

byte[] ciphertextBytes = ciphertext.toBytes();
assertTrue(symmetricEncryptionFunction.supportCiphertext(ciphertextBytes));

algorithm = CryptoServiceProviders.getAlgorithm("sm3");
assertNotNull(algorithm);
byte[] algoBytes = CryptoAlgorithm.toBytes(algorithm);
byte[] rawCiphertextBytes = ciphertext.toBytes();
byte[] ripemd160CiphertextBytes = BytesUtils.concat(algoBytes,rawCiphertextBytes);

Class<?> expectedException = CryptoException.class;
Exception actualEx = null;
try {
symmetricEncryptionFunction.resolveSymmetricKey(ripemd160CiphertextBytes);
} catch (Exception e) {
actualEx = e;
}
assertNotNull(actualEx);
assertTrue(expectedException.isAssignableFrom(actualEx.getClass()));
}
} }

+ 1
- 1
source/sdk/sdk-samples/src/main/java/com/jd/blockchain/sdk/samples/SDKDemo_User.java View File

@@ -65,7 +65,7 @@ public class SDKDemo_User {


// 在本地产生要注册的账户的秘钥; // 在本地产生要注册的账户的秘钥;
//BlockchainKeyGenerator generator = BlockchainKeyGenerator.getInstance(); //BlockchainKeyGenerator generator = BlockchainKeyGenerator.getInstance();
//BlockchainKeyPair user = generator.generate(CryptoKeyType.PUB_KEY);
//BlockchainKeyPair user = generator.generate(CryptoKeyType.PUBLIC);


SignatureFunction signatureFunction = asymmetricCryptography.getSignatureFunction(CryptoAlgorithm.ED25519); SignatureFunction signatureFunction = asymmetricCryptography.getSignatureFunction(CryptoAlgorithm.ED25519);
CryptoKeyPair cryptoKeyPair = signatureFunction.generateKeyPair(); CryptoKeyPair cryptoKeyPair = signatureFunction.generateKeyPair();


Loading…
Cancel
Save