Category Archives: Snippets

Demonstrating Digital Signature in Java

In modern cryptography, digital signatures ensure the authenticity and integrity of a message. One of the most widely used techniques for digital signing is based on the RSA algorithm. This post will guide you through the basic concept of how RSA digital signatures work and provide a simple Java implementation to illustrate the signing and verification process.

What is an RSA Digital Signature?

An RSA Digital Signature is a cryptographic method where the sender signs the message using their private key, and the receiver verifies it using the sender’s public key. This ensures:

  • The message is not altered during transit.
  • The message truly comes from the claimed sender.

Here’s how it works conceptually:

  1. The sender selects two large prime numbers p and q.
  2. Computes N = p * q and phi = (p - 1)(q - 1).
  3. Chooses a public exponent e such that 1 < e < phi and gcd(e, phi) = 1.
  4. Computes the private key d = e^(-1) mod phi.
  5. To sign a message m, the sender calculates the signature s = m^d mod N.
  6. To verify, the receiver computes m' = s^e mod N and checks if m' == m.
Continue reading Demonstrating Digital Signature in Java

Implementing Diffie–Hellman Key Exchange Algorithm in Java

In modern cryptography, secure key exchange is essential for private communication over public channels. One of the earliest practical implementations of public key exchange is the Diffie-Hellman Key Exchange. This post will guide you through the concept and a simple Java implementation to illustrate how two parties can securely generate a shared secret key.

What is Diffie-Hellman Key Exchange?

The Diffie-Hellman Key Exchange (developed in 1976) is a method that allows two users to exchange cryptographic keys over a public channel securely. The beauty of this method is that both parties can compute the same secret key independently, which can then be used for encrypting future messages.

Here’s how it works conceptually:

  1. Two parties (say, Alice and Bob) agree on a large prime number p and a primitive root g of that number.
  2. Alice chooses a private key x, computes R1 = g^x mod p and sends R1 to Bob.
  3. Bob chooses a private key y, computes R2 = g^y mod p and sends R2 to Alice.
  4. Alice computes the secret key as k1 = R2^x mod p.
  5. Bob computes the secret key as k2 = R1^y mod p.
  6. Both k1 and k2 will be the same due to properties of modular arithmetic:
    g^(xy) mod p = g^(yx) mod p.
Continue reading Implementing Diffie–Hellman Key Exchange Algorithm in Java

Implementation of Monkey-Banana Problem in Prolog

In this post, we implement the Monkey-Banana Problem in Prolog — a classic AI planning problem used to demonstrate goal-directed reasoning. The scenario: a monkey is in a room with a banana hanging from the ceiling. There is also a stick and a chair in the room. To reach the banana, the monkey must pick up the stick, move the chair under the banana, climb on the chair, and then hit the banana with the stick. The Prolog program models each required action as an interactive predicate and chains them together to achieve the final goal.

How Does the Prolog Solution Work?

Each step in the plan is modelled as a Prolog predicate (take, move, get_on, hit). Each predicate asks the user a yes/no question. The main goal go chains all four predicates together using Prolog’s conjunction operator (,). If every step is confirmed by the user, the goal succeeds and the monkey reaches the banana. If any step fails, Prolog backtracks to the second go clause and reports failure.

Continue reading Implementation of Monkey-Banana Problem in Prolog

Implementation of Inverse Kinematics in Java

In this post, we implement Inverse Kinematics (IK) for a SCARA robot in Java. While Direct Kinematics computes the end-effector position from known joint angles, Inverse Kinematics does the opposite: given a desired end-effector position (W vector), it computes the joint angles required to reach that position. This is the fundamental calculation needed for robot path planning and motion control.

What is Inverse Kinematics?

For a SCARA robot with 4 joints, the IK solution uses geometric and trigonometric relationships derived from the robot’s DH parameters. The inputs are:

  • W — End-effector position vector: [wx, wy, wz, w4, w5, w6]
  • d — Link offsets: [d1, d2, d3, d4]
  • a — Link lengths: [a1, a2, a3, a4]

The output is the set of joint angles q1, q2, q3, q4 that position the end-effector at the specified W coordinates.

Continue reading Implementation of Inverse Kinematics in Java

Implementing HCTM in Java

In this post, we implement the Homogeneous Coordinate Transformation Matrix (HCTM) in Java. HCTM is a fundamental concept in robotics and computer graphics for representing rotation and translation of a coordinate frame in 3D space. Using a 4×4 matrix allows both rotation and translation to be combined into a single matrix multiplication, making it efficient to apply multiple transformations in sequence.

What is HCTM?

A Homogeneous Coordinate Transformation Matrix extends a standard 3D rotation matrix to 4×4 by appending a translation vector and a homogeneous row [0 0 0 1]. This program implements three rotations (Yaw around X-axis, Pitch around Y-axis, Roll around Z-axis) and one translation. Given a rotation angle (in degrees) and a 4-element coordinate frame vector, the program computes the transformed frame using matrix–vector multiplication.

Continue reading Implementing HCTM in Java

Implementing Diagnosis Problem in Prolog

In this post, we implement a medical diagnosis expert system in Prolog. The program asks the user a series of yes/no questions about a patient’s symptoms and attempts to diagnose a disease based on the answers. This is a classic demonstration of how Prolog’s pattern matching and backtracking can model the reasoning process of a rule-based expert system.

How Does the Diagnosis Work?

The Prolog program defines a hypothesis predicate that maps a combination of symptoms to a disease. A symptom predicate interactively queries the user for each symptom. If all required symptoms are confirmed, Prolog unifies the disease variable and reports the diagnosis. If no hypothesis can be satisfied, a fallback clause prints a failure message.

In this example, the only disease defined is common cold, which requires the patient to have both a headache and sneezing. You can easily extend this to cover more diseases by adding more hypothesis and symptom clauses.

Continue reading Implementing Diagnosis Problem in Prolog

Implementing Direct Kinematics in Java

In this post, we implement Direct Kinematics (Forward Kinematics) for a SCARA robot in Java using the Denavit-Hartenberg (DH) convention. Direct kinematics determines the position and orientation of the robot’s end-effector (tool tip) given the joint angles and link parameters. This is the fundamental calculation in robot arm control and simulation.

What is Direct Kinematics?

For a multi-joint robot arm, each link between joints is described by four DH parameters:

  • θ (theta) — Joint angle (rotation about Z-axis)
  • d — Link offset (translation along Z-axis)
  • a — Link length (translation along X-axis)
  • α (alpha) — Link twist (rotation about X-axis)

For each link, a 4×4 General Link Coordinate Transformation Matrix (GLCTM) is computed from its DH parameters. The final end-effector pose is obtained by multiplying all the link matrices together: T04 = T01 × T12 × T23 × T34.

Continue reading Implementing Direct Kinematics in Java