Category Archives: Snippets

Implementing JPEG Algorithm in Java

This Java code demonstrates the core steps of the JPEG compression algorithm. It implements the Discrete Cosine Transform (DCT), quantization, zigzag scan, and a simplified entropy encoding. These are fundamental processes for compressing digital images, reducing file size by transforming and encoding image data.

This example provides an educational look at the key stages of JPEG compression.

Continue reading Implementing JPEG Algorithm in Java

Demonstrating Transposition Cipher in Java

A transposition cipher is a method of encryption where the positions of characters are shifted according to a certain system, without changing the actual characters themselves. It rearranges the characters in a message to create ciphertext, and the same method in reverse restores the original message.

This post walks you through how a transposition cipher works using a Java program. This particular example arranges characters into a matrix and reads them column-wise (for encryption) or row-wise (for decryption).


How the Transposition Cipher Works

  1. Encryption:
    • Fill a matrix column-wise with characters of the message.
    • Read the characters row-wise to get the encrypted message.
    • The operation is repeated twice for added confusion.
  2. Decryption:
    • Reverse the above process by filling the matrix row-wise.
    • Read it column-wise to retrieve the original message.
Continue reading Demonstrating Transposition Cipher in Java

C++ Implementation of Substitution Cipher

This simple C++ implementation of a substitution cipher—specifically the Caesar cipher—demonstrates how basic cryptographic techniques can be used with file handling.

The Caesar cipher is one of the oldest and simplest substitution cipher techniques. It works by shifting the letters in a message by a fixed number of positions in the alphabet. Unlike transposition ciphers, which rearrange character positions, substitution ciphers replace characters with others based on a defined scheme.

This blog post demonstrates a C++ implementation that reads a message from a file, performs Caesar cipher encryption or decryption, and writes the result to another file.


How It Works

  1. Input and Output Files:
    • Input is read from a file named anip.txt.
    • Output is written to a file named anop.txt.
  2. User Choices:
    • You can choose between encryption and decryption.
    • Provide a key (shift amount), e.g., 2.
  3. Caesar Cipher Logic:
    • For encryption, each letter is shifted forward in the alphabet by the key.
    • For decryption, each letter is shifted backward by the key.
    • Non-alphabet characters are left unchanged.
Continue reading C++ Implementation of Substitution Cipher