📜  实现一次性算法的Java程序

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

实现一次性算法的Java程序

One Time Pad 算法也称为Vernam Cipher 。它是一种加密字母纯文本的方法。它是将纯文本转换为密文的转置技术之一。在这种机制中,我们为纯文本的每个字符分配一个数字。

分配如下:

ABCDEFGHIJ
0123456789
KLMNOPQRST
10111213141516171819
UVWXYZ
202122232425

密钥与明文的关系:在该算法中,密钥的长度应该等于明文的长度。

例子:

Input:  plain text - HELLO
        Key - MONEY
Output: Cipher - TSYPM
        Message - HELLO
        
Input:  plain text - SAVE
        Key - LIFE  
Output: Cipher - DIAI        
        Message - SAVE

以上解释:

第 1 部分(明文到密文)

密文 → TSYPM

第 2 部分(密文到 Message)

消息 → 你好

Java
// Java program which implements
// one time pad algorithm
  
import java.io.*;
public class GFG {
  
    // function which returns encryptedText
    public static String stringEncryption(String text,
                                          String key)
    {
  
        // initializing cipherText
        String cipherText = "";
  
        // initialize cipher array of key length
        // which stores the sum of corresponding no.'s
        // of plainText and key.
        int cipher[] = new int[key.length()];
        
        for (int i = 0; i < key.length(); i++)
        {
            cipher[i] = text.charAt(i) - 'A' + key.charAt(i)
                        - 'A';
        }
  
        // if the sum is greater than 25
        // subtract 26 from it and store that resulting
        // value
        for (int i = 0; i < key.length(); i++)
        {
            if (cipher[i] > 25)
            {
                cipher[i] = cipher[i] - 26;
            }
        }
  
        // convert the no.'s into integers
        // convert these integers to corresponding
        // characters and add them up to cipherText
        for (int i = 0; i < key.length(); i++) 
        {
            int x = cipher[i] + 'A';
            cipherText += (char)x;
        }
  
        // returning the cipherText
        return cipherText;
    }
  
    // function which returns plainText
    public static String stringDecryption(String s,String key)
    {
        // initializing plainText
        String plainText = "";
  
        // initializing integer array of key length
        // which stores difference of corresponding no.'s of
        // each character of cipherText and key
        int plain[] = new int[key.length()];
  
        // running for loop for each character
        // subtracting and storing in the array
        for (int i = 0; i < key.length(); i++) 
        {
            plain[i]= s.charAt(i) - 'A' - (key.charAt(i) - 'A');
        }
  
        // if the difference is less than 0
        // add 26 and store it in the array.
        for (int i = 0; i < key.length(); i++)
        {
            if (plain[i] < 0) 
            {
                plain[i] = plain[i] + 26;
            }
        }
  
        // convert int to corresponding char
        // add them up to plainText
        for (int i = 0; i < key.length(); i++)
        {
            int x = plain[i] + 'A';
            plainText += (char)x;
        }
  
        // returning plainText
        return plainText;
    }
  
    // main function
    public static void main(String[] args)
    {
  
        // declaration of plain text
        String plainText = "Hello";
  
        // declaration of key
        String key = "MONEY";
  
        // converting plain text to toUpperCase
        // function call to stringEncryption
        // with plainText and key as parameters
        String encryptedText = 
               stringEncryption(plainText.toUpperCase(), key.toUpperCase());
  
        // printing cipher Text
        System.out.println("Cipher Text - "+ encryptedText);
  
        // function call to stringDecryption
        // with encryptedText and key as parameters
        System.out.println("Message - "
                 + stringDecryption(encryptedText,
                                    key.toUpperCase()));
    }
}


输出
Cipher Text - TSYPM
Message - HELLO