***DAILY CHALLENGE***: this one is about encryption & decryption. sounds complicated?!? Not this one ;) | Sololearn: Learn to code for FREE!


***DAILY CHALLENGE***: this one is about encryption & decryption. sounds complicated?!? Not this one ;)

TASK: write a function that get as input an ecnrypted text and a key (both strings) and outputs the decrypted text! input text: "#mpdgpuefzouquyyxzv#|#ypkerrzcovchvgjuwp#" let the program calculate and print the ouput. The '#' and '|' symbolize beginnig and end of string (don't use them in decryption)!!! EXTRA: use these functions to get the int value for a character (and vice versa): s = " abcdefghijklmnopqrstuvwxyz" function get_int(c): i = 0 while i < s.length(): if s[i] == c: return i print("ERROR") return -1 function get_char(i): while i < 0: i += s.length() i %= s.length() return s[i] ############################################ INFO: (you need to use One-Time-Pad & Cesar-Cipher) the key is encrypted the following way: msg = "hi" 1) OTP encryption (create a string size of msg. loop over key and msg and add their int values) random key = "zf" = 26 6, msg = "hi" = 8 9 --> encrypted text is: 26+8 6+9 = 34 15 = "go" ( = 7 15) 2) CESAR encryption (add to each int value (from char) the length of the key) only on key! key = "go" = 7 15 --> encrypted key is: 7+2 5+2 = 9 7 = "ig" now ouput is allways as follows: #key#|#txt# result: -----> "#ig#|#go#" (and 'nobody' knows what that means ^^) YOUR PART IS TO GO THE SAME WAY BACKWARDS MEANING: redo the cesar_cipher(- key.length) then redo the otp encryption by using get_char(key - encrypted) for all chars in key and encrypted Hope explanation is understandable ... if not check out my implementation: https://code.sololearn.com/cgq1RmKzNmlN/#py GOOD LUCK CODERS!!! If this one is too complicated try another one: -- will be uploaded soon --

7/28/2017 4:20:46 PM

Julian Fechner

108 Answers

New Answer


https://code.sololearn.com/cMaS4FBo84lP/#cs That was kinda easy.


@aziz solves this issue with a perfect algorithm https://code.sololearn.com/cSIH9XiW6Hhz/?ref=app


ok imagine the following situation: -my input is: msg = "hello" -now i create a random string with same length: key = "abdfs" -convert each character from msg and key into numbers msg = "hello" = [8,5,12,12,15] key = "abdfh" = [1,2,4,6,8] -now add each number sum = [9,7,16,18,32] -convert the numbers back to characters: encrypted = "igprf" decrypting is the same but now you subtract each number from key - each number from encrypted text (THIS IS YOUR PART)


@Vari it's a cesar encryp, k=2


@@ julian there is a bug in your post...(not in code; in post) u took a=1 up to z=26 so g is 7 of course now in your example it came up 34-15 u changed it 7-15 why???????? SHOULD NOT IT BE 8-15 (34-26)=8 so it wont be" go" it will be "ho"


N.B This works on PC only. https://code.sololearn.com/c1X7r71oO4Ua/?ref=app


Sounds like a good challenge though I think I'll pass. My phone can only do so much 😂




My site is encrypted https://code.sololearn.com/WF13NaXMd1vO/?ref=app


This was written before the challenge so it's mostly FYI: https://code.sololearn.com/W85GKwfubQ6Z/?ref=app The demo analyzes the ciphertext, guesses the key and decrypts the first part of Alice in Wonderland. ...or you can paste this at line 17 (it should look familiar): var sampleText="TQYBO SXQBBUDWU: jxyi edu yi qrekj udshofjyed & tushofjyed. iekdti secfbysqjut?!? Dej jxyi edu ... OEKH FQHJ YI JE WE JXU IQCU MQO RQSAMQHTI CUQDYDW: hute jxu suiqh_syfxuh(- auo.budwjx) jxud hute jxu ejf udshofjyed ro kiydw wuj_sxqh(auo - udshofjut) veh qbb sxqhi yd auo qdt udshofjut Xefu unfbqdqjyed yi kdtuhijqdtqrbu ... yv dej sxusa ekj co ycfbucudjqjyed WEET BKSA SETUHI!!! Yv jxyi edu yi jee secfbysqjut jho qdejxuh edu mybb ru kfbeqtut ieed";


#include <stdio.h> #include <ctype.h> #define MAXSIZE 1024 void encrypt(char*); void decrypt(char*); int menu(); int main(void) { char c, choice[2], s[MAXSIZE]; while(1) { menu(); gets(choice); if((choice[0]=='e')||(choice[0]=='E')) { puts("Input text to encrypt->"); gets(s); encrypt(s); } else if((choice[0]=='d')||(choice[0]=='D')) { puts("Input text to decrypt->"); gets(s); decrypt(s); } else break; } return 0; } void encrypt(char*str) { int n=0; char *p=str, q[MAXSIZE]; while(*p) { if(islower(*p)) { if((*p>='a')&&(*p<'x')) q[n]=toupper(*p + (char)3); else if(*p=='x') q[n]='A'; else if(*p=='y') q[n]='B'; else q[n]='C'; } else { q[n]=*p; } n++; p++; } q[n++]='\0'; puts(q); } void decrypt(char*str) { int n=0; char *p=str, q[MAXSIZE]; while(*p) { if(isupper(*p)) { if((*p>='D')&&(*p<='Z')) q[n]=tolower(*p - (char)3); else if(*p=='A') q[n]='x'; else if(*p=='B') q[n]='y'; else q[n]='z'; } else { q[n]=*p; } n++; p++; } q[n++]='\0'; puts(q); } int menu() { puts("To encrypt, input e or E\n"); puts("To decrypt, input d or D\n"); puts("To exit, input any other letter\n"); puts("Your choice:->\n"); return 0; }


Public static void main (String[]args){ system.out.println("Java has ruined my life"); }






My progress so far https://code.sololearn.com/cbxTOxwBapKa/?ref=app


This is my code ,, I try to make it simple https://code.sololearn.com/c2Ov4DE0jpE0/?ref=app




Rather crude way of doing it https://code.sololearn.com/cbxTOxwBapKa/?ref=app


here's my try in Python https://code.sololearn.com/cVfgGi82JzYC/?ref=app


last week it was around midnight. I decided to write a program that could encrypt any text and I wrote half of it but when I woke up the next morning I didn't understand anything of the code that I wrote the day before that was how I didn't created an encryption program 😅😅😅