需要帮助确保文本移动'x'的空间量被转换为字母,而不是随机符号 [英] Need help ensuring text shifted 'x' amount of spaces is transformed into letters, not random symbols

查看:223
本文介绍了需要帮助确保文本移动'x'的空间量被转换为字母,而不是随机符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在加密一个页面上,并决定加入一个凯撒密码(Caesar Cipher),一种方法是在左边或右边换一个字母X。示例:使用shift参数为2加密字母A。A => C.



下面的代码在某种程度上正在进行。它可以加密/解密我输入的文本,只要它在某些ASCII值之间。



ASCII中的值为65-90的是大写字母,而97-122是小写字母。我的问题是如果我的shift参数如此之大,那我超过这些间隔。如果我尝试这样做,字母A就成为这个字符(



同时解密您的加密数据,您将需要使用下一个公式:





其中En(x)表示加密的char,x = char, n =转移值;



公式图片来源[维基百科]






然而,这个公式对人类来说相当不错,但不适用于电脑,人类只有26个拉丁字符,而计算机有ASCII代表128,我们也不关心这个字母的情况,所以Hello = heLLo对于我们来说,而与代表97和A的计算机不一样



所以您需要将ASCII转换为简单的1-26个字符范围。



here我将在您的加密部分实现一个简单的编辑,希望能帮助您实现对代码部分进行解密的其余部分。 ($ i = 0,$ n = strlen($ string);

  $ i< $ N; $ i ++){
$ char = $ string [$ i];
//这里我们检查字符类型
//来设置稍后使用的范围
if(ctype_upper($ char)){
$ range = 65;
} else if(ctype_lower($ char)){
$ range = 97;
}
//将ascii索引字符转换为字母索引字符
$ ascii = ord($ char) - $ range;
//基于字母索引字符的caesar folrmula
$ shifChar = chr((($ ascii + $ shift)%26)+ $ range);
$ shifString。= $ shifChar;
}
echo $ shifString;

如果你想确保只有字母字符被编码,你可以使用 ctype_alpha 来检查,如果字符是字母字符,则返回true。



同样重要的是要注意,在解密Caesar-cipher时,您将需要如前所述使用下一个公式:

  $ shifChar = chr((($ ascii  -  $ shift)%26)+ $ range); 
// ^注意这个

还可以更改您的范围如下:

  if(ctype_upper($ char)){
$ range = 90; //从Z
开始else else(ctype_lower($ char)){
$ range = 122; //从z
开始






另一个非常有用的来源是 from cs50 ,这是一个凯撒密码C教程,但它具有相同的概念。


I'm working on a page on cryptography, and have decided to include a Caesar Cipher, a method in which you shift a letter 'X' amount of spaces left or right. Example: Encrypting the letter 'A' with a shift parameter of 2. A => C.

The code below is working somewhat decently. It's able to encrypt/decrypt the text I enter, as long as it's between certain ASCII values.

The values 65-90 in ASCII is uppercase letters, and 97-122 is lowercase letters. My problem is if my shift parameter is so large, that I exceed these intervals.

Let's say I have the letter 'A' which I want to encrypt with a shift parameter of 100. If I try this, the letter 'A' becomes this character (http://www.theasciicode.com.ar/ascii-codes/spanish-enye-capital-letter-n-tilde-enie-uppercase-ascii-code-165.gif), which can't show up in my browser, and is converted into a weird question mark.

My question is, what are my options for avoiding these weird symbols? Is it possible to make it so it only includes letters (uppercase and lowercase) and maybe numbers as well?

I hope you can help me. I will be online all day, so if you need any more information, just ask :-)

My form field:

<form method="post">
<input type="text" name="textCaesarEncrypt" autocomplete="off" placeholder="Indtast tekst">
<input type="number" name="shiftCaesarEncrypt" autocomplete="off">
<label>Krypter</label>
<input type="radio" name="caesarEncryptOrDecrypt" value="caesarEncrypt">
<label>Dekrypter</label>
<input type="radio" name="caesarEncryptOrDecrypt" value="caesarDecrypt">
<input type="submit" name="submitCaesarEncrypt" value="Videre">

My PHP:

    <?php
        $caesarEncryptOrDecrypt = $_POST ["caesarEncryptOrDecrypt"];

     if (!empty($_POST['textCaesarEncrypt']) AND 
     !empty($_POST['shiftCaesarEncrypt'])){

$string = $_POST['textCaesarEncrypt'];
$shift = $_POST['shiftCaesarEncrypt'];
$shiftedString = "";


if($caesarEncryptOrDecrypt == "caesarEncrypt") {
    for ($i = 0; $i < strlen($string); $i++) {
        $ascii = ord($string[$i]);
        $shiftedChar = chr($ascii + $shift);

        $shiftedString .= $shiftedChar;
    }
    echo $shiftedString;
}

if($caesarEncryptOrDecrypt == "caesarDecrypt") {
    for ($i = 0; $i < strlen($string); $i++) {
        $ascii = ord($string[$i]);
        $shiftedChar = chr($ascii - $shift);

        $shiftedString .= $shiftedChar;
    }
    echo $shiftedString;


}

}   
?>

解决方案

First of all, it's important to mention that Caesar-Cipher is not recommended, it's too weak, weak, weak, too weak and pretty easy to be broken.

for sure you don't want to use an encrypting system which was used before more that 2000 years

Julius Caesar: 13 July 100 BC – 15 March 44 BC)


However, The key here is in the up next formula -in the encrypting case-:

while to decrypt your encrypted data , you will need to use the next formula :

where En(x) refers to encrypted char, x = char, n = shift value;

formulas images source [wikipedia]


However this formula is pretty good for humans, but not for computers , humans only have 26 Latin character, while computers have ASCII which represented by 128, also we don't care about the case of the letter, so Hello = heLLo for us, while it is not the same with computers which represents a as 97 and A as 65.

so you will need to convert your ASCII to easy 1-26 range of character.

here i will implement a simple edit in your encrypting part, and I hope that helps you to implement the rest of decrypting part of your code.

for ($i = 0, $n = strlen($string); $i < $n; $i++) {
    $char = $string[$i];
    // here we are checking the character type
    // to set the range which will be used later
    if (ctype_upper($char)) {
        $range = 65;
    } else if (ctype_lower($char)) {
        $range = 97;
    }
    // convert the ascii indexed chars into alphabetic indexed chars
    $ascii = ord($char) - $range;
    // the caesar folrmula based on alphabetic indexed chars
    $shiftedChar = chr((($ascii + $shift) % 26) + $range);
    $shiftedString .= $shiftedChar;
}
echo $shiftedString;

If you want to make sure that only alphabetic characters will be encoded, you may use ctype_alpha to check that, it's returns true if the character is alphabetic chars.

It's also important to notice that in decrypting Caesar-cipher , you will need as mentioned before to use the next formula :

$shiftedChar = chr((($ascii - $shift) % 26) + $range);
//                          ^ notice this

also to change your ranges as follows :

if (ctype_upper($char)) {
    $range = 90; // start with Z
} else if (ctype_lower($char)) {
    $range = 122; // start with z
}


another great helpful source from cs50 , this is a Caesar-cipher C tutorial , but it has the same concept.

这篇关于需要帮助确保文本移动'x'的空间量被转换为字母,而不是随机符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆