Class BlockCipherSerializer

All Implemented Interfaces:
MoleculerLifecycle

public class BlockCipherSerializer extends ChainedSerializer
Serializer with Symmetric Key Encryption. This enables message-level encryption. Sample of usage:
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);
  • Field Details

    • DEFAULT_ALGORITHM

      public static final String DEFAULT_ALGORITHM
      Same as "aes-256-cbc" in Node.js.
      See Also:
    • EMPTY_IV

      public static final byte[] EMPTY_IV
      Empty IV block.
    • algorithm

      protected String algorithm
      Type of the block Cipher. Possible values include:
      • AES/CTR/NoPadding
      • AES/CBC/PKCS5Padding
      • AES
      • DES
      • DESede
      • RC2
      • Blowfish
      • ARCFOUR
      • etc.
    • password

      protected String password
      Password for Symmetric Key Encryption. Using this hard-coded, default password is not secure. Use a custom password instead of this. The required password length is 32, when using "AES/CBC/PKCS5Padding"!
    • iv

      protected byte[] iv
      Algorithm parameters (IV). Can be "null".
    • secretKey

      protected SecretKeySpec secretKey
      SecretKeySpec, can be specified externally. If not specified, it is calculated from the "password".
    • encriptors

      protected ThreadLocal<Cipher> encriptors
    • decriptors

      protected ThreadLocal<Cipher> decriptors
  • Constructor Details

    • BlockCipherSerializer

      public 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

      public BlockCipherSerializer(String password)
      Creates a JSON Serializer that uses AES encryption algorithm to encrypt/decrypt messages.
      Parameters:
      password - password for Symmetric Key Encryption
    • BlockCipherSerializer

      public BlockCipherSerializer(Serializer parent)
      Creates a Serializer that uses a symmetric encryption algorithm to encrypt/decrypt messages.
      Parameters:
      parent - parent Serializer (eg. a JsonSerializer)
    • BlockCipherSerializer

      public BlockCipherSerializer(Serializer parent, String password)
      Creates a Serializer that uses a symmetric encryption algorithm to encrypt/decrypt messages.
      Parameters:
      parent - parent Serializer (eg. a JsonSerializer)
      password - password for Symmetric Key Encryption
    • BlockCipherSerializer

      public BlockCipherSerializer(Serializer parent, String password, String algorithm)
      Creates a Serializer that uses a symmetric encryption algorithm to encrypt/decrypt messages.
      Parameters:
      parent - parent Serializer (eg. a JsonSerializer)
      password - password for Symmetric Key Encryption
      algorithm - block Cipher type (eg. "AES", "DES", "DESede", "Blowfish")
    • BlockCipherSerializer

      public BlockCipherSerializer(String password, String algorithm, byte[] iv)
      Creates a Serializer that uses a symmetric encryption algorithm to encrypt/decrypt messages.
      Parameters:
      password - password for Symmetric Key Encryption
      algorithm - block Cipher type (eg. "AES", "DES", "DESede", "Blowfish")
      iv - IV parameter (can be null)
    • BlockCipherSerializer

      public BlockCipherSerializer(String password, String algorithm, String iv)
      Creates a Serializer that uses a symmetric encryption algorithm to encrypt/decrypt messages.
      Parameters:
      password - password for Symmetric Key Encryption
      algorithm - block Cipher type (eg. "AES", "DES", "DESede", "Blowfish")
      iv - IV parameter (can be null)
    • BlockCipherSerializer

      public BlockCipherSerializer(Serializer parent, String password, String algorithm, String iv)
      Creates a Serializer that uses a symmetric encryption algorithm to encrypt/decrypt messages.
      Parameters:
      parent - parent Serializer (eg. a JsonSerializer)
      password - password for Symmetric Key Encryption
      algorithm - block Cipher type (eg. "AES", "DES", "DESede", "Blowfish")
      iv - IV parameter (can be null)
    • BlockCipherSerializer

      public BlockCipherSerializer(Serializer parent, String password, String algorithm, byte[] iv)
      Creates a Serializer that uses a symmetric encryption algorithm to encrypt/decrypt messages.
      Parameters:
      parent - parent Serializer (eg. a JsonSerializer)
      password - password for Symmetric Key Encryption
      algorithm - block Cipher type (eg. "AES", "DES", "DESede", "Blowfish")
      iv - IV parameter (can be null)
  • Method Details