# c++ string function

Write a C++ program

that reads in two strings from standard input: a key and a message msg

that defines a C++ function encrypt(key,msg) that encrypts the plain text message msg using the Vigenère Cipher with secret key. The function should be a void function that changes msg into the cipher text. msg is both an input and an output parameter.

hat outputs the msg and the cipher text.

For example, for input

ABC

The output could be

key is ABC

cipher text isBVAICO

Now, implement the C++ function decrypt(key,ctxt) that decrypts the cipher text. In this case function, ctxt is both an input and an output parameter.

key is ABC

cipher text isBVAICO

### 2 個解答

• 1 十年前
最愛解答

#include <iostream>

#include <windows.h>

using namespace std;

Encrypt

I use reference instead of pointer since reference looks prettier here

void encrypt( const string key, string &msg )

{

for( unsigned int x=0, y=0; x<msg.size(); ++x )

{

char a = msg[x]+( key[y<=key.size()?(y=0)++:y++]-'A' );

if ( a>'Z' || a<'A' ) a -= 'Z'-'A'+1;

msg[x] = a;

}

return;

}

take the second char as example, from 'U' to 'V' by the key 'B'

'B'-'A'=1

'U'+1='V'

so 'U'+('B'-'A')='V'

Since char don't end at 'Z' nor start at 'A', we have to check out of range results and make them go back to 'A' after 'Z'.

What we do is a -= 'Z'-'A'+1 (a = a-'Z'+'A'-1). Deducting 'Z' will make a became the difference between itself and 'Z', then we add 'A' to it. -1 because the alphabet after 'Z' should be 'A' but not 'B' (since we added 'A', say if the value is one behind 'Z', ('Z'+1)-'Z'=1, 1+'A'='B', but should be 'A'.)

Decrypt is similar, just alternate some plus and minus sign is ok.

char a = msg[x]-( key[y>=key.size()?(y=0)++:y++]-'A' );

if ( a>'Z' || a<'A' ) a += 'Z'-'A'+1;

Finally in main()...

......

string key, msg;

getline( cin, key );

getline( cin, msg );

......

and some missed cout to show the results.

• ?
Lv 5
1 十年前

Assumption: Only upper case characters would be entered.

#include

#include

using namespace std;

string Encrypt( const string sMsg, const string sKey ) {

int i, j;

char cEncrypt;

string sEncrypt;

for ( i = 0, j = 0; j < sMsg.length( ); i++, j++ ) {

if ( i == sKey.length( ) ) i = 0;

cEncrypt = ( sMsg.at( j ) + sKey.at( i ) - 'A' );

if ( cEncrypt > 'Z' ) cEncrypt = cEncrypt - 'Z' + 'A' - 1;

sEncrypt.append( 1, cEncrypt );

}

return sEncrypt;

}

string Decrypt( const string sMsg, const string sKey ) {

int i, j;

char cDecrypt;

string sDecrypt;

for ( i = 0, j = 0; j < sMsg.length( ); i++, j++ ) {

if ( i == sKey.length( ) ) i = 0;

cDecrypt = ( sMsg.at( j ) - sKey.at( i ) + 'A' );

if ( cDecrypt < 'A' ) cDecrypt = cDecrypt + 'Z' - 'A' + 1;

sDecrypt.append( 1, cDecrypt );

}

return sDecrypt;

}

int main( void ) {

string sMsg, sKey;

cout << "ENTER KEY : ";

cin >> sKey;

cout << "ENTER PLAIN TEXT : ";

cin >> sMsg;

cout << "CIPHER TEXT : " << Encrypt( sMsg, sKey ) << endl;

cout << "DECIPHERED TEXT : " << Decrypt( Encrypt( sMsg, sKey ), sKey ) << endl;

return 0;

}