Skip to main content
Unlocking the Secrets of Ciphering: A Beginner’s Guide to Cryptography
  1. Blog/
  2. Cybersecurity/
  3. Cryptography/

Unlocking the Secrets of Ciphering: A Beginner’s Guide to Cryptography

·602 words·3 mins·
Deadnaut
Author
Deadnaut
Documenting cybersecurity challenges, CTF writeups, and penetration testing insights from the digital frontier.
Table of Contents

Introduction
#

In an increasingly digital world, protecting information has never been more critical. One of the oldest and most effective ways to secure data is through ciphering — the process of converting plain information into unreadable text using cryptographic techniques. In this post, we’ll explore the basics of ciphering, its historical significance, and its modern applications, complete with C code examples.


Alt text

What Is Ciphering?
#

Ciphering is the process of transforming readable information (plaintext) into an encoded format (ciphertext) to protect it from unauthorized access.

Below is a simple example in C that uses a basic substitution cipher to encrypt a message by shifting each character:

Alt text

#include <stdio.h>
#include <string.h>

void encryptMessage(char *message, int shift) {
    for (int i = 0; i < strlen(message); i++) {
        if (message[i] >= 'A' && message[i] <= 'Z') {
            message[i] = ((message[i] - 'A' + shift) % 26) + 'A';
        } else if (message[i] >= 'a' && message[i] <= 'z') {
            message[i] = ((message[i] - 'a' + shift) % 26) + 'a';
        }
    }
}

int main() {
    char message[] = "HelloWorld";
    int shift = 3;
    encryptMessage(message, shift);
    printf("Encrypted Message: %s\n", message);
    return 0;
}

Classic Example: Caesar Cipher in C
#

Here’s a practical implementation of the classic Caesar cipher:

#include <stdio.h>
#include <string.h>

void caesarCipher(char *text, int shift) {
    for (int i = 0; i < strlen(text); i++) {
        if (text[i] >= 'A' && text[i] <= 'Z') {
            text[i] = ((text[i] - 'A' + shift) % 26) + 'A';
        } else if (text[i] >= 'a' && text[i] <= 'z') {
            text[i] = ((text[i] - 'a' + shift) % 26) + 'a';
        }
    }
}

int main() {
    char text[] = "SimpleText";
    int shift = 5;
    caesarCipher(text, shift);
    printf("Encrypted Caesar Cipher: %s\n", text);
    return 0;
}

Types of Ciphers
#

Substitution Cipher
#

Alt text
#include <stdio.h>

void simpleSubstitution(char *text) {
    for (int i = 0; text[i] != '\0'; i++) {
        text[i] ^= 0x20; 
    }
}

int main() {
    char text[] = "HelloCipher";
    simpleSubstitution(text);
    printf("After Substitution Cipher: %s\n", text);
    return 0;
}

Transposition Cipher
#

Alt text
#include <stdio.h>
#include <string.h>

void reverseCipher(char *text) {
    int len = strlen(text);
    for (int i = 0; i < len / 2; i++) {
        char temp = text[i];
        text[i] = text[len - i - 1];
        text[len - i - 1] = temp;
    }
}

int main() {
    char text[] = "CipherExample";
    reverseCipher(text);
    printf("Transposition Cipher: %s\n", text);
    return 0;
}

Importance of Ciphering
#

Ciphering ensures secure communication by making messages unreadable to unauthorized parties.

Here’s an example using an XOR-based cipher in C:

#include <stdio.h>
#include <string.h>

void xorCipher(char *text, char key) {
    for (int i = 0; i < strlen(text); i++) {
        text[i] ^= key;
    }
}

int main() {
    char text[] = "SensitiveData";
    char key = 'K';

    xorCipher(text, key);
    printf("Ciphered Text: %s\n", text);
    xorCipher(text, key);
    printf("Deciphered Text: %s\n", text);
    return 0;
}

Applications of Ciphering
#

One basic example is simple password hashing using bitwise operations in C:

#include <stdio.h>
#include <string.h>

unsigned int hashPassword(char *password) {
    unsigned int hash = 0;
    for (int i = 0; i < strlen(password); i++) {
        hash = (hash << 5) + password[i];
    }
    return hash;
}

int main() {
    char password[] = "MySecurePassword";
    unsigned int hash = hashPassword(password);
    printf("Password Hash: %u\n", hash);
    return 0;
}

Ciphering is fundamental to many areas of cybersecurity, ensuring confidentiality, integrity, and privacy in data transmission and storage.


The Future of Ciphering
#

As technology evolves, the need for more advanced cryptographic techniques becomes evident. Quantum-resistant algorithms are becoming increasingly important. While such advanced methods are beyond what simple C code can demonstrate, staying informed is essential.

Alt text