[À¯¿ëÇÑ 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
|
|