5 kyu

Simple Encryption #2 - Index-Difference

324 of 880user5036852

Description:

For encrypting strings this region of chars is given (in this order!):

  • all letters (ascending, first all UpperCase, then all LowerCase)
  • all digits (ascending)
  • the following chars: .,:;-?! '()$%&"

These are 77 chars! (This region is zero-based.)

Write two methods:

string Encrypt(string text)
string Decrypt(string encryptedText)
function encrypt(text)
function decrypt(encryptedText)
export function encrypt(text:string):string
export function decrypt(encryptedText:string):string
def encrypt(text)
def decrypt(encrypted_text)
std::string encrypt(std::string text)
std::string decrypt(std::string text)
(encrypt (text))
(decrypt (text))

Prechecks:

  1. If the input-string has chars, that are not in the region, throw an Exception(C#, Python) or Error(JavaScript).
  2. If the input-string is null or empty return exactly this value!

For building the encrypted string:

  1. For every second char do a switch of the case.
  2. For every char take the index from the region. Take the difference from the region-index of the char before (from the input text! Not from the fresh encrypted char before!). (Char2 = Char1-Char2)
    Replace the original char by the char of the difference-value from the region. In this step the first letter of the text is unchanged.
  3. Replace the first char by the mirror in the given region. ('A' -> '"', 'B' -> '&', ...)

Simple example:

  • Input: "Business"
  • Step 1: "BUsInEsS"
  • Step 2: "B61kujla"
    • B -> U
      • B (1) - U (20) = -19
      • -19 + 77 = 58
      • Region[58] = "6"
    • U -> s
      • U (20) - s (44) = -24
      • -24 + 77 = 53
      • Region[53] = "1"
  • Step 3: "&61kujla"

This kata is part of the Simple Encryption Series:
Simple Encryption #1 - Alternating Split
Simple Encryption #2 - Index-Difference
Simple Encryption #3 - Turn The Bits Around
Simple Encryption #4 - Qwerty

Have fun coding it and please don't forget to vote and rank this kata! :-)

Fundamentals
Cryptography
Security
Strings
Arrays
Algorithms

Stats:

CreatedJul 10, 2016
PublishedJul 10, 2016
Warriors Trained3635
Total Skips877
Total Code Submissions9976
Total Times Completed880
C# Completions113
JavaScript Completions200
Python Completions324
C++ Completions202
TypeScript Completions57
Clojure Completions11
Total Stars234
% of votes with a positive feedback rating92% of 254
Total "Very Satisfied" Votes221
Total "Somewhat Satisfied" Votes25
Total "Not Satisfied" Votes8
Total Rank Assessments8
Average Assessed Rank
5 kyu
Highest Assessed Rank
4 kyu
Lowest Assessed Rank
6 kyu
Ad
Contributors
  • user5036852 Avatar
  • zebulan Avatar
  • smile67 Avatar
  • Dentzil Avatar
  • kazk Avatar
  • misha-bohdan Avatar
  • FArekkusu Avatar
  • hobovsky Avatar
  • rge123 Avatar
Ad