public class BlockCipherSerializer extends ChainedSerializer
BlockCipherSerializer serializer = new BlockCipherSerializer(); serializer.setAlgorithm("AES/CBC/PKCS5Padding"); serializer.setPassword("12345678901234567890123456789012"); serializer.setIv("1234567890123456"); Transporter trans = new NatsTransporter("localhost"); trans.setSerializer(serializer); ServiceBroker broker = ServiceBroker.builder() .nodeID("node1") .transporter(trans) .build();Chaining Serializers (serialize then compress then encrypt packets):
Transporter trans = new NatsTransporter("localhost"); MsgPackSerializer msgPack = new MsgPackSerializer(); DeflaterSerializer deflater = new DeflaterSerializer(msgPack); BlockCipherSerializer cipher = new BlockCipherSerializer(deflater); trans.setSerializer(cipher);
Modifier and Type | Field and Description |
---|---|
protected java.lang.String |
algorithm
Type of the block Cipher.
|
protected java.lang.ThreadLocal<javax.crypto.Cipher> |
decriptors |
static java.lang.String |
DEFAULT_ALGORITHM
Same as "aes-256-cbc" in Node.js.
|
static byte[] |
EMPTY_IV
Empty IV block.
|
protected java.lang.ThreadLocal<javax.crypto.Cipher> |
encriptors |
protected byte[] |
iv
Algorithm parameters (IV).
|
protected java.lang.String |
password
Password for Symmetric Key Encryption.
|
protected javax.crypto.spec.SecretKeySpec |
secretKey
SecretKeySpec, can be specified externally.
|
parent
debug, format, reader, writer
broker, logger, name
Constructor and Description |
---|
BlockCipherSerializer()
Creates a JSON Serializer that uses AES encryption algorithm with the
default password (using the hard-coded, default password is not very
secure).
|
BlockCipherSerializer(Serializer parent)
Creates a Serializer that uses a symmetric encryption algorithm to
encrypt/decrypt messages.
|
BlockCipherSerializer(Serializer parent,
java.lang.String password)
Creates a Serializer that uses a symmetric encryption algorithm to
encrypt/decrypt messages.
|
BlockCipherSerializer(Serializer parent,
java.lang.String password,
java.lang.String algorithm)
Creates a Serializer that uses a symmetric encryption algorithm to
encrypt/decrypt messages.
|
BlockCipherSerializer(Serializer parent,
java.lang.String password,
java.lang.String algorithm,
byte[] iv)
Creates a Serializer that uses a symmetric encryption algorithm to
encrypt/decrypt messages.
|
BlockCipherSerializer(Serializer parent,
java.lang.String password,
java.lang.String algorithm,
java.lang.String iv)
Creates a Serializer that uses a symmetric encryption algorithm to
encrypt/decrypt messages.
|
BlockCipherSerializer(java.lang.String password)
Creates a JSON Serializer that uses AES encryption algorithm to
encrypt/decrypt messages.
|
BlockCipherSerializer(java.lang.String password,
java.lang.String algorithm,
byte[] iv)
Creates a Serializer that uses a symmetric encryption algorithm to
encrypt/decrypt messages.
|
BlockCipherSerializer(java.lang.String password,
java.lang.String algorithm,
java.lang.String iv)
Creates a Serializer that uses a symmetric encryption algorithm to
encrypt/decrypt messages.
|
Modifier and Type | Method and Description |
---|---|
protected byte[] |
decrypt(javax.crypto.Cipher cipher,
byte[] bytes) |
protected byte[] |
encrypt(javax.crypto.Cipher cipher,
byte[] bytes) |
java.lang.String |
getAlgorithm() |
protected java.lang.String |
getAlgorithmName() |
protected javax.crypto.Cipher |
getDecryptor() |
protected javax.crypto.Cipher |
getEncriptor() |
byte[] |
getIv() |
java.lang.String |
getPassword() |
javax.crypto.spec.SecretKeySpec |
getSecretKey() |
io.datatree.Tree |
read(byte[] source) |
void |
setAlgorithm(java.lang.String algorithm) |
void |
setIv(byte[] iv) |
void |
setIv(java.lang.String iv) |
void |
setPassword(java.lang.String password) |
void |
setSecretKey(javax.crypto.spec.SecretKeySpec secretKey) |
void |
started(ServiceBroker broker) |
byte[] |
write(io.datatree.Tree value) |
getFormat, setDebug, stopped
isDebug
getBroker, getLogger, getName
public static final java.lang.String DEFAULT_ALGORITHM
public static final byte[] EMPTY_IV
protected java.lang.String algorithm
protected java.lang.String password
protected byte[] iv
protected javax.crypto.spec.SecretKeySpec secretKey
protected java.lang.ThreadLocal<javax.crypto.Cipher> encriptors
protected java.lang.ThreadLocal<javax.crypto.Cipher> decriptors
public BlockCipherSerializer()
public BlockCipherSerializer(java.lang.String password)
password
- password for Symmetric Key Encryptionpublic BlockCipherSerializer(Serializer parent)
parent
- parent Serializer (eg. a JsonSerializer)public BlockCipherSerializer(Serializer parent, java.lang.String password)
parent
- parent Serializer (eg. a JsonSerializer)password
- password for Symmetric Key Encryptionpublic BlockCipherSerializer(Serializer parent, java.lang.String password, java.lang.String algorithm)
parent
- parent Serializer (eg. a JsonSerializer)password
- password for Symmetric Key Encryptionalgorithm
- block Cipher type (eg. "AES", "DES", "DESede", "Blowfish")public BlockCipherSerializer(java.lang.String password, java.lang.String algorithm, byte[] iv)
password
- password for Symmetric Key Encryptionalgorithm
- block Cipher type (eg. "AES", "DES", "DESede", "Blowfish")iv
- IV parameter (can be null)public BlockCipherSerializer(java.lang.String password, java.lang.String algorithm, java.lang.String iv)
password
- password for Symmetric Key Encryptionalgorithm
- block Cipher type (eg. "AES", "DES", "DESede", "Blowfish")iv
- IV parameter (can be null)public BlockCipherSerializer(Serializer parent, java.lang.String password, java.lang.String algorithm, java.lang.String iv)
parent
- parent Serializer (eg. a JsonSerializer)password
- password for Symmetric Key Encryptionalgorithm
- block Cipher type (eg. "AES", "DES", "DESede", "Blowfish")iv
- IV parameter (can be null)public BlockCipherSerializer(Serializer parent, java.lang.String password, java.lang.String algorithm, byte[] iv)
parent
- parent Serializer (eg. a JsonSerializer)password
- password for Symmetric Key Encryptionalgorithm
- block Cipher type (eg. "AES", "DES", "DESede", "Blowfish")iv
- IV parameter (can be null)public void started(ServiceBroker broker) throws java.lang.Exception
started
in interface MoleculerLifecycle
started
in class ChainedSerializer
java.lang.Exception
protected java.lang.String getAlgorithmName()
public byte[] write(io.datatree.Tree value) throws java.lang.Exception
write
in class Serializer
java.lang.Exception
protected byte[] encrypt(javax.crypto.Cipher cipher, byte[] bytes) throws java.lang.Exception
java.lang.Exception
public io.datatree.Tree read(byte[] source) throws java.lang.Exception
read
in class Serializer
java.lang.Exception
protected byte[] decrypt(javax.crypto.Cipher cipher, byte[] bytes) throws java.lang.Exception
java.lang.Exception
protected javax.crypto.Cipher getEncriptor() throws java.lang.Exception
java.lang.Exception
protected javax.crypto.Cipher getDecryptor() throws java.lang.Exception
java.lang.Exception
public java.lang.String getAlgorithm()
public void setAlgorithm(java.lang.String algorithm)
public java.lang.String getPassword()
public void setPassword(java.lang.String password)
public byte[] getIv()
public void setIv(java.lang.String iv)
public void setIv(byte[] iv)
public javax.crypto.spec.SecretKeySpec getSecretKey()
public void setSecretKey(javax.crypto.spec.SecretKeySpec secretKey)