Category Archives: Snippets

Animating a Truck in C

Computer graphics animation is the technique of creating the illusion of movement by rapidly drawing and erasing images on screen. In the Borland Turbo C environment, the graphics.h library provides low-level drawing functions that make it straightforward to build simple frame-by-frame animations. In this post, we walk through a C program that animates a truck moving across the screen using rectangles and circles to represent the truck body and wheels.

What the Program Does

The program draws a simple truck shape — a large rectangle for the cabin/body, a smaller rectangle for the cab, and two circles for the wheels — then shifts this shape incrementally across the screen to create an animation effect. Between each frame the viewport is cleared, giving the impression of smooth movement.

Continue reading Animating a Truck in C

Demonstrating Bully Algorithm in Java

The Bully Algorithm is a classic election algorithm used in distributed systems to elect a coordinator (leader) among a group of processes. When a process notices that the current coordinator is no longer responding, it initiates an election. The process with the highest priority among all active (running) processes wins the election and becomes the new coordinator.

This Java program simulates the Bully Algorithm with user-defined processes, each having a status (active or inactive) and a priority value.

Continue reading Demonstrating Bully Algorithm in Java

Demonstrating Deadlock with Resource Allocation

Deadlock is a situation in a distributed or multi-process system where a set of processes are permanently blocked, each waiting for a resource that another process in the set holds. Detecting which processes are causing a deadlock is a critical operating system responsibility.

This C program implements a deadlock detection algorithm using a resource allocation graph. It uses a claim matrix (maximum resource needs), an allocation matrix (currently held resources), and availability vectors to identify processes that are involved in a deadlock.

Continue reading Demonstrating Deadlock with Resource Allocation

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

Java Program to Demonstrating RSA

In the world of cryptography, RSA (Rivest–Shamir–Adleman) is a popular public-key cryptosystem widely used for secure data transmission. It’s based on the principle that while multiplying large prime numbers is computationally easy, factoring their product is not — which ensures security.

This post will walk you through how RSA encryption and decryption work using a simple Java program. You’ll learn how the sender encrypts a message using the recipient’s public key, and how the receiver decrypts it using their private key.


What Happens Under the Hood?

The RSA algorithm follows these steps:

  1. Key Generation
    • Choose two large prime numbers p and q.
    • Compute N = p * q.
    • Compute the totient function: phi = (p-1)(q-1).
    • Choose an integer e such that 1 < e < phi and gcd(e, phi) = 1.
    • Compute d, the modular inverse of e modulo phi.
  2. Encryption
    • The sender uses the recipient’s public key (e, N) to compute ciphertext: c = m^e mod N.
  3. Decryption
    • The recipient uses their private key (d, N) to decrypt: m = c^d mod N.
Continue reading Java Program to Demonstrating RSA

Implementing Product Cipher in Java

In classical cryptography, a product cipher is a cipher that applies multiple transformations (substitution and permutation) to secure the plaintext. The goal is to combine the strengths of various techniques to produce a more secure encryption scheme.

In this post, we’ll walk through the basic concept of a product cipher and demonstrate a simple Java program that implements both encryption and decryption using:

  • Additive Caesar Cipher (a type of substitution cipher)
  • Transposition Cipher (reordering the characters)

By combining these two, we create a more secure scheme than using either one alone.


What is a Product Cipher?

A product cipher involves multiple rounds or stages of encryption where each stage transforms the message using a different method. Common combinations include:

  • Substitution followed by transposition
  • Multiple rounds of substitution and transposition

This layered approach makes it harder to crack using frequency analysis or brute force.

In our Java implementation, we use:

  1. Additive Caesar Cipher: Each character is shifted by a fixed key (k1).
  2. Transposition Cipher: The encrypted text is written row-wise into a matrix and read column-wise using a given number of rows (k2).
Continue reading Implementing Product Cipher in Java