# 算法设计：如何理解和实现关键字密码？

2021年3月19日13:02:54 发表评论 642 次浏览

## 本文概述

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

K R Y P T O S A B C D E F G H I J L M N Q U V W X Z

``````Input :
Keyword : secret
Message : Zombie Here
Output :
Ciphered String : ZLJEFT DTOT

Take the first example, we used "secret" keyword there.
Plain Text : A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
When "secret" keyword is used, the new encypting text becomes :
Encrypting : S E C R T A B D F G H I J K L M N O P Q U V W X Y Z
This means 'A' means 'S', 'B' means 'E' and 'C' means 'C' and so on.
Lets encode the given message "Zombie Here"
ZOMBIE HERE becomes ZLJEFT DTOT

Input :
Keyword : Star War
Message : Attack at dawn
Output :
Ciphered String : SPPSAG SP RSVJ``````

• 所有消息均以大写形式编码。
• 尽管可以在其中输入空格, 但关键字中并未考虑空格, 特殊字符和数字。
• 在加密消息时, 空格, 特殊字符和数字不受影响。

## C ++

``````// CPP program for encoding the string
// using classical cipher

#include<bits/stdc++.h>
using namespace std;

// Function generates the encoded text
string encoder(string key)
{
string encoded = "" ;
// This array represents the
// 26 letters of alphabets
bool arr[26] = {0};

// This loop inserts the keyword
// at the start of the encoded string
for ( int i=0; i<key.size(); i++)
{
if (key[i] >= 'A' && key[i] <= 'Z' )
{
// To check whether the character is inserted
// earlier in the encoded string or not
if (arr[key[i]-65] == 0)
{
encoded += key[i];
arr[key[i]-65] = 1;
}
}
else if (key[i] >= 'a' && key[i] <= 'z' )
{
if (arr[key[i]-97] == 0)
{
encoded += key[i] - 32;
arr[key[i]-97] = 1;
}
}
}

// This loop inserts the remaining
// characters in the encoded string.
for ( int i=0; i<26; i++)
{
if (arr[i] == 0)
{
arr[i]=1;
encoded += char (i + 65);
}
}
return encoded;
}

// Function that generates encodes(cipher) the message
string cipheredIt(string msg, string encoded)
{
string cipher= "" ;

// This loop ciphered the message.
// Spaces, special characters and numbers remain same.
for ( int i=0; i<msg.size(); i++)
{
if (msg[i] >= 'a' && msg[i] <= 'z' )
{
int pos = msg[i] - 97;
cipher += encoded[pos];
}
else if (msg[i] >= 'A' && msg[i] <= 'Z' )
{
int pos = msg[i] - 65;
cipher += encoded[pos];
}
else
{
cipher += msg[i];
}
}
return cipher;
}

// Driver code
int main()
{
// Hold the Keyword
string key;
key = "Computer" ;
cout << "Keyword : " <<key << endl;

// Function call to generate encoded text
string encoded = encoder(key);

// Message that need to encode
string message = "lsbin" ;
cout << "Message before Ciphering : " << message << endl;

// Function call to print ciphered text
cout << "Ciphered Text : " << cipheredIt(message, encoded) << endl;

return 0;
}``````

## Java

``````// Java program for encoding the string
// using classical cipher

class GFG
{

// Function generates the encoded text
static String encoder( char [] key)
{
String encoded = "" ;

// This array represents the
// 26 letters of alphabets
boolean [] arr = new boolean [ 26 ];

// This loop inserts the keyword
// at the start of the encoded string
for ( int i = 0 ; i < key.length; i++)
{
if (key[i] >= 'A' && key[i] <= 'Z' )
{
// To check whether the character is inserted
// earlier in the encoded string or not
if (arr[key[i] - 65 ] == false )
{
encoded += ( char ) key[i];
arr[key[i] - 65 ] = true ;
}
}
else if (key[i] >= 'a' && key[i] <= 'z' )
{
if (arr[key[i] - 97 ] == false )
{
encoded += ( char ) (key[i] - 32 );
arr[key[i] - 97 ] = true ;
}
}
}

// This loop inserts the remaining
// characters in the encoded string.
for ( int i = 0 ; i < 26 ; i++)
{
if (arr[i] == false )
{
arr[i] = true ;
encoded += ( char ) (i + 65 );
}
}
return encoded;
}

// Function that generates encodes(cipher) the message
static String cipheredIt(String msg, String encoded)
{
String cipher = "" ;

// This loop ciphered the message.
// Spaces, special characters and numbers remain same.
for ( int i = 0 ; i < msg.length(); i++)
{
if (msg.charAt(i) >= 'a' && msg.charAt(i) <= 'z' )
{
int pos = msg.charAt(i) - 97 ;
cipher += encoded.charAt(pos);
}
else if (msg.charAt(i) >= 'A' && msg.charAt(i) <= 'Z' )
{
int pos = msg.charAt(i) - 65 ;
cipher += encoded.charAt(pos);
}
else
{
cipher += msg.charAt(i);
}
}
return cipher;
}

// Driver code
public static void main(String[] args)
{
// Hold the Keyword
String key;
key = "Computer" ;
System.out.println( "Keyword : " + key);

// Function call to generate encoded text
String encoded = encoder(key.toCharArray());

// Message that need to encode
String message = "lsbin" ;
System.out.println( "Message before Ciphering : " + message);

// Function call to print ciphered text
System.out.println( "Ciphered Text : " + cipheredIt(message, encoded));
}
}

// This code is contributed by 29AjayKumar``````

## C#

``````// C# program for encoding the string
// using classical cipher
using System;

class GFG
{

// Function generates the encoded text
static String encoder( char [] key)
{
String encoded = "" ;

// This array represents the
// 26 letters of alphabets
Boolean[] arr = new Boolean[26];

// This loop inserts the keyword
// at the start of the encoded string
for ( int i = 0; i < key.Length; i++)
{
if (key[i] >= 'A' && key[i] <= 'Z' )
{
// To check whether the character is inserted
// earlier in the encoded string or not
if (arr[key[i] - 65] == false )
{
encoded += ( char ) key[i];
arr[key[i] - 65] = true ;
}
}
else if (key[i] >= 'a' && key[i] <= 'z' )
{
if (arr[key[i] - 97] == false )
{
encoded += ( char ) (key[i] - 32);
arr[key[i] - 97] = true ;
}
}
}

// This loop inserts the remaining
// characters in the encoded string.
for ( int i = 0; i < 26; i++)
{
if (arr[i] == false )
{
arr[i] = true ;
encoded += ( char ) (i + 65);
}
}
return encoded;
}

// Function that generates encodes(cipher) the message
static String cipheredIt(String msg, String encoded)
{
String cipher = "" ;

// This loop ciphered the message.
// Spaces, special characters and numbers remain same.
for ( int i = 0; i < msg.Length; i++)
{
if (msg[i] >= 'a' && msg[i] <= 'z' )
{
int pos = msg[i] - 97;
cipher += encoded[pos];
}
else if (msg[i] >= 'A' && msg[i] <= 'Z' )
{
int pos = msg[i] - 65;
cipher += encoded[pos];
}
else
{
cipher += msg[i];
}
}
return cipher;
}

// Driver code
public static void Main(String[] args)
{
// Hold the Keyword
String key;
key = "Computer" ;
Console.WriteLine( "Keyword : " + key);

// Function call to generate encoded text
String encoded = encoder(key.ToCharArray());

// Message that need to encode
String message = "lsbin" ;
Console.WriteLine( "Message before Ciphering : " + message);

// Function call to print ciphered text
Console.WriteLine( "Ciphered Text : " + cipheredIt(message, encoded));
}
}

/* This code contributed by PrinciRaj1992 */``````

``````Keyword : Computer
Message before Ciphering : lsbin
Ciphered Text : ``````

``````Input :
Keyword : secret
Message : zljeft dtOT
Output :
Deciphered String : ZOMBIE HERE

Input :
Keyword : joker0O7hack123
Message : QjTijl
Output :
Deciphered String : BATMAN``````
``````// CPP program for decoding the string
// which generate using classical cipher

#include<bits/stdc++.h>
using namespace std;

// Original Set of letters
string plaintext = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;

// Function generates the encoded text
string encoder(string key)
{
string encoded = "" ;
bool arr[26] = {0};

// This loop inserts the keyword
// at the start of the encoded string
for ( int i=0; i<key.size(); i++)
{
if (key[i] >= 'A' && key[i] <= 'Z' )
{
// To check whether the character is inserted
// earlier in the encoded string or not
if (arr[key[i]-65] == 0)
{
encoded += key[i];
arr[key[i]-65] = 1;
}
}
else if (key[i] >= 'a' && key[i] <= 'z' )
{
if (arr[key[i]-97] == 0)
{
encoded += key[i] - 32;
arr[key[i]-97] = 1;
}
}
}

// This loop inserts the remaining
// characters in the encoded string.
for ( int i=0; i<26; i++)
{
if (arr[i] == 0)
{
arr[i]=1;
encoded += char (i + 65);
}
}
return encoded;
}

// This function will decode the message
string decipheredIt(string msg, string encoded)
{
// Hold the position of every character (A-Z)
// from encoded string
map < char , int > enc;
for ( int i=0; i<encoded.size(); i++)
{
enc[encoded[i]]=i;
}

string decipher= "" ;

// This loop deciphered the message.
// Spaces, special characters and numbers remain same.
for ( int i=0; i<msg.size(); i++)
{
if (msg[i] >= 'a' && msg[i] <= 'z' )
{
int pos = enc[msg[i]-32];
decipher += plaintext[pos];
}
else if (msg[i] >= 'A' && msg[i] <= 'Z' )
{
int pos = enc[msg[i]];
decipher += plaintext[pos];
}
else
{
decipher += msg[i];
}
}
return decipher;
}

// Driver code
int main()
{
// Hold the Keyword
string key;
key = "Computer" ;
cout << "Keyword : " << key << endl;

// Function call to generate encoded text
string encoded = encoder(key);

// Message that need to decode
string message = "EUUDN TIL EUUDN" ;
cout << "Message before Deciphering : " << message << endl;

// Function call to print deciphered text
cout << "Deciphered Text : " << decipheredIt(message, encoded) << endl;

return 0;
}``````

``````Keyword : Computer
Message before Deciphering : EUUDN TIL EUUDN
Deciphered Text : GEEKS FOR GEEKS``````