|
[유용한 String 관련 java code] String 암호화(Encrypt), 복호화(Decrypt) 기능 java code
특정 string을 암호화 하고
암호화된 string을 다시 복호화 하는 기능
Java code와
C++ code 2개 모듈을 작성하였습니다.
실제 사용하는 코드입니다.
C++로 먼저 개발을 하였고 이를 다시 java로 변경하였습니다.
<%
WCEncrypt enc = new WCEncrypt();
int nKey = 100;
String sSrc = "abcdef";
String sEnc = enc.Encrypt(sSrc.getBytes(),nKey);
String sDec = enc.Decrypt(sEnc.getBytes(),nKey);
%>
<%="sSrc="+sSrc%><br>
<%="sEnc="+sEnc%><br>
<%="sDec="+sDec%><br>
sSrc=abcdef
sEnc=618D7F84B7E4
sDec=abcdef
아래의 code에서 WCPage는 www.webdevlib.net에서 source code를 다운로드 받아 사용이 가능합니다.
만약 WCPage를 사용하지 않고 그냥 사용하고 싶다면 WCPage가 들어간 부분을 모두 제거하면 됩니다.
package wdl;
import java.util.*;
import java.sql.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.text.SimpleDateFormat;
public class WCEncrypt
{
public int m_nC1Key = 74102;
public int m_nC2Key = 12337;
public int m_nC3Key = 100;
public WCPage m_oPage = null;
public void setKey1(int nKey)
{
m_nC1Key = nKey;
}
public void setKey2(int nKey)
{
m_nC2Key = nKey;
}
public WCEncrypt(WCPage oPage)
{
m_oPage = oPage;
}
public WCEncrypt()
{
}
public WCEncrypt(int nKey1,int nKey2)
{
m_nC1Key = nKey1;
m_nC2Key = nKey2;
}
public WCEncrypt(WCPage oPage,int nKey1,int nKey2)
{
m_oPage = oPage;
m_nC1Key = nKey1;
m_nC2Key = nKey2;
}
public void setKey(int nKey1,int nKey2,int nKey3)
{
m_nC1Key = nKey1;
m_nC2Key = nKey2;
m_nC3Key = nKey3;
}
public void setPage(WCPage oPage)
{
m_oPage = oPage;
}
public byte HexaByte(int nVal)
{
byte [] szHexaByte = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
if (nVal > 15)
{
nVal = 0;
}
return szHexaByte[nVal];
}
public String ValueToHex(int szSrc[])
{
if (szSrc == null)
return null;
int nSrcLen = szSrc.length;
byte szBuf[] = new byte[nSrcLen*2];
for (int i=0;i<nSrcLen*2;i++)
{
szBuf[i] = 0;
}
for(int i=0;i<nSrcLen;i++)
{
szBuf[(i*2)+0] = HexaByte(((int)(szSrc[i]))/16);
szBuf[(i*2)+1] = HexaByte(((int)(szSrc[i]))%16);
}
String sRet = new String(szBuf);
return sRet;
}
public byte[] HexToValue(byte[] szSrc)
{
int nLen = szSrc.length;
byte[] szDest = new byte[nLen/2];
char szChar[] = new char[2];
for(int I = 0 ; I < nLen/2; I++)
{
szChar[0] = (char)szSrc[I*2];
szChar[1] = (char)szSrc[I*2+1];
byte btDest = (byte)HexToDecimal(szChar);
int nDest = btDest < 0 ? ( Byte.MAX_VALUE + 1 ) * 2 + btDest : btDest;
szDest[I] = (byte)nDest;
}
String sRet = new String(szDest);
return szDest;
}
public String Encrypt(byte btSrc[], int Key)
{
int nSrcLen = btSrc.length;
long nKey2 = Key;
int FirstResult[] = new int[nSrcLen];
for (int i=0;i<nSrcLen;i++)
{
FirstResult[i] = 0;
}
int nLen = btSrc.length;
for(int i = 0 ; i < nLen ; i++)
{
byte btByte = (byte)btSrc[i];
int cSrc = btByte < 0 ? ( Byte.MAX_VALUE + 1 ) * 2 + btByte : btByte;
long nXor = ((long)nKey2) / ((long)256);
byte btTmp = (byte)(cSrc^nXor);
FirstResult[i] = btTmp < 0 ? ( Byte.MAX_VALUE + 1 ) * 2 + btTmp : btTmp;
byte cFirstResult = (byte)FirstResult[i];
int nFirstResult = cFirstResult < 0 ? ( Byte.MAX_VALUE + 1 ) * 2 + cFirstResult : cFirstResult;
long nFirstResultKey = (long)(nFirstResult + nKey2);
nKey2 = (nFirstResultKey) * m_nC1Key + m_nC2Key;
}
String sRet = "";
sRet = ValueToHex(FirstResult);
return sRet;
}
public int HexToDecimal(char[] szSrc)
{
int nRet = 0;
int nLen = szSrc.length;
for (int i=0;i<nLen;i++)
{
byte cChar = (byte)szSrc[i];
nRet = nRet * 16;
nRet += HexToDecimal(cChar);
}
return nRet;
}
public int HexToDecimal(byte cChar)
{
if (cChar == 'A' || cChar == 'a')
return 10;
if (cChar == 'B' || cChar == 'b')
return 11;
if (cChar == 'C' || cChar == 'c')
return 12;
if (cChar == 'D' || cChar == 'd')
return 13;
if (cChar == 'E' || cChar == 'e')
return 14;
if (cChar == 'F' || cChar == 'f')
return 15;
return (cChar-48);
}
public String Decrypt(byte szSrc[],int Key)
{
if (szSrc == null)
return null;
int nSrcLen = szSrc.length;
byte FirstResult[] = new byte[nSrcLen/2];
for (int i=0;i<nSrcLen/2;i++)
{
FirstResult[i] = 0;
}
int nLen = 0;
FirstResult = HexToValue(szSrc);
byte szFirstResult[] = FirstResult;
byte szBuf[] = new byte[nSrcLen/2];
for (int i=0;i<nSrcLen/2;i++)
{
szBuf[i] = 0;
}
byte szResult[] = new byte[nSrcLen/2];
for (int i=0;i<nSrcLen/2;i++)
{
szResult[i] = 0;
}
int nKey = Key < 0 ? ( Integer.MAX_VALUE + 1 ) * 2 + Key : Key;
long nKey2 = (long)nKey;
for(int I = 0 ; I < nSrcLen/2 ; I++)
{
int nVal = szFirstResult[I] < 0 ? ( Byte.MAX_VALUE + 1 ) * 2 + szFirstResult[I] : szFirstResult[I];
long nFirstResult = ((long)nVal);
long nXor = (nKey2/(long)256);
long nXorResult = nFirstResult ^ nXor;
szResult[I] = (byte)(nXorResult);
byte cFirstResult = ((byte)szFirstResult[I]);
long cFirstResultKey = (nFirstResult + nKey2);
nKey2 = cFirstResultKey * m_nC1Key + m_nC2Key;
}
String sRet = new String(szResult);
return sRet;
}
}
// LEncrypt.h: interface for the CLEncrypt class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_LEncrypt_H__E90C5977_3621_4B32_97AF_68E9CF9A0CC1__INCLUDED_)
#define AFX_LEncrypt_H__E90C5977_3621_4B32_97AF_68E9CF9A0CC1__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class CLEncrypt
{
public:
unsigned long m_nC1Key;
unsigned long m_nC2Key;
CLEncrypt();
virtual ~CLEncrypt();
char HexaChar(int nVal);
void ValueToHex(CString &sRet,unsigned char * szSrc,int nLen);
void HexToValue(unsigned char * szDest,int * nDestLen,LPCTSTR szSrc);
void Encrypt(CString &sRet,LPCTSTR szSrc,int Key);
int HexToDecimal(LPCTSTR szSrc);
int HexToDecimal(unsigned char cChar);
void Decrypt(CString &sRet,LPCTSTR szSrc,int Key);
};
#endif // !defined(AFX_LENCRIPT_H__E90C5977_3621_4B32_97AF_68E9CF9A0CC1__INCLUDED_)
// LEncrypt.cpp: implementation of the CLEncrypt class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "LEncrypt.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CLEncrypt::CLEncrypt()
{
m_nC1Key = 74102;
m_nC2Key = 12337;
}
CLEncrypt::~CLEncrypt()
{
}
char CLEncrypt::HexaChar(int nVal)
{
char * szHexaChar = "0123456789ABCDEF";
if (nVal > 15)
nVal = 0;
return szHexaChar[nVal];
}
// Byte로 구성된 데이터를 Hexadecimal 문자열로 변환
void CLEncrypt::ValueToHex(CString &sRet,unsigned char * szSrc,int nLen)
{
// int I;
// SetLength(Result, Length(S)*2); // 문자열 크기를 설정
// for I = 0 to Length(S)-1 do
unsigned char szBuf[1024];
memset(szBuf,0,sizeof(szBuf));
for(int I = 0 ; I < nLen ; I++)
{
szBuf[(I*2)+0] = HexaChar(((unsigned char)szSrc[I]) / 16);
szBuf[(I*2)+1] = HexaChar(((unsigned char)szSrc[I]) % 16);
}
sRet = szBuf;
}
// Hexadecimal로 구성된 문자열을 Byte 데이터로 변환
void CLEncrypt::HexToValue(unsigned char * szDest,int *nDestLen,LPCTSTR szSrc)
{
// int I;
// SetLength(Result, Length(S) div 2);
// for I = 0 to (Length(S) div 2) - 1 do
int nLen = strlen(szSrc);
for(int I = 0 ; I < nLen/2; I++)
{
char szChar[4];
memset(szChar,0,sizeof(szChar));
memcpy(szChar,&szSrc[I*2],2);
szDest[I] = HexToDecimal(szChar);
}
*nDestLen = nLen/2;
}
// 암호걸기
void CLEncrypt::Encrypt(CString & sRet,LPCTSTR szSrc,int Key)
{
unsigned long nKey2 = Key;
// byte I;
// CString FirstResult;
// SetLength(FirstResult, Length(S)); // 문자열의 크기를 설정
unsigned char FirstResult[1024];
memset(FirstResult,0,sizeof(FirstResult));
// for I = 1 to Length(S) do
int nLen = strlen(szSrc);
for(int I = 0 ; I < nLen ; I++)
{
unsigned char cSrc = ((unsigned char)szSrc[I]);
unsigned long nXor = ((unsigned long)nKey2/(unsigned long)256);
// shr : 오른쪽으로 쉬프트 시킴.
FirstResult[I] = (unsigned char)
(
cSrc ^ nXor
); // ^ : xor
unsigned char cFirstResult = ((unsigned char)FirstResult[I]);
unsigned long nFirstResultKey = (unsigned long)(cFirstResult + nKey2);
nKey2 = (nFirstResultKey) * m_nC1Key + m_nC2Key;
}
ValueToHex(sRet,FirstResult,nLen);
}
int CLEncrypt::HexToDecimal(LPCTSTR szSrc)
{
int nRet = 0;
int nLen = strlen(szSrc);
for (int i=0;i<nLen;i++)
{
char cChar = szSrc[i];
nRet = nRet * 16;
nRet += HexToDecimal(cChar);
}
return nRet;
}
int CLEncrypt::HexToDecimal(unsigned char cChar)
{
// 0 : 48
if (cChar == 'A' || cChar == 'a')
return 10;
if (cChar == 'B' || cChar == 'b')
return 11;
if (cChar == 'C' || cChar == 'c')
return 12;
if (cChar == 'D' || cChar == 'd')
return 13;
if (cChar == 'E' || cChar == 'e')
return 14;
if (cChar == 'F' || cChar == 'f')
return 15;
return cChar-48;
}
// 암호풀기
void CLEncrypt::Decrypt(CString &sRet,LPCTSTR szSrc,int Key)
{
// byte I;
// CString FirstResult;
// FirstResult = HexToValue(S);
unsigned char FirstResult[1024];
memset(FirstResult,0,sizeof(FirstResult));
int nLen = 0;
HexToValue(FirstResult,&nLen,szSrc);
// SetLength( Result, Length(FirstResult) );
// for I = 1 to Length(FirstResult) do
LPCTSTR szFirstResult = (LPCTSTR)FirstResult;
unsigned char szBuf[1024];
memset(szBuf,0,sizeof(szBuf));
unsigned char szResult[1024];
memset(szResult,0,sizeof(szResult));
unsigned long nKey2 = Key;
for(int I = 0 ; I < nLen ; I++)
{
unsigned long nFirstResult = ((unsigned long)szFirstResult[I]);
unsigned long nXor = (nKey2/(unsigned long)256);
unsigned long nXorResult = nFirstResult ^ nXor;
szResult[I] = (unsigned char)(nXorResult);
unsigned char cFirstResult = ((unsigned char)szFirstResult[I]);
unsigned long cFirstResultKey = (cFirstResult + nKey2);
nKey2 = cFirstResultKey * m_nC1Key + m_nC2Key;
}
sRet = szResult;
}
출처 : 고급 웹 UI 개발 라이브러리 Web Development Library 소스공개 : http://www.webdevlib.net