Vernam Cipher 或 One Time Pad 算法的实现
One Time Pad 算法也称为Vernam Cipher 。它是一种加密字母纯文本的方法。它是将纯文本转换为密文的替代技术之一。在这种机制中,我们为纯文本的每个字符分配一个数字。
那作业 如下:
A | B | C | D | E | F | G | H | I | J |
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
K | L | M | N | O | P | Q | R | S | T |
10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 |
U | V | W | X | Y | Z | ||||
20 | 21 | 22 | 23 | 24 | 25 |
密钥与明文的关系:在该算法中,密钥的长度应与明文的长度相等。
例子:
Input: Message = HELLO, Key = MONEY
Output: Cipher – TSYPM, Message – HELLO
Explanation:
Part 1: Plain text to Ciphertext
Plain text — H E L L O → 7 4 11 11 14
Key — M O N E Y → 12 14 13 4 24
Plain text + key → 19 18 24 15 38 → 19 18 24 15 12 (= 38 – 26)
Cipher Text → T S Y P M
Part 2: Ciphertext to Message
Cipher Text — T S Y P M → 19 18 24 15 12
Key — M O N E Y→ 12 14 13 4 24
Cipher text – key → 7 4 11 11 -12 → 7 4 11 11 14
Message → H E L L O
Input: Message = SAVE, Key = LIFE
Output: Cipher – DIAI
Message – SAVE
下面是 Vernam Cipher 的实现:
Java
// Java program Implementing One Time Pad Algorithm
// Importing required classes
import java.io.*;
// Main class
public class GFG {
// Method 1
// Returning encrypted text
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;
}
}
// Converting 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;
}
// Method 2
// Returning plain text
public static String stringDecryption(String s,
String key)
{
// Initializing plain text
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;
}
}
// Converting 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;
}
// Method 3
// Main driver method
public static void main(String[] args)
{
// Declaring plain text
String plainText = "Hello";
// Declaring 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);
// Calling above method to stringDecryption
// with encryptedText and key as parameters
System.out.println(
"Message - "
+ stringDecryption(encryptedText,
key.toUpperCase()));
}
}
Cipher Text - TSYPM
Message - HELLO