[go: up one dir, main page]

0% found this document useful (0 votes)
6 views6 pages

Ap24 CSP Create FRQ 1 Set 1 Sample B Program Code

The document outlines a program that implements a Caesar cipher for encrypting and decrypting messages. It includes functionalities for shifting letters, auto-decrypting messages, and filtering potential decrypted messages based on a word list. The program also features a user interface for inputting messages and displaying results.

Uploaded by

samfrk10
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views6 pages

Ap24 CSP Create FRQ 1 Set 1 Sample B Program Code

The document outlines a program that implements a Caesar cipher for encrypting and decrypting messages. It includes functionalities for shifting letters, auto-decrypting messages, and filtering potential decrypted messages based on a word list. The program also features a user interface for inputting messages and displaying results.

Uploaded by

samfrk10
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

//disgusting program to encrypt and decrypt messages in caesar cipher

var words = getColumn("Words","Word");

var filteredWords = [];

var alphabetList = ["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 encryptNumber = 1;

var foundMessages = [];

var counter = 0;

//hide ui for autodecrypter

hideElement("messageFoundText");

hideElement("changeFoundMessageButton");

//onEvent to encrypt message inputted to the amount set by user

onEvent("encryptButton","click",function(){

setText("messageOutput", crypt(getText("messageInput"), encryptNumber));

});

//onEvent to push alphabet over another letter

onEvent("addButton", "click", function(){

if(encryptNumber == 25){

encryptNumber = 1;

}else{

encryptNumber++;

setText("encryptionNumber", encryptNumber + " letter(s) shifted");


});

//onEvent to push alphabet back a letter

onEvent("minusButton", "click", function(){

if(encryptNumber == 1){

encryptNumber = 25;

}else{

encryptNumber--;

setText("encryptionNumber", encryptNumber + " letter(s) shifted");

});

//onEvent to take encrypted message and output possible decrypted messages

onEvent("autoDecryptButton", "click", function(){

foundMessages = [];

counter = 0;

filteredWords = [];

//if message is 25 characters or less, messages are filtered by 2 letter words and up

if((getText("messageInput")).length > 25){

for(var f = 0; f < words.length; f++){

//messages larger than 25 are filtered by 4 letter words and up

if(words[f].length >= 4){

appendItem(filteredWords, words[f]);

}else{
for(var g = 0; g < words.length; g++){

if(words[g].length >= 2){

appendItem(filteredWords, words[g]);

for(var i = 0; i < 26; i++){

var message = crypt(getText("messageInput"), i);

//if any part of message matches with first 100 words in word list, then message is added
to possible message list

for(var a = 0; a < 100; a++){

if((message.toLowerCase()).includes((filteredWords[a]).toLowerCase())){

appendItem(foundMessages, message);

break;

//checks if found message has more than four back to back consonants and removes
them if they do

// (works better than it sounds)

for(var d = 0; d < foundMessages.length; d++){

var consonants = "bcdfghjklmnpqrstvwxyz";

var consonantCount = 0;

for(var e = 0; e < foundMessages[d].length; e++){

if(consonants.includes(foundMessages[d].substring(e, e + 1)) && consonantCount < 5){

consonantCount++;
}else if(consonantCount < 5){

consonantCount = 0;

}else{

removeItem(foundMessages, d);

break;

//if no message fits with word list, then message is sent back letting user know

if(foundMessages.length == 0){

showElement("messageFoundText");

setText("messageFoundText", "no messages found");

setText("messageOutput", "");

//shows only message found

}else if(foundMessages.length == 1){

showElement("messageFoundText");

setText("messageFoundText", "messages found = 1");

setText("messageOutput", foundMessages[0]);

//shows first message found, then shows option to change to another message

}else{

showElement("messageFoundText");

showElement("changeFoundMessageButton");

setText("messageFoundText", "messages found = " + foundMessages.length);

setText("messageOutput", foundMessages[0]);

});
//onEvent to switch to another message found by autodecrypt

onEvent("changeFoundMessageButton", "click", function(){

if(counter == foundMessages.length-1){

counter = 0;

}else{

counter++;

setText("messageOutput", foundMessages[counter]);

});

//function to encrypt a message in caesar cipher using message and number inputted

function crypt(message, number){

hideElement("messageFoundText");

hideElement("changeFoundMessageButton");

var encryptedList = ["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 encryptedMessage = "";

//creates new alphabet based on number given

for(var i = 0; i < number; i++){

insertItem(encryptedList, 0, encryptedList[25]);

removeItem(encryptedList, 26);

//loop to check for any characters that are not a letter

for(var a = 0; a < message.length; a++){

var miscCheck = 0;
for(var c = 0; c < 26; c++){

if((message.toLowerCase()).substring(a, a+1) == alphabetList[c]){

miscCheck++;

break;

//if character is a letter, takes corresponding letter from encrypted alphabet adds to the
message

if(miscCheck == 1){

for(var b = 0; b < 26; b++){

if((message.toLowerCase()).substring(a, a+1) == alphabetList[b]){

encryptedMessage = encryptedMessage + encryptedList[b];

break;

//just adds it to message if not a letter

}else{

encryptedMessage = encryptedMessage + message.substring(a, a+1);

return encryptedMessage;

You might also like