Secure Coding Practices: Understanding Cryptography and Sanitization in Defensive Coding

Secure Coding Practices: Understanding Cryptography and Sanitization in Defensive Coding

1. Introduction to Cryptography: Cryptography is crucial in protecting sensitive information in an application. It involves the use of cryptographic functions which can perform several actions:

  • Hashing Functions: These functions hash the data, creating a hash value that helps verify data integrity.
  • Encryption and Decryption Functions: These are used to encrypt data and decrypt it when necessary.

Cryptographic Vulnerabilities: A breach in cryptographic logic can expose sensitive data, making it vital to carefully implement and scrutinize cryptographic functions.

Common Cryptographic Issues:

  • Not encrypting sensitive data including user data, passwords, and application configuration data.
  • Insecure encryption keys management. If keys are stored in config files, ensure these files are encrypted.
  • Using outdated cryptographic APIs which might lack recent enhancements and might be vulnerable.

Mitigation Techniques:

  • Encrypt all sensitive data.
  • Pay attention to trust boundaries to prevent data breaches between different network segments or departments within an organization.
  • Use standard and updated encryption and hashing algorithms for reliable security.
  • Implement cryptographic flexibility in your software design, allowing easy update of algorithms without hardcoding them.


2. Introduction to Sanitization: Sanitization is the process of making information safe by converting it from a harmful format to a safe format.

Types of Sanitization:

  • Input Sanitization: Cleansing the data as it is input into the system. Techniques include stripping harmful characters and substituting unwanted characters with safe alternatives.
  • Output Sanitization: Involves securing the data after processing but before it reaches the user. Typically done through encoding, converting characters to their HTML equivalents to prevent misunderstanding by browsers.

Principles of Data Sanitization:

  • Maintain the integrity and meaning of the data.
  • Implement effective techniques for stripping, substitution, and literalization to prevent injection and cross-site scripting attacks.

HTML Encoding Example:

  • Converting a greater-than sign > to its HTML encoded equivalent >. This ensures that when a user's browser receives the data, it renders it correctly without executing any embedded scripts.


By understanding and implementing strong cryptographic and sanitization practices, developers can significantly enhance the security of their applications, defending against both data breaches and malicious attacks. Always ensure using updated tools and techniques and monitor the sensitive data flow within the application environment for optimum security.

To view or add a comment, sign in

More articles by Col. Jatinder Singh Randhawa Retd.

Others also viewed

Explore content categories