자바소스 - 간단한 암호화 방법
목록  
제 목 [Java2] 간단한 암호화 방법
작성자 박세청 작성일 2009/01/09 08:12


자바 간단한 암호화 방법

 

public class Encryption{
 
 public String encryptionPassword(String strKey){
  if(strKey==null||strKey.length()==0){
   return "";
  }
  int iLen = strKey.length();
  String strEncryption ="";
  for(int iCnt=0;iCnt<iLen;iCnt++){
   //암호화 (문자열의 짝수, 홀수 위치 구분)
   if(iCnt%2==0){  //홀수문자
    strEncryption +=  (char) ((byte) strKey.charAt(iCnt) - iLen + iCnt);
   }else{//짝수문자
    strEncryption +=  (char) ((byte) strKey.charAt(iCnt) + iLen + iCnt);
   }
  }
  return strEncryption;
 }

 public String decryptionPassword(String strKey){
  if(strKey==null||strKey.length()==0){
   return "";
  }
  int iLen = strKey.length();
  String strDecryption ="";
  for(int iCnt=0;iCnt<iLen;iCnt++){
   //암호화 (문자열의 짝수, 홀수 위치 구분)
   if(iCnt%2==0){  //홀수문자
    strDecryption +=  (char) ((byte) strKey.charAt(iCnt) + iLen - iCnt);
   }else{//짝수문자
    strDecryption +=  (char) ((byte) strKey.charAt(iCnt) - iLen - iCnt);
   }
  }
  return strDecryption;
 }

 public static void main(String[] args){
  String key ="abc$%";
  Encryption e = new Encryption();
  System.out.println(key);
  System.out.println(e.encryptionPassword(key));
  System.out.println(e.decryptionPassword(e.encryptionPassword(key)));
 }
}





이전글 바이트(byte)로 한글 자를 경우 깨지는 문제 처리
다음글 DB 쿼리 결과를 화일로 저장..

목록