@@ -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.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.InputStream; | |||
@@ -184,7 +184,7 @@ public class AESEncryptionFunction implements SymmetricEncryptionFunction { | |||
public boolean supportSymmetricKey(byte[] symmetricKeyBytes) { | |||
// 验证输入字节数组长度=算法标识长度+密钥类型长度+密钥长度,字节数组的算法标识对应AES算法且密钥密钥类型是对称密钥 | |||
return symmetricKeyBytes.length == SYMMETRICKEY_LENGTH && CryptoAlgorithm.match(AES, symmetricKeyBytes) | |||
&& symmetricKeyBytes[ALGORYTHM_CODE_SIZE] == SYMMETRIC_KEY.CODE; | |||
&& symmetricKeyBytes[ALGORYTHM_CODE_SIZE] == SYMMETRIC.CODE; | |||
} | |||
@Override | |||
@@ -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.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; | |||
@@ -96,7 +96,7 @@ public class ED25519SignatureFunction implements SignatureFunction { | |||
public boolean supportPrivKey(byte[] privKeyBytes) { | |||
// 验证输入字节数组长度=算法标识长度+密钥类型长度+密钥长度,密钥数据的算法标识对应ED25519签名算法,并且密钥类型是私钥 | |||
return privKeyBytes.length == PRIVKEY_LENGTH && CryptoAlgorithm.match(ED25519, privKeyBytes) | |||
&& privKeyBytes[ALGORYTHM_CODE_SIZE] == PRIV_KEY.CODE; | |||
&& privKeyBytes[ALGORYTHM_CODE_SIZE] == PRIVATE.CODE; | |||
} | |||
@Override | |||
@@ -109,7 +109,7 @@ public class ED25519SignatureFunction implements SignatureFunction { | |||
public boolean supportPubKey(byte[] pubKeyBytes) { | |||
// 验证输入字节数组长度=算法标识长度+密钥类型长度+密钥长度,密钥数据的算法标识对应ED25519签名算法,并且密钥类型是公钥 | |||
return pubKeyBytes.length == PUBKEY_LENGTH && CryptoAlgorithm.match(ED25519, pubKeyBytes) | |||
&& pubKeyBytes[ALGORYTHM_CODE_SIZE] == PUB_KEY.CODE; | |||
&& pubKeyBytes[ALGORYTHM_CODE_SIZE] == PUBLIC.CODE; | |||
} | |||
@@ -1,7 +1,7 @@ | |||
//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.assertEquals; | |||
//import static org.junit.Assert.assertNotNull; | |||
@@ -207,8 +207,8 @@ | |||
// assertEquals(expectedPubKeyLength,rawPubKeyBytes.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); | |||
// byte[] rawDigest = signatureDigest.getRawDigest(); | |||
@@ -414,8 +414,8 @@ | |||
// assertEquals(expectedPubKeyLength,rawPubKeyBytes.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); | |||
// byte[] rawCiphertextBytes = ciphertext.getRawCiphertext(); | |||
@@ -753,7 +753,7 @@ | |||
// verifyResolvePubKey(asymmetricCrypto,algorithm,32,pubKeyBytesWithWrongAlgCode,IllegalArgumentException.class); | |||
// | |||
// byte[] pubKeyBytesWithWrongKeyType= pubKeyBytes; | |||
// pubKeyBytesWithWrongKeyType[1] = PRIV_KEY.CODE; | |||
// pubKeyBytesWithWrongKeyType[1] = PRIVATE.CODE; | |||
// verifyResolvePubKey(asymmetricCrypto,algorithm,32,pubKeyBytesWithWrongKeyType,IllegalArgumentException.class); | |||
// | |||
// pubKeyBytes = null; | |||
@@ -777,7 +777,7 @@ | |||
// verifyResolvePubKey(asymmetricCrypto,algorithm,65,pubKeyBytesWithWrongAlgCode,IllegalArgumentException.class); | |||
// | |||
// pubKeyBytesWithWrongKeyType= pubKeyBytes; | |||
// pubKeyBytesWithWrongKeyType[1] = PRIV_KEY.CODE; | |||
// pubKeyBytesWithWrongKeyType[1] = PRIVATE.CODE; | |||
// verifyResolvePubKey(asymmetricCrypto,algorithm,65,pubKeyBytesWithWrongKeyType,IllegalArgumentException.class); | |||
// | |||
// pubKeyBytes = null; | |||
@@ -800,7 +800,7 @@ | |||
// verifyResolvePubKey(asymmetricCrypto,algorithm,32,pubKeyBytesWithWrongAlgCode,IllegalArgumentException.class); | |||
// | |||
// pubKeyBytesWithWrongKeyType= pubKeyBytes; | |||
// pubKeyBytesWithWrongKeyType[1] = PRIV_KEY.CODE; | |||
// pubKeyBytesWithWrongKeyType[1] = PRIVATE.CODE; | |||
// verifyResolvePubKey(asymmetricCrypto,algorithm,32,pubKeyBytesWithWrongKeyType,IllegalArgumentException.class); | |||
// | |||
// pubKeyBytes = null; | |||
@@ -863,7 +863,7 @@ | |||
// verifyResolvePrivKey(asymmetricCrypto,algorithm,32,privKeyBytesWithWrongAlgCode,IllegalArgumentException.class); | |||
// | |||
// byte[] privKeyBytesWithWrongKeyType = privKeyBytes; | |||
// privKeyBytesWithWrongKeyType[1] = PUB_KEY.CODE; | |||
// privKeyBytesWithWrongKeyType[1] = PUBLIC.CODE; | |||
// verifyResolvePrivKey(asymmetricCrypto,algorithm,32,privKeyBytesWithWrongKeyType,IllegalArgumentException.class); | |||
// | |||
// privKeyBytes = null; | |||
@@ -887,7 +887,7 @@ | |||
// verifyResolvePrivKey(asymmetricCrypto,algorithm,32,privKeyBytesWithWrongAlgCode,IllegalArgumentException.class); | |||
// | |||
// privKeyBytesWithWrongKeyType = privKeyBytes; | |||
// privKeyBytesWithWrongKeyType[1] = PUB_KEY.CODE; | |||
// privKeyBytesWithWrongKeyType[1] = PUBLIC.CODE; | |||
// verifyResolvePrivKey(asymmetricCrypto,algorithm,32,privKeyBytesWithWrongKeyType,IllegalArgumentException.class); | |||
// | |||
// privKeyBytes = null; | |||
@@ -910,7 +910,7 @@ | |||
// verifyResolvePrivKey(asymmetricCrypto,algorithm,32,privKeyBytesWithWrongAlgCode,IllegalArgumentException.class); | |||
// | |||
// privKeyBytesWithWrongKeyType = privKeyBytes; | |||
// privKeyBytesWithWrongKeyType[1] = PUB_KEY.CODE; | |||
// privKeyBytesWithWrongKeyType[1] = PUBLIC.CODE; | |||
// verifyResolvePrivKey(asymmetricCrypto,algorithm,32,privKeyBytesWithWrongKeyType,IllegalArgumentException.class); | |||
// | |||
// privKeyBytes = null; | |||
@@ -1,5 +1,20 @@ | |||
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 | |||
* @title: AESEncryptionFunctionTest | |||
@@ -7,4 +22,257 @@ package test.com.jd.blockchain.crypto.service.classic; | |||
* @date 2019-04-01, 13:57 | |||
*/ | |||
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())); | |||
} | |||
} |
@@ -16,8 +16,8 @@ | |||
//import java.io.OutputStream; | |||
//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.*; | |||
// | |||
//public class SymmetricCryptographyImplTest { | |||
@@ -147,7 +147,7 @@ | |||
// //验证SymmetricKey的getRawKeyBytes方法 | |||
// assertEquals(16, symmetricKey.getRawKeyBytes().length); | |||
// //验证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); | |||
@@ -393,7 +393,7 @@ | |||
// byte[] key = new byte[16]; | |||
// 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); | |||
// | |||
// byte[] truncatedSymmetricKeyBytes = new byte[symmetricKeyBytes.length-2]; | |||
@@ -406,7 +406,7 @@ | |||
// | |||
// byte[] symmetricKeyBytesWithWrongKeyType= symmetricKeyBytes; | |||
// System.arraycopy(symmetricKeyBytes,0,symmetricKeyBytesWithWrongKeyType,0,symmetricKeyBytesWithWrongKeyType.length); | |||
// symmetricKeyBytesWithWrongKeyType[1] = PRIV_KEY.CODE; | |||
// symmetricKeyBytesWithWrongKeyType[1] = PRIVATE.CODE; | |||
// verifyResolveSymmetricKey(symmetricCrypto,algorithm,symmetricKeyBytesWithWrongKeyType,IllegalArgumentException.class); | |||
// | |||
// symmetricKeyBytes = null; | |||
@@ -415,7 +415,7 @@ | |||
// | |||
// //test 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); | |||
// | |||
@@ -429,7 +429,7 @@ | |||
// | |||
// symmetricKeyBytesWithWrongKeyType= symmetricKeyBytes; | |||
// System.arraycopy(symmetricKeyBytes,0,symmetricKeyBytesWithWrongKeyType,0,symmetricKeyBytesWithWrongKeyType.length); | |||
// symmetricKeyBytesWithWrongKeyType[1] = PRIV_KEY.CODE; | |||
// symmetricKeyBytesWithWrongKeyType[1] = PRIVATE.CODE; | |||
// verifyResolveSymmetricKey(symmetricCrypto,algorithm,symmetricKeyBytesWithWrongKeyType,IllegalArgumentException.class); | |||
// | |||
// symmetricKeyBytes = null; | |||
@@ -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; | |||
@@ -10,7 +10,7 @@ public class PrivKey extends BaseCryptoKey { | |||
private static final long serialVersionUID = 6265440395252295646L; | |||
public PrivKey(CryptoAlgorithm algorithm, byte[] rawCryptoBytes) { | |||
super(algorithm, rawCryptoBytes, CryptoKeyType.PRIV_KEY); | |||
super(algorithm, rawCryptoBytes, CryptoKeyType.PRIVATE); | |||
} | |||
public PrivKey(byte[] cryptoBytes) { | |||
@@ -19,6 +19,6 @@ public class PrivKey extends BaseCryptoKey { | |||
@Override | |||
public CryptoKeyType getKeyType() { | |||
return CryptoKeyType.PRIV_KEY; | |||
return CryptoKeyType.PRIVATE; | |||
} | |||
} |
@@ -11,7 +11,7 @@ public class PubKey extends BaseCryptoKey { | |||
private static final long serialVersionUID = -2055071197736385328L; | |||
public PubKey(CryptoAlgorithm algorithm, byte[] rawCryptoBytes) { | |||
super(algorithm, rawCryptoBytes, CryptoKeyType.PUB_KEY); | |||
super(algorithm, rawCryptoBytes, CryptoKeyType.PUBLIC); | |||
} | |||
public PubKey(byte[] cryptoBytes) { | |||
@@ -20,6 +20,6 @@ public class PubKey extends BaseCryptoKey { | |||
@Override | |||
public CryptoKeyType getKeyType() { | |||
return CryptoKeyType.PUB_KEY; | |||
return CryptoKeyType.PUBLIC; | |||
} | |||
} |
@@ -1,6 +1,6 @@ | |||
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; | |||
public SymmetricKey(CryptoAlgorithm algorithm, byte[] rawKeyBytes) { | |||
super(algorithm, rawKeyBytes, SYMMETRIC_KEY); | |||
super(algorithm, rawKeyBytes, SYMMETRIC); | |||
} | |||
public SymmetricKey(byte[] keyBytes) { | |||
@@ -22,7 +22,7 @@ public class SymmetricKey extends BaseCryptoKey { | |||
@Override | |||
public CryptoKeyType getKeyType() { | |||
return SYMMETRIC_KEY; | |||
return SYMMETRIC; | |||
} | |||
} |
@@ -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.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 org.bouncycastle.crypto.AsymmetricCipherKeyPair; | |||
@@ -90,7 +90,7 @@ public class SM2CryptoFunction implements AsymmetricEncryptionFunction, Signatur | |||
public boolean supportPrivKey(byte[] privKeyBytes) { | |||
// 验证输入字节数组长度=算法标识长度+密钥类型长度+密钥长度,密钥数据的算法标识对应SM2算法,并且密钥类型是私钥 | |||
return privKeyBytes.length == PRIVKEY_LENGTH && CryptoAlgorithm.match(SM2, privKeyBytes) | |||
&& privKeyBytes[ALGORYTHM_CODE_SIZE] == PRIV_KEY.CODE; | |||
&& privKeyBytes[ALGORYTHM_CODE_SIZE] == PRIVATE.CODE; | |||
} | |||
@Override | |||
@@ -103,7 +103,7 @@ public class SM2CryptoFunction implements AsymmetricEncryptionFunction, Signatur | |||
public boolean supportPubKey(byte[] pubKeyBytes) { | |||
// 验证输入字节数组长度=算法标识长度+密钥类型长度+椭圆曲线点长度,密钥数据的算法标识对应SM2算法,并且密钥类型是公钥 | |||
return pubKeyBytes.length == PUBKEY_LENGTH && CryptoAlgorithm.match(SM2, pubKeyBytes) | |||
&& pubKeyBytes[ALGORYTHM_CODE_SIZE] == PUB_KEY.CODE; | |||
&& pubKeyBytes[ALGORYTHM_CODE_SIZE] == PUBLIC.CODE; | |||
} | |||
@Override | |||
@@ -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.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.InputStream; | |||
@@ -172,7 +172,7 @@ public class SM4EncryptionFunction implements SymmetricEncryptionFunction { | |||
public boolean supportSymmetricKey(byte[] symmetricKeyBytes) { | |||
// 验证输入字节数组长度=算法标识长度+密钥类型长度+密钥长度,字节数组的算法标识对应SM4算法且密钥密钥类型是对称密钥 | |||
return symmetricKeyBytes.length == SYMMETRICKEY_LENGTH && CryptoAlgorithm.match(SM4, symmetricKeyBytes) | |||
&& symmetricKeyBytes[ALGORYTHM_CODE_SIZE] == SYMMETRIC_KEY.CODE; | |||
&& symmetricKeyBytes[ALGORYTHM_CODE_SIZE] == SYMMETRIC.CODE; | |||
} | |||
@Override | |||
@@ -1,5 +1,16 @@ | |||
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 | |||
* @title: SM4EncryptionFunctionTest | |||
@@ -7,4 +18,256 @@ package test.com.jd.blockchain.crypto.service.sm; | |||
* @date 2019-04-03, 16:35 | |||
*/ | |||
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())); | |||
} | |||
} |
@@ -65,7 +65,7 @@ public class SDKDemo_User { | |||
// 在本地产生要注册的账户的秘钥; | |||
//BlockchainKeyGenerator generator = BlockchainKeyGenerator.getInstance(); | |||
//BlockchainKeyPair user = generator.generate(CryptoKeyType.PUB_KEY); | |||
//BlockchainKeyPair user = generator.generate(CryptoKeyType.PUBLIC); | |||
SignatureFunction signatureFunction = asymmetricCryptography.getSignatureFunction(CryptoAlgorithm.ED25519); | |||
CryptoKeyPair cryptoKeyPair = signatureFunction.generateKeyPair(); | |||