Enigma but an unbreakable one
Same arduino but a different machine.
In this series of showing the power of a turing machine this time i created a cipher machine.
Turing machine is like a transformer. It can transform into any other machine. This time it transformed itself into a cipher machine.
I have used leds to represent the 26 english alphabets.
Here is the video showing the alphabets.
and here is the demonstration of how the machine works.
Message Encrypter
void setup() {
//buttons
pinMode(2, INPUT);
pinMode(3, INPUT);
//RGB led
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
//normal leds
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
pinMode(A0, OUTPUT);
pinMode(A1, OUTPUT);
pinMode(A2, OUTPUT);
Serial.begin(9600);
}
int count = -1;
int leds[] = {8, 9, 10, 11, 12, 13, A0, A1, A2};
void turnOnRGBRed(){
digitalWrite(5, HIGH);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
}
void turnOnRGBGreen(){
digitalWrite(5, LOW);
digitalWrite(6, HIGH);
digitalWrite(7, LOW);
}
void turnOnRGBBlue(){
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, HIGH);
}
void turnOffLeds(){
for(int i=0; i<9; i++){
digitalWrite(leds[i], LOW);
}
//turn off rgb led
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
}
void turnOnLed(int n){
for(int i=0; i<9; i++){
if((i) == n){
digitalWrite(leds[i], HIGH);
}
else{
digitalWrite(leds[i], LOW);
}
}
}
void convertNumberToLEDs(int n){
turnOnLed( n % 9 );
switch(n/9){
case 0:
turnOnRGBRed();
break;
case 1:
turnOnRGBGreen();
break;
case 2:
turnOnRGBBlue();
break;
}
}
//mode = 0 Input the key, mode = 1 Input the text, mode = 2 shows the cipher
int mode = 0;
int keys[100];
int keyindex = 0;
int plain[100];
int plainindex = 0;
int cipher[100];
int cipherindex = 0;
int prevkey = 0;
void calculateCipher(){
for(int i=0; i<keyindex; i++){
cipher[i] = (keys[i] + plain[i]) % 26;
}
}
void showCipher(){
delay(1000);
for(int i=0; i<keyindex; i++){
convertNumberToLEDs(cipher[i]);
Serial.println(cipher[i]);
delay(1000);
}
}
void loop() {
// put your main code here, to run repeatedly:
//cycles the alphabet
if(digitalRead(2) == HIGH){
count += 1;
if(count > 25){
count = -1;
}
prevkey = 2;
delay(300);
}
if(digitalRead(3) == HIGH){
if(count >= 0 ){
//in mode 0 stores the key
if(mode == 0){
keys[keyindex] = count;
keyindex += 1;
}//in mode 1 stores the plain text
else if(mode == 1){
plain[plainindex] = count;
plainindex += 1;
}
count = -1;
}
if(prevkey == 3 && count == -1){
mode += 1;
//in mode 2 shows the cipher
if(mode >= 2){
calculateCipher();
showCipher();
}
}
prevkey = 3;
delay(300);
}
if(count >= 0 ){
convertNumberToLEDs(count);
}
else{
turnOffLeds();
}
}
Message Decrypter
void setup() {
// put your setup code here, to run once:
pinMode(2, INPUT);
pinMode(3, INPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
pinMode(A0, OUTPUT);
pinMode(A1, OUTPUT);
pinMode(A2, OUTPUT);
Serial.begin(9600);
}
int count = -1;
int leds[] = {8, 9, 10, 11, 12, 13, A0, A1, A2};
void turnOnRGBRed(){
digitalWrite(5, HIGH);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
}
void turnOnRGBGreen(){
digitalWrite(5, LOW);
digitalWrite(6, HIGH);
digitalWrite(7, LOW);
}
void turnOnRGBBlue(){
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, HIGH);
}
void turnOffLeds(){
for(int i=0; i<9; i++){
digitalWrite(leds[i], LOW);
}
//turn off rgb led
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
}
void turnOnLed(int n){
for(int i=0; i<9; i++){
if((i) == n){
digitalWrite(leds[i], HIGH);
}
else{
digitalWrite(leds[i], LOW);
}
}
}
void convertNumberToLEDs(int n){
turnOnLed( n % 9 );
switch(n/9){
case 0:
turnOnRGBRed();
break;
case 1:
turnOnRGBGreen();
break;
case 2:
turnOnRGBBlue();
break;
}
}
int mode = 0;
int keys[100];
int keyindex = 0;
int plain[100];
int plainindex = 0;
int cipher[100];
int cipherindex = 0;
int prevkey = 0;
void calculatePlain(){
for(int i=0; i<keyindex; i++){
plain[i] = (cipher[i] - keys[i]) % 26;
if(plain[i] < 0){
plain[i] = 26 + plain[i];
}
}
}
void showPlain(){
delay(1000);
for(int i=0; i<keyindex; i++){
convertNumberToLEDs(plain[i]);
Serial.println(plain[i]);
delay(1000);
}
}
void loop() {
// put your main code here, to run repeatedly:
if(digitalRead(2) == HIGH){
count += 1;
if(count > 25){
count = -1;
}
prevkey = 2;
delay(300);
}
if(digitalRead(3) == HIGH){
if(count >= 0 ){
if(mode == 0){
keys[keyindex] = count;
keyindex += 1;
}
else if(mode == 1){
cipher[cipherindex] = count;
cipherindex += 1;
}
count = -1;
}
if(prevkey == 3 && count == -1){
mode += 1;
if(mode >= 2){
calculatePlain();
showPlain();
}
}
prevkey = 3;
delay(300);
}
if(count >= 0 ){
convertNumberToLEDs(count);
}
else{
turnOffLeds();
}
}
No comments:
Post a Comment