
1) Node.JS를 이해하고 Local환경에 설치한다.
2) Node Library 세팅하여 web service 구현한다.
3) Redis DB를 Local환경에 설치한다.
4) View 화면 및 Server+DB 암호화 연동 부분을 실습한다.


const redis = require('redis');
const crypto = require('crypto');
const express = require('express');
const bodyParser = require('body-parser');
//32바이트 길이의 secret 키
const key = crypto.randomBytes(32);
//초기화 벡터 생성 (16 바이트)
const iv = crypto.randomBytes(16);
const app = express();
const client = redis.createClient;
//JSON 파싱을 위한 미들웨어 설정
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
//데이터를 암호화하는 함수
function encrypt(text){
const cipher = crypto.createCipheriv('aes-256-cbc', key, iv);
let encrypted = cipher.update(text, 'utf8', 'hex');
encrypted += cipher.final('hex');
return {iv: iv.toString('hex'), encryptedData: encrypted};
}
//데이터를 복호화하는 함수
function decrypt(text){
const decipher = crypto.createDecipheriv('aes-256-cbc',key,iv);
let decrypted = decipher.update(text, 'hex', 'utf8');
decrypted += decipher.final('utf8');
return decrypted;
}
app.get('/', function(req, res){
res.send("Hello world!");
})
//서버 시작
const PORT = 3333;
app.listen(PORT, ()=>{
console.log(`서버 실행 포트: ${PORT}`);
});

//로컬 Redis 서버에 연결
const client = redis.createClient(6379, 'localhost');
client.connect();
// 비동기로 Redis에 데이터 저장
async function saveToRedis(key, data){
try{
await client.set(key, JSON.stringify(data));
return true;
} catch(err){
console.error('Redis 저장 중 에러 발생: ', err);
return false;
}
}
//암호화된 데이터를 Redis에 저장하는 API 엔드포인트
app.post('/insert', async (req, res)=>{
const {data} = req.body;
const encryptedData = encrypt(data);
//암호화된 데이터를 Redis에 저장
const result = await saveToRedis('data', encryptedData);
if(result){
res.status(200).send(" data : "+JSON.stringify(encryptedData)+" <br/> 저장 완료 !! ");
}else{
res.status(500).send('Insert error!');
}
});
//node 종료시 호출
process.on('SIGINT', ()=>{
client.quit();
console.log('서버 종료 및 Redis 클라이언트 종료');
process.exit();
})
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>NoSQL 데이터 암호화</title>
</head>
<body>
<h1>데이터 암호화 TEST</h1>
<form id="dataForm">
<input type="text" id="dataInput" placeholder="데이터 입력">
<button type="submit">저장</button>
</form>
<div id="result"></div>
<script>
const form = document.getElementById('dataForm')
const input = document.getElementById('dataInput');
const resultDiv = document.getElementById('result');
form.addEventListener('submit', async(event)=>{
event.preventDefault();
const data = input.value;
//데이터를 서버에 전송
const response = await fetch('/insert', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({data})
});
if(response.ok){
const result = await response.text();
resultDiv.textContent = result;
}else{
resultDiv.textContent = 'error!!';
}
});
</script>
</body>
</html>
welcome화면으로 index.html을 지정하기 위해 아래 구문 추가.
// index.html을 제공하기 위한 미들웨어 설정
app.use(express.static(__dirname)); //현재 디렉토리 지정
node서버 재시작 후 localhost:3333 재접속시 view화면 정상접속 확인.
임의의 데이터 입력후 저장, reponse data 확인.
const redis = require('redis');
const crypto = require('crypto');
const express = require('express');
const bodyParser = require('body-parser');
const key = crypto.randomBytes(32); //32바이트 길이의 secret 키
const iv = crypto.randomBytes(16); //초기화 벡터 생성 (16 바이트)
const app = express();
//로컬 Redis 서버에 연결
const client = redis.createClient(6379, 'localhost');
client.connect();
//JSON 파싱을 위한 미들웨어 설정
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
// index.html을 제공하기 위한 미들웨어 설정
app.use(express.static(__dirname)); //현재 디렉토리 지정
//데이터를 암호화하는 함수
function encrypt(text){
const cipher = crypto.createCipheriv('aes-256-cbc', key, iv);
let encrypted = cipher.update(text, 'utf8', 'hex');
encrypted += cipher.final('hex');
return {iv: iv.toString('hex'), encryptedData: encrypted};
}
//데이터를 복호화하는 함수
function decrypt(text){
const decipher = crypto.createDecipheriv('aes-256-cbc',key,iv);
let decrypted = decipher.update(text, 'hex', 'utf8');
decrypted += decipher.final('utf8');
return decrypted;
}
// app.get('/', function(req, res){
// res.send("Hello world!");
// })
//서버 시작
const PORT = 3333;
app.listen(PORT, ()=>{
console.log(`서버가 포트 ${PORT}에서 실행중입니다.`);
});
// 비동기로 Redis에 데이터 저장
async function saveToRedis(key, data){
try{
await client.set(key, JSON.stringify(data));
return true;
} catch(err){
console.error('Redis 저장 중 에러 발생: ', err);
return false;
}
}
//암호화된 데이터를 Redis에 저장하는 API 엔드포인트
app.post('/insert', async (req, res)=>{
const {data} = req.body;
const encryptedData = encrypt(data);
//암호화된 데이터를 Redis에 저장
const result = await saveToRedis('Data', encryptedData);
if(result){
res.status(200).send(" data : "+JSON.stringify(encryptedData)+" <br/> 저장 완료 !! ");
}else{
res.status(500).send('Insert error!');
}
});
//node 종료시 호출
process.on('SIGINT', ()=>{
client.quit();
console.log('서버 종료 및 Redis 클라이언트 종료');
process.exit();
})
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>NoSQL 데이터 암호화</title>
</head>
<body>
<h1>데이터 암호화 TEST</h1>
<form id="dataForm">
<input type="text" id="dataInput" placeholder="데이터 입력">
<button type="submit">저장</button>
</form>
<div id="result"></div>
<script>
const form = document.getElementById('dataForm')
const input = document.getElementById('dataInput');
const resultDiv = document.getElementById('result');
form.addEventListener('submit', async(event)=>{
event.preventDefault();
const data = input.value;
//데이터를 서버에 전송
const response = await fetch('/insert', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({data})
});
if(response.ok){
const result = await response.text();
resultDiv.textContent = result;
}else{
resultDiv.textContent = 'error!!';
}
});
</script>
</body>
</html>
app.get('/', function(req, res){
res.send("Hello world!");
})
을 주석처리해야함
Redis와 node.js 서버 모두 켜놓고 테스트 해야함
Redis DB에서 확인하는 테스트에서 redis-cli.exe –h localhost –p 6379 만 입력하면 에러가 나는 경우가 있음
- 해결법이 여러가지인데 아래의 3번으로 해결!
Redis 설치 확인:
Redis가 제대로 설치되어 있는지 확인. Redis를 설치하지 않았다면, 공식 웹사이트에서 설치 파일을 다운로드하고 설치.
환경 변수 설정:
Redis 설치 경로를 환경 변수에 추가. 예를 들어, redis-cli.exe가 C:\Redis 폴더에 설치되어 있다면, 해당 경로를 시스템 환경 변수 PATH에 추가해야 함.
경로를 포함하여 명령 실행:
명령어를 실행할 때 전체 경로를 포함하여 실행.
예를 들어:
C:\Redis\redis-cli.exe -h localhost -p 6379
현재 디렉토리에서 실행:
명령어를 실행하는 디렉토리가 redis-cli.exe 파일이 있는 디렉토리인지 확인. 그렇지 않다면 해당 디렉토리로 이동한 후 다시 시도.