Skip to main content

Non-encrypted hard drives


Implementation of full disk encryption


  • Usage of Java for building robust and scalable applications
  • Usage of for file input/output operations
  • Usage of for efficient writing of data to a file
  • Usage of for writing data to a file


Non compliant code


val file = new File("/path/to/your/file")
val bw = new BufferedWriter(new FileWriter(file))
bw.write("This is the sensitive customer data.")

The above Scala code is writing sensitive customer data to a file in a hard drive. However, it is vulnerable to data breaches because the data is not encrypted before being written to the file.

The FileWriter class is used to write characters to a file. The BufferedWriter class is used to provide buffering for Writer instances. It makes the performance fast. But in this scenario, it is being used to write plain text data to a file, which means anyone with access to the file can read the data.

This is a major security risk, especially if the data is sensitive, such as customer information. If an attacker gains access to the file, they can easily read and misuse the data.

The vulnerability here is the lack of encryption when writing data to a file. To mitigate this vulnerability, the data should be encrypted before being written to the file. This can be done using various encryption algorithms available.


  • Implement full disk encryption on the server or machine where the application is running.
  • Ensure that the encryption is applied to all storage devices, including hard drives and any other storage media.
  • Use secure encryption algorithms and key management practices to protect the data.
  • Avoid storing sensitive customer data directly on the local file system.
  • Consider using a secure database or cloud storage solution that provides built-in encryption.
  • If storing data locally, encrypt the data before writing it to the file system.
  • Use secure file permissions to restrict access to the sensitive data files.
  • Regularly monitor and update the encryption mechanisms to ensure they are up to date and secure.
  • Implement secure backup and disaster recovery procedures to protect the encrypted data.
  • Train and educate developers and system administrators on the importance of data encryption and best practices.

Compliant code

import javax.crypto.Cipher
import javax.crypto.spec.SecretKeySpec
import java.util.Base64

val secret = "ThisIsASecretKey" // Replace with your secret key
val skeySpec = new SecretKeySpec(secret.getBytes("UTF-8"), "AES")

val cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING")
cipher.init(Cipher.ENCRYPT_MODE, skeySpec)

val text = "This is the sensitive customer data."
val encrypted = cipher.doFinal(text.getBytes("UTF-8"))

val file = new File("/path/to/your/file")
val bw = new BufferedWriter(new FileWriter(file))

The above code snippet is a revised version of the original code that now includes encryption of sensitive data before writing it to the file system.

The javax.crypto.Cipher and javax.crypto.spec.SecretKeySpec classes are used to create an AES cipher with a secret key. The secret key should be securely managed and not hardcoded as in this example.

The Cipher.getInstance("AES/ECB/PKCS5PADDING") method call sets up the cipher to use AES encryption with ECB mode and PKCS5 padding.

The cipher.init(Cipher.ENCRYPT_MODE, skeySpec) method call initializes the cipher to encryption mode using the secret key.

The cipher.doFinal(text.getBytes("UTF-8")) method call encrypts the sensitive data.

The encrypted data is then encoded as a Base64 string using Base64.getEncoder.encodeToString(encrypted). This is necessary because the encrypted data is in binary format, which may not be properly handled by the BufferedWriter.

Finally, the Base64-encoded encrypted data is written to the file.

This code ensures that the sensitive data is encrypted before being stored, providing a layer of protection against unauthorized access. However, it is important to note that this is just one part of a comprehensive data security strategy. Other measures such as secure key management, use of secure storage solutions, and regular security audits are also necessary.