📜  自动密钥密码 |对称密码

📅  最后修改于: 2022-05-13 01:57:05.973000             🧑  作者: Mango

自动密钥密码 |对称密码

Autokey Cipher是一种多表替换密码。它与 Vigenere 密码密切相关,但使用不同的方法生成密钥。它是由 Blaise de Vigenère 于 1586 年发明的。一般来说,它比 Vigenere 密码更安全。

示例 1:

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

示例 2:

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

在这个密码中,密钥是一个子密钥流,用于加密明文中的相应字符。

如图所示,自动键被添加到第一个子键中。

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 

这是 Autokey Cipher 的Java代码。

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