SM2 Crypto icon

SM2 Crypto

Encrypt or decrypt strings using the SM2 algorithm

Overview

This node implements encryption and decryption of strings using the SM2 cryptographic algorithm. It is useful for securely transforming sensitive data within n8n workflows, such as encrypting confidential information before storage or decrypting received encrypted messages for processing.

Typical use cases include:

  • Encrypting user data before saving it to a database.
  • Decrypting incoming encrypted payloads from external systems.
  • Securing communication between different parts of an automation workflow.

For example, you can provide a public key to encrypt a message field, then later decrypt it with the corresponding private key.

Properties

Name Meaning
Operation Choose whether to "Encrypt" or "Decrypt" the input data.
Token (Key) The cryptographic key: a public key for encryption or a private key for decryption.
Input Field Name The name of the input JSON field containing the string to encrypt or decrypt (default: "data").

Output

The node outputs a JSON object with the following structure depending on the operation:

  • Encrypt:

    {
      "ciphertext": "04..."  // The encrypted string prefixed with "04"
    }
    
  • Decrypt:

    • If the decrypted plaintext is valid JSON, it outputs the parsed JSON object.
    • Otherwise, it outputs an object with:
      {
        "error": <parsing error details>,
        "plaintext": "<decrypted string>"
      }
      
  • In case of any error during encryption or decryption, the output will be:

    {
      "error": "<error message>"
    }
    

No binary data output is produced by this node.

Dependencies

  • This node depends on the sm-crypto library for SM2 encryption and decryption.
  • Requires providing a valid cryptographic key (public key for encryption, private key for decryption).
  • No additional environment variables or external API credentials are needed.

Troubleshooting

  • Common issues:
    • Providing an invalid or incorrectly formatted key will cause errors.
    • Input data that is not a string or cannot be serialized to JSON may cause unexpected results.
    • Decryption failures if ciphertext is corrupted or does not start with the expected prefix ("04").
  • Error messages:
    • Errors during encryption/decryption are returned in the output under the error field.
    • JSON parsing errors after decryption are also reported with details and the raw plaintext.
  • Resolution tips:
    • Ensure keys are correctly generated and correspond to the SM2 algorithm.
    • Verify input fields contain valid strings or JSON-serializable data.
    • Confirm ciphertext format matches expectations (starts with "04" for encrypted data).

Links and References

Discussion