Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- Here is a tool to add to your codebreaking toolbox
- 1. Type your secret message in the top box.
- 2. Use the cipher disk to define your shift (from 1-26)
- 3. Your encrypted message will appear below.
- */
- angleMode="radians";
- //▲ ▼ ◀ △ ▷ ▽ ◁ ▴ ▾ ▸ ◂
- var COLORS={
- // Brit
- BRIT_BLUE: color(29,86,214,255),
- // Applied Math
- AM0: color(48,167,222,255), AM1: color(99,180,227,255),
- AM2: color(139,194,233,255), AM3: color(41,115,149,255),
- AM4: color(37,104,135,255), AM5: color(29,81,105,252),
- AM0_LT: color(48,167,222,4),
- _WHITE: color(255,255,255,255), WHITE_LT: color(255,255,255,128),
- _BLACK: color(0,0,0,255), BLACK_LT: color(0,0,0,128),
- RED: color(170,29,29,255), RED_LT: color(170,29,29,128),
- BLUE: color(29,86,170,255), BLUE_LT: color(29,86,170,128),
- YELLOW: color(238,214,15,255), YELLOW_LT: color(238,214,15,128),
- ORANGE: color(238,136,15,255), ORANGE_LT: color(238,136,15,128),
- GREEN: color(158,182,58,255), GREEN_LT: color(158,182,58,128),
- GRAY: color(128,128,128,255), GRAY_LT: color(128,128,128,128),
- BROWN: color(155,145,135,255), BROWN_LT: color(155,145,135,128),
- GRAY_16: color(16,16,16,255), GRAY_16LT: color(16,16,16,128),
- GRAY_32: color(32,32,32,255), GRAY_32LT: color(32,32,32,128),
- GRAY_64: color(64,64,64,255), GRAY_64LT: color(64,64,64,128),
- GRAY_96: color(96,96,96,255), GRAY_96LT: color(96,96,96,128),
- GRAY_128: color(128,128,128,255), GRAY_128LT: color(128,128,128,128),
- GRAY_160: color(160,160,160,255), GRAY_160LT: color(160,160,160,128),
- GRAY_192: color(192,192,192,255), GRAY_192LT: color(192,192,192,128),
- GRAY_224: color(224,224,224,255), GRAY_224LT: color(224,224,224,128),
- GRAY_240: color(240,240,240,255), GRAY_240LT: color(240,240,240,128),
- GRAY_255: color(255,255,255,255), GRAY_255LT: color(255,255,255,128)
- };
- var app={ pressed: false,
- legend: false,
- instructions: true,
- animate: true,
- animateRate: 15,
- legendX: -360,
- title: "Title",
- toolTip: "Welcome",
- currentNode: -1,
- nodeSize: 20,
- focus: 1,
- background: COLORS._BLACK,
- rotation: 90,
- index: 0,
- text: [],
- key: [],
- encrypted: [],
- textFrequency: [],
- encryptedFrequency: []
- };
- // Control Types
- var CONTROLS={ TOGGLE: 0,
- INDEX: 1,
- BUTTON: 2,
- LABEL: 3,
- CLEAR: 4,
- DISK: 5
- };
- var tooltips=["Display / Hide Legend (F1)",
- "Click to type message (DEL to backspace)",
- "Click to type shift word",
- "Encrypted message",
- "Clear the message (F5)",
- "Click to lock/unlock",
- "Clear the message"
- ];
- var CAPTIONS={DECRYPT: "Decrypt",
- ENCRYPT: "Encrypt",
- TITLE: "Caesar Cipher",
- DESCRIPTION: "Type in a secret message. Rotate the cipher disk
- and watch as each letter is shifted and the encrypted text appears.",
- SHIFT: "Encrypted message:",
- MESSAGE: "Type in a secret message:"};
- // Generic control properties
- var prop=function(i,x,y,w,h,t,v,f,c){
- this.i=i; // index
- this.x=x; // left
- this.y=y; // top
- this.w=w; // width
- this.h=h; // height
- this.t=t; // control type
- this.f=f; // execute function
- this.v=v; // value
- this.c=c; // caption
- };
- var
- englishFrequency=[8.167,1.492,2.782,4.253,12.702,2.228,2.015,6.094,6.966,0.153,0
- .772,4.025,2.406,6.749,7.507,1.929,0.095,5.987,6.327,9.056,2.758,0.978,2.360,0.
- 150,1.974,0.074];
- var
- alphabet=["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"];
- // Converts text from integer to alpha
- var convertText=function(param){
- var n=0;
- var _s="";
- for(n=0; n<param.length; n++){
- if(param[n]===-2){ _s+=" ";}
- else { _s+=alphabet[param[n]]; }
- }
- return _s;
- };
- var calculateRotation=function(){
- var _x=(mouseX-200);
- var _y=-1*(mouseY-200);
- var theta=degrees(atan2(_y,_x));
- //println(theta);
- var offset=360/52;
- if(theta>=0){ theta=theta-theta%(offset*2)+offset; }
- else { theta=360+theta-theta%(offset*2)-offset; }
- var i=round((theta-90)/360*26);
- if(theta<90){ i+=26; }
- //println(i);
- app.rotation=theta;
- app.key=26-i;
- };
- var index=function(prop){
- var _pressed=false;
- var _locked=false;
- var _hit=function(x,y){
- if(x>=prop.x && x<=prop.x+prop.w &&
- y>=prop.y && y<=prop.y+prop.h){ return true; }
- };
- this.pressed=function(x,y) { _pressed=true; };
- this.released=function(x,y) { _pressed=false; };
- var _draw=function(x,y){
- var disp=0;
- if(_hit(x,y)){ stroke(COLORS.GRAY_160);
- if(app.pressed){ disp=1; }
- else { disp=0; }
- }
- else{ stroke(COLORS.GRAY_128); disp=0; }
- strokeWeight(3);
- line(prop.x+disp, prop.y+disp, prop.x+prop.w, prop.y+disp);
- line(prop.x+disp, prop.y+5+disp, prop.x+prop.w, prop.y+5+disp);
- line(prop.x+disp, prop.y+10+disp, prop.x+prop.w, prop.y+10+disp);
- };
- this.clicked=function(x,y) { if(_hit(x,y)){ app.legend=!app.legend; }
- };
- this.refresh=function(x,y) { _draw(x,y);
- };
- this.mouseMoved=function(x,y){ if(_hit(x,y)){ app.tooltip=tooltips[prop.i]; }
- };
- };
- var label=function(prop){ // label definition
- var _hit=function(x,y){
- if(x>=prop.x && x<=prop.x+prop.w &&
- y>=prop.y && y<=prop.y+prop.h){ return true; }
- };
- var _focus=function(){ return app.focus===prop.i; };
- var _draw=function(x,y){
- strokeWeight(1);
- stroke(COLORS.GRAY);
- textAlign(RIGHT,CENTER);
- fill(COLORS.GRAY_224);
- if(_focus()) {
- if(prop.i!==3){ fill(COLORS._WHITE); stroke(COLORS.BRIT_BLUE);
- strokeWeight(1.5); }
- }
- if(_hit(x,y)) { if(prop.i!==3){ fill(COLORS._WHITE); } }
- rect(prop.x, prop.y, prop.w, prop.h, 2);
- fill(COLORS._BLACK);
- textSize(12);
- textAlign(RIGHT,CENTER);
- var s="";
- if (prop.i===1){ s=convertText(app.text); }
- else if(prop.i===2){ s=convertText(app.key); }
- else if(prop.i===3){ s=convertText(app.encrypted); }
- text(s, prop.x-3 + prop.w, prop.y+prop.h/2+1);
- };
- this.clicked=function(x,y) { if(_hit(x,y)){ app.focus=1; } };
- this.refresh=function(x,y) { _draw(x,y); };
- this.mouseMoved=function(x,y) { if(_hit(x,y)){ app.tooltip=tooltips[prop.i]; }
- };
- };
- var button=function(prop){ // button definition
- // private methods --------------------------------------------------
- var _hit=function(x,y){
- if(x>=prop.x && x<=prop.x+prop.w &&
- y>=prop.y && y<=prop.y+prop.h){ return true; }
- };
- var _draw=function(x,y){
- strokeWeight(1);
- var disp=0;
- var clr=COLORS.RED;
- var hit=_hit(x,y);
- if(hit){ fill(COLORS._WHITE);
- if(app.pressed){ disp=1; }
- else { disp=0; } }
- else { fill(COLORS.GRAY_192);
- disp=0; }
- // Shadow
- noStroke();
- fill(COLORS.GRAY_32);
- rect(prop.x+2, prop.y+2, prop.w, prop.h, 2);
- // Gradient
- var incr=1;
- for(var n=0; n<prop.h; n+=incr){
- if(hit) { stroke(lerpColor(color(192,192,192), color(224,224,224),
- n/prop.h)); }
- else { stroke(lerpColor(color(224,224,224), color(192,192,192),
- n/prop.h)); }
- rect(prop.x+disp+1, prop.y+n+disp+1, prop.w-2, incr-2,1);
- }
- // Button
- noFill();
- rect(prop.x+disp, prop.y+disp, prop.w, prop.h, 2);
- // Caption
- fill(COLORS._BLACK);
- textAlign(CENTER,CENTER);
- textSize(10);
- text(prop.c, prop.x + prop.w/2+disp, prop.y+prop.h/2+disp);
- };
- // public methods --------------------------------------------------
- this.pressed=function(x,y){
- if(_hit(x,y)){ }
- };
- this.released=function(x,y){
- if(_hit(x,y)){}
- };
- this.clicked=function(x,y){
- if(_hit(x,y)){ prop.f(prop.c); }
- };
- this.refresh=function(x,y){ _draw(x,y); };
- this.mouseMoved=function(x,y){ if(_hit(x,y)){ app.tooltip=tooltips[prop.i]; }
- };
- };
- var clear=function(prop){ // play/pause button definition
- // private methods --------------------------------------------------
- var _hit=function(x,y){
- if(x>=prop.x-10 && x<=prop.x+10 &&
- y>=prop.y-10 && y<=prop.y+10){ return true; }
- };
- var _draw=function(x,y){
- strokeWeight(1);
- var disp=21;
- var clr=COLORS.RED;
- var hit=_hit(x,y);
- pushMatrix();
- translate(prop.x,prop.y);
- if(hit){ fill(COLORS.GRAY_160);
- stroke(COLORS.GRAY_160);
- if(app.pressed){ disp=0;
- rotate(45); }
- else { disp=0; } }
- else { fill(COLORS.GRAY_128);
- stroke(COLORS.GRAY_128);
- disp=0; rotate(radians(0)); }
- //rect(prop.x+disp-prop.w/2, prop.y+disp-prop.h/2, prop.w, prop.h, 2);
- //fill(COLORS.GRAY_160);
- textAlign(CENTER,CENTER);
- textSize(24);
- text("↻", 0+disp,0+disp);
- popMatrix();
- };
- // public methods --------------------------------------------------
- this.pressed=function(x,y){
- if(_hit(x,y)){ }
- };
- this.released=function(x,y){
- if(_hit(x,y)){}
- };
- this.clicked=function(x,y){
- if(_hit(x,y)){
- //app.focus=prop.i;
- prop.f(prop.c);
- }
- };
- this.refresh=function(x,y){ _draw(x,y); };
- this.mouseMoved=function(x,y){ if(_hit(x,y)){ app.tooltip=tooltips[prop.i]; }
- };
- };
- var disk=function(prop){ // disk definition
- var _cipherDisk=function(){
- fill(COLORS.BRIT_BLUE);
- //text("Shift: " + app.key, 380,385);
- textAlign(CENTER,CENTER);
- textSize(14);
- pushMatrix();
- translate(200,200);
- scale(1,-1);
- strokeWeight(2);
- stroke(COLORS.BLUE);
- fill(COLORS._WHITE);
- ellipse(0,0,230,230);
- stroke(COLORS.GRAY);
- strokeWeight(1);
- fill(COLORS._WHITE);
- ellipse(0,0,100,100);
- rotate(radians(app.rotation+360/52));
- // Interior disk
- for(var n=0; n<26; n++){
- strokeWeight(0.5);
- line(50,0,113,0);
- rotate(-radians(360/52));
- if(n===0){ fill(COLORS.BLUE);
- ellipse(101,0,20,20); }
- pushMatrix();
- scale(1,-1);
- translate(102,0);
- rotate(radians(90));
- textSize(14);
- if(app.index===n){ fill(COLORS.YELLOW); }
- else { fill(COLORS.BRIT_BLUE); }
- text(alphabet[n],0,0);
- popMatrix();
- rotate(-radians(360/52));
- }
- popMatrix();
- };
- var _textDisk=function(){
- var _w = 180;
- textAlign(CENTER,CENTER);
- textSize(14);
- pushMatrix();
- translate(200,200);
- scale(1,-1);
- stroke(COLORS.RED);
- strokeWeight(2);
- fill(COLORS.GRAY_16);
- ellipse(0,0,_w,_w);
- stroke(COLORS.GRAY);
- strokeWeight(1);
- fill(COLORS.GRAY_224);
- ellipse(0,0,_w-40,_w-40);
- fill(COLORS._WHITE);
- ellipse(0,0,_w-80,_w-80);
- rotate(radians(90+360/52));
- // Interior disk
- for(var n=0; n<26; n++){
- strokeWeight(0.5);
- stroke(COLORS.RED_LT);
- line(50,0,90,0);
- rotate(-radians(360/52));
- if(n===0){
- fill(COLORS.BLUE_LT);
- pushMatrix();
- rotate(radians(app.rotation-90));
- if(prop.v){ stroke(COLORS.BRIT_BLUE); }
- else { stroke(COLORS.RED); }
- strokeWeight(5);
- line(47,0,0,0);
- noStroke();
- fill(COLORS.RED);
- ellipse(80,0,20,20);
- popMatrix();
- }
- pushMatrix();
- scale(1,-1);
- translate(102,0);
- rotate(radians(90));
- textSize(12);
- fill(COLORS._WHITE);
- text(alphabet[n],0,22);
- textSize(8);
- if(app.index===n){ fill(COLORS.RED); }
- else { fill(COLORS.GRAY_64); }
- text(n+1,0,42);
- popMatrix();
- rotate(-radians(360/52));
- }
- popMatrix();
- textSize(10);
- textAlign(CENTER,CENTER);
- if(!prop.v){ fill(COLORS.RED);
- text("Click to unlock",200,210); }
- else{ fill(COLORS.BRIT_BLUE);
- text("Click to lock",200,210); }
- };
- // private methods --------------------------------------------------
- var _hit=function(x,y){
- if(dist(x,y,200,200)<prop.w/2){ return true; }
- };
- var _draw=function(x,y){
- strokeWeight(1);
- var disp=0;
- var clr=COLORS.RED;
- var hit=_hit(x,y);
- if(hit){ fill(COLORS._WHITE);
- if(app.pressed){ disp=1; }
- else { disp=0; } }
- else { fill(COLORS.GRAY_192);
- disp=0; }
- if(prop.v){ calculateRotation(); }
- else {
- fill(COLORS.RED);
- text("Click to unlock",200,200); }
- _cipherDisk();
- _textDisk();
- };
- // public methods --------------------------------------------------
- this.pressed=function(x,y){
- if(_hit(x,y)){ }
- };
- this.released=function(x,y){
- if(_hit(x,y)){}
- };
- this.clicked=function(x,y){
- if(_hit(x,y)){ prop.f(prop.c); prop.v=!prop.v; }
- };
- this.refresh=function(x,y){ _draw(x,y); };
- this.mouseMoved=function(x,y){ if(_hit(x,y)){ app.tooltip=tooltips[prop.i]; }
- };
- };
- var encrypt=function(){
- var encrypt=[];
- var k=0;
- for(var j=0; j<app.text.length; j++){
- if(app.text[j]===-2){
- app.encrypted[j]=-2;
- }
- else{
- app.encrypted[j] = (app.text[j]+app.key)%alphabet.length;
- k++;
- }
- }
- };
- var processE=function(){
- var n=0;
- for(n=0; n<alphabet.length; n++){
- app.encryptedFrequency[n]=0;
- }
- encrypt();
- if(app.encrypted.length===0) { return; }
- for(n=0; n<app.encrypted.length; n++){
- app.encryptedFrequency[app.encrypted[n]]++;
- }
- };
- var reset=function(c){
- app.text=[];
- // app.key=0;
- app.encrypted=[];
- for(var n=0; n<alphabet.length;n++){
- app.textFrequency[n]=0;
- app.encryptedFrequency[n]=0;
- }
- };
- var controls=function(){ // controls collection definition
- var _controls=[];
- this.add=function(prop){ // ADD
- switch (prop.t){
- case CONTROLS.BUTTON: _controls.push(new button(prop)); break;
- case CONTROLS.LABEL: _controls.push(new label(prop)); break;
- case CONTROLS.INDEX: _controls.push(new index(prop)); break;
- case CONTROLS.CLEAR: _controls.push(new clear(prop)); break;
- case CONTROLS.DISK: _controls.push(new disk(prop)); break;
- }
- };
- this.refresh=function(x,y){ // REFRESH
- for(var ctl in _controls){ _controls[ctl].refresh(x,y); }
- };
- this.clicked=function(x,y){ // CLICKED
- for(var ctl in _controls){_controls[ctl].clicked(x,y);}
- };
- this.released=function() { // RELEASED
- for(var ctl in _controls){_controls[ctl].released();}
- };
- this.mouseMoved=function(x,y) { // MOVED
- for(var ctl in _controls){ _controls[ctl].mouseMoved(x,y);}
- };
- this.item=function(n) { return _controls[n]; };
- this.length=function(){ return _controls.length; };
- };
- // controls collection object
- var controls=new controls();
- var legend=function(){
- if(app.legend){
- if(app.legendX<40){ app.legendX+=20; }
- }
- else{
- if(app.legendX>-320){ app.legendX-=20; }
- }
- var _x=app.legendX;
- stroke(COLORS.AM5);
- fill(COLORS._BLACK);
- rect(_x,20,320,350);
- for(var n=20; n<370; n++){
- stroke(29,81,105,275-n/2);
- line(_x,n,_x+320,n);
- }
- noFill();
- stroke(COLORS.AM0);
- strokeWeight(2);
- fill(COLORS.AM5);
- //rect(50,20,300,360);
- var title=CAPTIONS.TITLE;
- var sub1=CAPTIONS.DESCRIPTION;
- fill(COLORS._WHITE);
- textAlign(CENTER,TOP);
- textSize(20);
- text(title,_x+150,30);
- textAlign(LEFT,TOP);
- textSize(12);
- text(sub1,_x+10,60,300,400);
- };
- var blank=function(){
- };
- // Add controls to the controls collection
- controls.add(new prop(0,5,5,15,15,CONTROLS.INDEX,false,legend,"index"));
- controls.add(new prop(1,25,30,345,20,CONTROLS.LABEL,false,"","Message"));
- controls.add(new prop(3,25,350,345,20,CONTROLS.LABEL,false,"","Encrypted
- Text"));
- controls.add(new prop(4,385,15,20,20,CONTROLS.CLEAR,false,reset, "clear"));
- controls.add(new prop(5,100,150,230,230,CONTROLS.DISK,false,blank, "disk"));
- var header=function(){
- fill(COLORS.GRAY_232);
- // Message
- textSize(16);
- textAlign(LEFT,BOTTOM);
- fill(COLORS.BRIT_BLUE);
- text(CAPTIONS.MESSAGE, 26,25);
- fill(COLORS.RED);
- text(CAPTIONS.SHIFT, 26,345);
- };
- var telemetry=function(){
- textSize(10);
- fill(COLORS.GRAY_232);
- text(app.tooltip,10,395);
- };
- var grid=function(){
- var n=0;
- strokeWeight(1);
- stroke(COLORS.GRAY_16);
- for(n=0; n<=height-40; n+=20){
- line(0,n,width,n);
- }
- for(n=0; n<=width; n+=20){
- line(n,0,n,height-40);
- }
- };
- reset();
- var draw=function(){
- // Order is important
- background(app.background);
- grid();
- //frequency();
- controls.refresh(mouseX,mouseY);
- header();
- telemetry();
- //cipherDisk();
- //textDisk();
- encrypt();
- // Always last!
- legend();
- };
- var mousePressed=function() { app.pressed=true; };
- var mouseReleased=function(){ app.pressed=false; };
- var mouseClicked=function(){ controls.clicked(mouseX,mouseY); };
- var mouseMoved=function() { controls.mouseMoved(mouseX,mouseY); };
- var keyPressed=function(){
- // println(keyCode);
- switch(keyCode) {
- case F1: app.legend=!app.legend; break;
- case F5: reset(); break;
- }
- if(keyCode===37 || keyCode===127 || keyCode===110){
- if(app.focus===1){ app.text.pop(app.text.length);
- app.encrypted.pop(app.encrypted.length);
- return; }
- else if(app.focus===2){ app.key.pop(app.key.length);
- app.encrypted.pop(app.encrypted.length);
- return; }
- }
- var n=keyCode-65;
- var _s=-3;
- if (n>-1 && n<alphabet.length){
- _s=n;
- if (app.focus===1){ app.textFrequency[n]++; }
- }
- else if(keyCode===32){ _s=-2; }
- if(_s!==-3){
- switch(app.focus){
- case 1: app.text.push(_s); break;
- case 2: if(_s!==-2){ app.key.push(_s); } break;
- default: break;
- }
- }
- encrypt();
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement