single table substitution cipher

1. Encryption principle

insert image description here

The alphabet is as follows:
insert image description here
each letter corresponds to a number one by one, so it is very convenient to take the modulus of 26

2. Encryption and decryption algorithm

2.1 Caesar Encryption

2.1.1 Encryption

PHP implementation

<?php
/**
 * ord():字符串变数字
 * chr():数字变字符串
 * 算字符串长度:strlen
 */
function caesarCipher(string $str):string
{
    
    
    $arr = [];
    for ($i = 0;$i < strlen($str);$i++)
    {
    
    
         $chr = ord($str[$i]);
         $chr = ($chr+3) % 122;
         if($chr<97)
         {
    
    
             $chr+=96;
         }
         array_push($arr,chr($chr));
    }
    return implode('',$arr);
}
echo  caesarCipher('axyz');

2.1.2 Decryption

PHP implementation

/**
 * ord()
 * chr()
 * 算字符串长度:strlen
 */
function caesarDecode(string $str):string
{
    
    
    $arr = [];
    for ($i = 0;$i < strlen($str);$i++)
    {
    
    
        $chr = ord($str[$i]);
        $chr = ($chr-3) % 122;
        if($chr<97)
        {
    
    
            $chr+=96;
        }
        array_push($arr,chr($chr));
    }
    return implode('',$arr);
}
echo  caesarCipher('axyz');

2.2 Shift transformation

2.2.1 Encryption

PHP implementation

<?php
function shiftEncryption(string $str,int $k):string
{
    
    
    $arrStr = str_split($str);
    $arrTb = [];
    $array = [];
    for($i = 97;$i<=122;$i++)
    {
    
    
        $array[chr($i)] = $i - 97;
    }
     foreach ($arrStr as $key=>$value)
     {
    
    

         array_push($arrTb,chr( (($key+$k)%26)+97));
     }
     return implode('',$arrTb);
}
echo shiftEncryption('abc',3);

2.2.2 Decryption

PHP implementation

<?php
function shiftEncryption(string $str,int $k):string
{
    
    
    $arrStr = str_split($str);
    $arrTb = [];
    $array = [];
    for($i = 97;$i<=122;$i++)
    {
    
    
        $array[chr($i)] = $i - 97;
    }
    foreach ($arrStr as $key=>$value)
    {
    
    
        if(($array[$value]-$k) > 0) {
    
    
            array_push($arrTb, chr(($array[$value] - $k) + 97));
        }else
        {
    
    
            array_push($arrTb, chr(($array[$value] - $k + 26) + 97));
        }
    }
    return implode('',$arrTb);
}
echo shiftEncryption('aet',3);

C language implementation

2.3 Affine Transformation

The encryption and decryption of affine transformation are:
c = Ea,b(m) ≡ a, + b(mod 26)
m = Da,b© ≡ a^-1(c - b)(mod 26)
where, a, b is the key, which is an integer satisfying 0≤a, b≤25 and gcd(a,26) equal to 1.
Where gcd(a,26) represents the greatest common factor of a and 26, gcd(a,26)=1 represents that a and 26 are relatively prime, a -1 represents the inverse of a, that is, a -1*
a 1mod26 .

2.3.1 Encryption

PHP implementation

/**
 * @param string $str
 * @return string
 * 反射加密
 *
 */
function reflectionEncript(string $str,int $a,int $b):string
{
    
    
    $arr1 = str_split($str);
    $arr2 = [];
    $array = [];
    for($i = 97;$i<=122;$i++)
    {
    
    
        $array[chr($i)] = $i - 97;
    }

    foreach ($arr1 as $key=>$value)
    {
    
    
        $m = $array[$value] * $a + $b;
        $m %= 26;
        $m += 97;
        array_push($arr2,chr($m));
    }
    return implode('',$arr2);
}

echo reflectionEncript('sec',7,21);

2.3.2 Decryption

C language implementation

#include <stdio.h>

int modular_inverse(int a, int m) {
    
    
    int m0 = m, t, q, x0 = 0, x1 = 1;

    if (m == 1)
       return 0;

    while (a > 1) {
    
    
        q = a / m;
        t = m;
        m = a % m;
        a = t;
        t = x0;
        x0 = x1 - q * x0;
        x1 = t;
    }

    if (x1 < 0)
       x1 += m0;

    return x1;
}

void decrypt(char ciphertext[], int a, int b) {
    
    
    int i = 0;
    int inverse_a = modular_inverse(a, 26);
    int length = strlen(ciphertext);
    char plaintext[length];

    for (i = 0; i < length; i++) {
    
    
        int c = (int) ciphertext[i] - 97;
        int m = (inverse_a * (c - b)) % 26;

        if (m < 0)
            m += 26;

        plaintext[i] = (char) (m + 97);
    }

    printf("Plaintext: %s\n", plaintext);
}

int main() {
    
    
    char ciphertext[] = "jgnnq";
    int a = 5;
    int b = 8;

    decrypt(ciphertext, a, b);

    return 0;
}

In this sample code, we use the modular_inverse() function to calculate the inverse. Then, the decrypt() function accepts the ciphertext, parameter a and parameter b, and decrypts each character in turn.
In the main() function, we provide an example ciphertext ("jgnnq"), and the values ​​of parameter a and parameter b (5 and 8). Then, we call the decrypt() function to decrypt and output the result (plaintext).

Guess you like

Origin blog.csdn.net/qq_53568983/article/details/126867501