Cifrado de clave automática | Cifrados simétricos

Autokey Cipher es un cifrado de sustitución polialfabético. Está estrechamente relacionado con el cifrado Vigenere, pero utiliza un método diferente para generar la clave. Fue inventado por Blaise de Vigenère en 1586. En general, más seguro que el cifrado Vigenère .

Ejemplo 1:

Plaintext = "HELLO"
Autokey = N
Ciphertext = "ULPWZ" 

Ejemplo-2:

Plaintext = "GEEKSFORGEEKS"
Autokey = P
Ciphertext = "VKIOCXTFXKIOC" 

En este cifrado, la clave es un flujo de subclaves que se utiliza para cifrar el carácter correspondiente en el texto sin formato.

Como se muestra, la autoclave se agrega a la primera de las subclaves.

Let's explain Example 1:

Given plain text is : H E L L O
Key is              : N H E L L

Let's encrypt:

Plain Text(P)       : H   E   L   L   O
Corresponding Number: 7   4   11  11  14     
Key(K)              : N   H   E   L   L
Corresponding Number: 13  7   4   11  11      
                    ---------------------
Applying the formula: 20  11  15  22  25  

Corresponding 
Letters are         : U    L   P   W   Z

Hence Ciphertext is: ULPWZ

Let's decrypt:

Cipher Text(C)      : U   L   P   W   Z
Key(K)              : N   H   E   L   L
                    ---------------------
Applying the formula: H   E   L   L   O

Hence Plaintext is: HELLO 

Aquí está el código Java para Autokey Cipher.

Java

// A JAVA program to illustrate
// Autokey Cipher Technique
  
// Importing required library
import java.lang.*;
import java.util.*;
  
public class AutoKey {
  
    private static final String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  
    public static void main(String[] args)
    {
        String msg = "HELLO";
        String key = "N";
  
        // This if statement is all about java regular expression
        // [] for range
        // // Extra \ is used to escape one \
        // \\d acts as delimiter
        // ? once or not at all
        // . Any character (may or may not match line terminators)
        if (key.matches("[-+]?\\d*\\.?\\d+"))
            key = "" + alphabet.charAt(Integer.parseInt(key));
        String enc = autoEncryption(msg, key);
  
        System.out.println("Plaintext : " + msg);
        System.out.println("Encrypted : " + enc);
        System.out.println("Decrypted : " + autoDecryption(enc, key));
    }
  
    public static String autoEncryption(String msg, String key)
    {
        int len = msg.length();
  
        // generating the keystream
        String newKey = key.concat(msg);
        newKey = newKey.substring(0, newKey.length() - key.length());
        String encryptMsg = "";
  
        // applying encryption algorithm
        for (int x = 0; x < len; x++) {
            int first = alphabet.indexOf(msg.charAt(x));
            int second = alphabet.indexOf(newKey.charAt(x));
            int total = (first + second) % 26;
            encryptMsg += alphabet.charAt(total);
        }
        return encryptMsg;
    }
  
    public static String autoDecryption(String msg, String key)
    {
        String currentKey = key;
        String decryptMsg = "";
  
        // applying decryption algorithm
        for (int x = 0; x < msg.length(); x++) {
            int get1 = alphabet.indexOf(msg.charAt(x));
            int get2 = alphabet.indexOf(currentKey.charAt(x));
            int total = (get1 - get2) % 26;
            total = (total < 0) ? total + 26 : total;
            decryptMsg += alphabet.charAt(total);
            currentKey += alphabet.charAt(total);
        }
        return decryptMsg;
    }
}
Output:

Plaintext : HELLO
Encrypted : ULPWZ
Decrypted : HELLO

Publicación traducida automáticamente

Artículo escrito por AmiyaRanjanRout y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *