Caesars Cipher (algorithm)

Source: Internet
Author: User

Topic

Let God be God, Caesar's Caesar.

Here we introduce the world's most popular Caesar password Caesar cipher, also known as the shift password.

The shift password, which is the letter in the password, is shifted according to the specified number.

A common case is the ROT13 password, which shifts the letters to 13 positions. By ' A '? ' N ', ' B '? ' O ', and so on.

Write a ROT13 function, implement input encryption string, output decrypt string.

All letters are uppercase, do not convert any non-alphabetic characters (for example: spaces, punctuation marks), encounter these special characters, skip them.

Tips

String.charcodeat ()

String.fromCharCode ()

Ideas

The core of the subject lies in the hints given in the two methods, we come to understand each.

String.prototype.charCodeAt ()

Grammar:

Str. charCodeAt (Index)

Example:

"ABC". charCodeAt (//returns

The previous example returns 65, which is the Unicode value of a.

String.fromCharCode ()

Grammar:

String. fromCharCode (NUM1, ..., numn)

The method returns a string instead of a string object.

Because fromCharCode is a static method of String, it should be used like this: String.fromCharCode () instead of as a method of the string object you create.

Example:

String. fromCharCode (+/-returns "ABC"    )

Okay, back to the title. The strings passed in are uppercase, while the uppercase letters A through Z Unicode values are in ascending order. The core of the cryptographic algorithm is the first 13 alphabetic Unicode values plus 13, and the last 13 alphabetic Unicode values minus 13 from the alphabet back. The characters, such as whitespace symbols other than uppercase letters, remain intact.

Solution
functionROT13 (STR) {LBH Qvq vg!var index =Nullvar temp ="";var _a ="A". charCodeAt (0);var _z = "Z". charCodeAt (0); var mid = (_a + _z)/2; For (var i = 0; i < str.length; i++) {index = str.charcodeat (i); if (index >= _a && Index <= mid) {temp + = String.fromCharCode (index + 13);} Else if (index <= _z && index > mid) {temp + = String.fromCharCode (index- 13);} else{Temp + = string.fromcharcode (index);}} return temp;}                 
Test rot13("SERR PBQR PNZC")should be decoded to "FREE CODE CAMP" rot13("SERR CVMMN!")should be decoded to "FREE PIZZA!" rot13("SERR YBIR?")should be decoded to "FREE LOVE?" rot13("GUR DHVPX OEBJA QBT WHZCRQ BIRE GUR YNML SBK.")should be decoded to "THE QUICK BROWN DOG JUMPED OVER THE LAZY FOX."

 

Caesars Cipher (algorithm)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.