The Wayback Machine - https://web.archive.org/web/20240613170709/https://www.geeksforgeeks.org/latin-alphabet-cipher-encryption/
Open In App

Latin alphabet cipher

Last Updated : 22 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

The Latin Alphabet Cipher Encryption Technique is one of the earliest and simplest techniques of encrypting data. It’s simply a type of substitution cipher technique, i.e., each letter of a given text is substituted by its corresponding number as represented in its alphabetical order. For Example, we have given a string as “hello everyone”, then its Latin Cipher Encryption will be “8 5 12 12 15 5 22 5 18 25 15 14 5”.

Examples: 

Input :  geeksforgeeks
Output : Encrypted Code using Latin Alphabet
         7 5 5 11 19 6 15 18 7 5 5 11 19 

Input :  hello everyone
Output : Encrypted Code using Latin Alphabet
         8 5 12 12 15  5 22 5 18 25 15 14 5  

Prerequisite : isalpha() and isdigit() functions in C/C++ with example

Below is the program to convert a given string to its Latin Alphabet cipher: 

C++




// Latin Alphabet Cipher Encryption header files
#include <bits/stdc++.h>
 
// function for calculating the encryption
void cipher(char str[])
{
    for (int i = 0; str[i] != '\0'; i++) {
        if (isalpha(str[i]) == 0 && str[i] != ' ') {
            printf("Enter only alphabets and space\n");
            return;
        }
    }
 
    printf("Encrypted Code using Latin Alphabet\n");
    for (int i = 0; str[i] != '\0'; i++) {
 
        if (str[i] >= 'A' && str[i] <= 'Z')
            printf("%d ", str[i] - 'A' + 1);
 
        else if (str[i] >= 'a' && str[i] <= 'z')
            printf("%d ", str[i] - 'a' + 1);
        if (str[i] == ' ')
            printf("%c", str[i]);
    }
    printf("\n");
}
 
// driver code
int main()
{
    char str[] = "geeksforgeeks";
    cipher(str);
    return 0;
}


Java




// Java program to demonstrate
// Latin Alphabet Cipher
 
class LatinCipher
{
    // function for calculating the encryption
    static void cipher(String str)
    {
        for (int i = 0; i < str.length(); i++)
        {
            if (!Character.isLetter(str.charAt(i)) &&
            str.charAt(i) != ' ')
            {
                System.out.println("Enter only alphabets and space");
                return;
            }
        }
 
        System.out.println("Encrypted Code using Latin Alphabet");
        for (int i = 0; i < str.length(); i++)
        {
            if (str.charAt(i) >= 'A' && str.charAt(i) <= 'Z')
            {
                System.out.print(str.charAt(i) - 'A' + 1 + " ");
            }
            else if (str.charAt(i) >= 'a' && str.charAt(i) <= 'z')
            {
                System.out.print(str.charAt(i) - 'a' + 1 + " ");
            }
            if (str.charAt(i) == ' ')
                System.out.print(str.charAt(i));
 
        }
        System.out.println();
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        String str = "geeksforgeeks";
        cipher(str);
    }
}
 
// This code is contributed by Vivekkumar Singh


Python3




# Python program to demonstrate
# Latin Alphabet Cipher
 
# function for calculating the encryption
def cipher(str):
 
    for i in range(len(str)):
        if str[i].isalpha() == 0 and str[i] != " ":
            print("Enter only alphabets and space")
            return
 
 
    print("Encrypted Code using Latin Alphabet")
    for i in range(len(str)):
 
        if str[i] >= "A" and str[i] <= "Z":
            print(ord(str[i])-ord("A")+1, end=" ")
         
        elif str[i] >= "a" and str[i] <= 'z':
            print(ord(str[i])-ord("a")+1, end=" ")
         
        if str[i] == " ":
            print(str[i])
     
    print()
 
 
# Driver Code
if __name__ == "__main__":
    str = "geeksforgeeks"
    cipher(str)
 
# This code is contributed by
# sanjeev2552


C#




// C# program to demonstrate
// Latin Alphabet Cipher
using System;
     
public class LatinCipher
{
    // function for calculating the encryption
    static void cipher(String str)
    {
        for (int i = 0; i < str.Length; i++)
        {
            if (!char.IsLetter(str[i]) &&
            str[i] != ' ')
            {
                Console.WriteLine("Enter only alphabets and space");
                return;
            }
        }
 
        Console.WriteLine("Encrypted Code using Latin Alphabet");
        for (int i = 0; i < str.Length; i++)
        {
            if (str[i] >= 'A' && str[i] <= 'Z')
            {
                Console.Write(str[i] - 'A' + 1 + " ");
            }
            else if (str[i] >= 'a' && str[i] <= 'z')
            {
                Console.Write(str[i] - 'a' + 1 + " ");
            }
            if (str[i] == ' ')
                Console.Write(str[i]);
 
        }
        Console.WriteLine();
    }
 
    // Driver Code
    public static void Main(String[] args)
    {
        String str = "geeksforgeeks";
        cipher(str);
    }
}
 
// This code has been contributed by 29AjayKumar


PHP




<?php
// Latin Alphabet Cipher
// Encryption header files
 
// function for calculating
// the encryption
function cipher($str)
{
        if (!ctype_alpha($str))
        {
            printf("Enter only " +
                   "alphabets and space\n");
            return;
        }
 
    printf("Encrypted Code using ");
    printf("Latin Alphabet\n");
    for ($i = 0; $i < strlen($str); $i++)
    {
        if ($str[$i] >= 'A' &&
            $str[$i] <= 'Z')
            echo (ord($str[$i]) -
                    65 + 1). " ";
 
        else if ($str[$i] >= 'a' &&
                 $str[$i] <= 'z')
            echo (ord($str[$i]) -
                    97 + 1). " ";
 
    }
    echo "\n";
}
 
// Driver Code
$str = "geeksforgeeks";
cipher($str);
 
// This code is contributed by mits.
?>


Javascript




<script>
    // JavaScript program to demonstrate
    // Latin Alphabet Cipher
    // function for calculating the encryption
    function cipher(str) {
        for (var i = 0; i < str.length; i++) {
            if (!isLetter(str[i]) && str[i] !== " ") {
                document.write("Enter only alphabets and space");
                return;
              }
        }
 
        document.write("Encrypted Code using Latin Alphabet <br>");
            for (var i = 0; i < str.length; i++) {
                  if (str[i] >= "A" && str[i] <= "Z") {
                    document.write(str[i].charCodeAt(0)
                                   - "A".charCodeAt(0) + 1 + "");
                  }
                  else if (str[i] >= "a" && str[i] <= "z") {
                    document.write(str[i].charCodeAt(0)
                               - "a".charCodeAt(0) + 1 + " ");
                  }
                  if (str[i] == " ")
                    document.write(str[i]);
            }
            document.write("<br>");
      }
 
      //check isLetter
      function isLetter(str) {
            return str.length === 1 && str.match(/[a-z]/i);
      }
 
      // Driver Code
      var str = "geeksforgeeks";
      cipher(str);
</script>


Output

Encrypted Code using Latin Alphabet
7 5 5 11 19 6 15 18 7 5 5 11 19 

Time Complexity: O(n), where n is the length of the given string.
Auxiliary Space: O(1), no extra space is required, so it is a constant.



Similar Reads

Difference between Monoalphabetic Cipher and Polyalphabetic Cipher
1. Monoalphabetic Cipher : A monoalphabetic cipher is any cipher in which the letters of the plain text are mapped to cipher text letters based on a single alphabetic key. Examples of monoalphabetic ciphers would include the Caesar-shift cipher, where each letter is shifted based on a numeric key, and the atbash cipher, where each letter is mapped
2 min read
Check whether a Matrix is a Latin Square or not
Given a square matrix of size N x N, the task is to check if it is Latin square or not. A square matrix is a Latin Square if each cell of the matrix contains one of N different values (in the range [1, N]), and no value is repeated within a row or a column. Examples: Input: 1 2 3 4 2 1 4 3 3 4 1 2 4 3 2 1 Output: YES Input: 2 2 2 2 2 3 2 3 2 2 2 3
8 min read
Encoding a sentence into Pig Latin
Design a program to take a sentence as an input, and then encode it into Pig Latin. A Pig Latin is an encrypted word in English, generated by placing the first letter of each word at the end, and then adding "ay" to the end. Examples: Input: s = "nevermind youve got them"Output: "evermindnay ouveyay otgay hemtay" Input: s = "sally knows best"Output
4 min read
Latin Square
A Latin Square is a n x n grid filled by n distinct numbers each appearing exactly once in each row and column. Given an input n, we have to print a n x n matrix consisting of numbers from 1 to n each appearing exactly once in each row and each column. Examples : Input: 3 Output: 1 2 3 3 1 2 2 3 1 Input: 5 Output: 1 2 3 4 5 5 1 2 3 4 4 5 1 2 3 3 4
6 min read
Encoding a word into Pig Latin
Design a program to take a word as an input, and then encode it into Pig Latin. A Pig Latin is an encrypted word in English, which is generated by doing the following alterations:The first vowel occurring in the input word is placed at the start of the new word along with the remaining alphabet of it. The alphabet is present before the first vowel
5 min read
Block Cipher modes of Operation
Encryption algorithms are divided into two categories based on the input type, as a block cipher and stream cipher. Block cipher is an encryption algorithm that takes a fixed size of input say b bits and produces a ciphertext of b bits again. If the input is larger than b bits it can be divided further. For different applications and uses, there ar
5 min read
Bifid Cipher in Cryptography
This cipher technique considered more secure compared to other substitution algorithms reason being it breaks the message apart into two separate streams and then recombines them. It is a combination of the Polybius square with the transposition and uses fractionation to achieve diffusion. This encrypting technique invented by Felin Delastelle. It
4 min read
Substitution Cipher
Hiding some data is known as encryption. When plain text is encrypted it becomes unreadable and is known as ciphertext. In a Substitution cipher, any character of plain text from the given fixed set of characters is substituted by some other character from the same set depending on a key. For example with a shift of 1, A would be replaced by B, B w
7 min read
Feistel Cipher
Feistel Cipher model is a structure or a design used to develop many block ciphers such as DES. Feistel cipher may have invertible, non-invertible and self invertible components in its design. Same encryption as well as decryption algorithm is used. A separate key is used for each round. However same round keys are used for encryption as well as de
3 min read
Autokey Cipher | Symmetric Ciphers
Autokey Cipher is a polyalphabetic substitution cipher. It is closely related to the Vigenere cipher but uses a different method of generating the key. It was invented by Blaise de VigenÃĻre in 1586. In general, more secure than the Vigenere cipher. Example-1: Plaintext = "HELLO" Autokey = N Ciphertext = "ULPWZ" Example-2: Plaintext = "GEEKSFORGEEKS
4 min read
Practice Tags :