我的凯撒密码版本有什么问题? pset 2

时间:2016-07-04 09:47:38

标签: c cs50 caesar-cipher

Caesar cypher使用用户定义的键和文本加密文本。

在密码学中,凯撒密码(也称为凯撒密码),移位密码,凯撒代码或凯撒变换,是最简单和最广为人知的加密技术之一。它是一种替换密码,其中明文中的每个字母都被字母表中的一些固定数量的位置的字母替换。例如,左移3,D将被A替换,E将变为B,依此类推。该方法以Julius Caesar命名,他在私人通信中使用了它

#include <stdio.h>
#include <cs50.h>
#include <ctype.h>


int main ( int argc , string argv[] )
{    
  int key,save;
  string s ;

  key = atoi(argv[1]);    
  s = GetString();

  if (  argc != 2 )
  {
    printf("prgram is yelling at you !!");
    return 1 ;
  }

  for ( int i = 0  ; s[i]!='\0' ; ++i)  // manipulation without storing character
  {
    if ( isalpha( s[i]) )           // checks whether input is in character set or not
    {
      if ( islower ( s[i] ) )       // FOR LOWER CASES
      {
        save = key % 24 ;
        s[i] = s[i] + save ;

        if ( s[i] > 'z' )
          s[i] = 'a' + ( s[i] - 'z' -1 );
      }

      if ( isupper ( s[i] ) )        // FOR UPPER CASES
      {
        save = key  % 24 ;
        s[i] =  s[i] + save ;

        if ( s[i] > 'Z' )
          s[i] = 'A' + ( s[i] - 'Z' -1 );
      }
    }

    printf ( "%c" , s[i] );
  }

  return 0 ;
}

事实:

:) caesar.c exists 
:) caesar.c compiles
:( encrypts "a" as "b" using 1 a s key
   \ expected output, but not "b" 
:( encrypts "barfoo" as "yxocll" using 23 as key
   \ expected output, but not "yxc"
:( encrypts "BARFOO" as "EDUIRR" using 3 as key
   \ expected output, but not "EDUIRR"
   :( encrypts "BaRFoo" as "FeVJss" using 4 as key
      \ expected output, but not "FeVJss"
   :( encrypts "barfoo" as "onesbb" using 65 as key
        \ expected output, but not "srw"
   :( encrypts "world, say hello!" as "iadxp, emk tqxxa!" using   12 as key
       \ expected output, but not "adxp, em tqxxa!"

   :( handles lack of argv[1]
        \ expected output, not standard error of \      "/opt/sandbox50/bin/run.sh: line 31: 189..."

2 个答案:

答案 0 :(得分:0)

问题在于您使用char类型来存储对于该类型而言过大的中间计算,而不是更大的类型。这个计算:

s[i] = s[i] + save ;

由于整数提升,将在类型int中完成添加,但随后它将被分配回类型char,结果将是实现定义。它可能会回绕并给出无效值。

要解决此问题,请使用int来存储

的结果
int temp = s[i] + save;
if ( temp > 'z' )
    temp = 'a' + ( temp - 'z' -1 );   
s[i] = temp;

答案 1 :(得分:0)

我在Swift中使用Xcode操场编写了一个小凯撒密码例程,看看它,希望它有所帮助:

            import UIKit

            let letters:[String] = ["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"]

            var message: String = "HERE GOES THE TEXT THAT YOU WANT TO ENCRYPT"

            func encryptMessage(message: String, shift: Int) -> String {
                var count = 0
                var letterIndex = 0
                var encryptedMessage = ""
                for i in message.stringByReplacingOccurrencesOfString(" ", withString: "").characters {
                    letterIndex = letters.indexOf(String(i))! + shift
                    if letterIndex >= 26 {
                        letterIndex = letterIndex - 26
                    }
                    count = count + 1
                    encryptedMessage += letters[letterIndex]
                }
                return encryptedMessage
            }

            func decryptMessage(message: String, shift: Int) -> String {
                var count = 0
                var letterIndex = 0
                var decryptedMessage = ""
                for i in message.stringByReplacingOccurrencesOfString(" ", withString: "").characters {
                    letterIndex = letters.indexOf(String(i))! - shift
                    if letterIndex < 0 {
                        letterIndex = 26 + letterIndex
                    }
                    count = count + 1
                    decryptedMessage += letters[letterIndex]
                }
                return decryptedMessage
            }
            // Encrypt Message By Shifting 5 positions to the right in the alphabet letters array (A becomes F, B becomes G...)
            print(encryptMessage(message,shift: 5))
            // Decrypt Message By Shifting 5 positions to the left in the alphabet letters array (F becomes A, G becomes B...)
            print(decryptMessage(encryptMessage(message,shift: 5),shift: 5))