아두이노 코딩-43: ASCII 표를 이용한 암호연습 코드

in #kr6 years ago (edited)

noname01.png

noname02.png

ASCII 표에 의하면 0∼127까지의 십진수는 특수 문자와 영어의 알파벳에 해당한다.
이 표를 이용하여 암호를 작성해 보자. 간단한 문장이라도 암호화하게 되면 암호화 규칙이나 암호 해독을 위한 Key를 모르면 원래의 정보를 해독할 수 없다.

준비물은 아두이노 우노 보드 하나만 있으면 된다.

아래의 간단한 암호화 코드에서는 시리얼 모니터에서 입력된 영어문장의 알파벳에 대해서 AㅡM 까지는 각각 13을 더해서 N-Z까지로 바꾸어 버린다. 13이라는 Key 는 알파벳이 26자라는데서 반으로 나눈 수이다. 반대로 N-Z까지는 A-M 으로 바꾸어 버린다.

소문자의 경우에도 동일한 알고리듬을 적용하기로 한다.

아래의 간단한 암호화 코드를 사용하여 다음 문장을 암호화하였다가(encryption) 다시 원래대로 해독해 보자(decryption).

I love Steemit!

암호화하면 V ybir Fgrrzvg! 가된다. 쉽게 원래의 문장으로 해독이 가능할까?

noname03.png

//Simple_Encryption_01

int inByte = 0; // serial input and output character

void setup() {
Serial.begin(9600); // initialize the serial port at 9600 baud
while (!Serial) {
; // 시리얼 포트 붙을때까지 대기
}
establishContact(); // 입력 데이타 대기
}

void loop() {

// This is the Rotate 13 encryption algorithm
// If you run the algorithm twice, you get back the original message
// Example: ABC -> NOP -> ABC
if (Serial.available() > 0) { // 입력버퍼에 데이타가 들어 오면
inByte = Serial.read(); // 입력을 읽는다
if (inByte >= 'A' && inByte <= 'M') { // A to M get converted to N to Z
inByte += 13;
} /* if upper case A-M /
else if (inByte >= 'N' && inByte <= 'Z') { // N to Z get converted to A to M
inByte -= 13;
} /
if upper case N-Z /
// Lower case a to m get converted to n to z
else if (inByte >= 'a' && inByte <= 'm') {
inByte += 13;
} /
if lower case a-m /
// Lower case n to z get converted to a to m
else if (inByte >= 'n' && inByte <= 'z') {
inByte -= 13;
} /
if lower case n-z */
Serial.write(inByte); // write the encrypted character back
} // if Serial.available() > 0
}// loop 끝

void establishContact() {
// write 'A' repeatedly until you receive data from the host
while (Serial.available() <= 0) {
Serial.print('A'); // write 'A' to the host
delay(500); // this delay is optional
} // while Serial.available() <= 0
Serial.println();
} // establishContact()

Sort:  

아두이노! 저도 예쩐에 잠깐 깔짝 대다 만 경험이 있어서 반가워 몇자 적습니다 ㅋㅋ

Coin Marketplace

STEEM 0.17
TRX 0.13
JST 0.027
BTC 60497.39
ETH 2637.52
USDT 1.00
SBD 2.56