Автоключ шифр | Симметричные шифры

Опубликовано: 1 Декабря, 2021

Autokey Cipher - это полиалфавитный шифр замещения. Он тесно связан с шифром Виженера, но использует другой метод генерации ключа. Он был изобретен Блезом де Виженера в 1586 году. В целом, более надежен, чем шифр Виженера.

Пример-1:

 Plaintext = "ПРИВЕТ"
Автоключ = N
Ciphertext = "ULPWZ"

Пример-2:

 Plaintext = "GEEKSFORGEEKS"
Автоключ = P
Ciphertext = "VKIOCXTFXKIOC"

В этом шифре ключ представляет собой поток подключей, который используется для шифрования соответствующего символа в открытом тексте.

Как показано, автоключ добавляется к первому из подключей.

 Поясним пример 1:

Данный простой текст: ПРИВЕТ
Ключ: NHELL

Зашифруем:

Обычный текст (P): ПРИВЕТ
Корреспондентский номер: 7 4 11 11 14     
Ключ (K): NHELL
Корреспондентский номер: 13 7 4 11 11      
                    ---------------------
Применяя формулу: 20 11 15 22 25  

Соответствующий 
Буквы: ULPWZ

Следовательно, зашифрованный текст: ULPWZ

Расшифруем:

Шифрованный текст (C): ULPWZ
Ключ (K): NHELL
                    ---------------------
Применяя формулу: ПРИВЕТ

Следовательно, открытый текст: ПРИВЕТ

Вот код Java для Autokey Cipher.

Ява

// 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;
}
}
 Выход:

Открытый текст: ПРИВЕТ
Зашифрованный: ULPWZ
Расшифровано: HELLO