2012년 5월 10일 목요일

[JAVA_Security]Java(자바) 데이터 암호화 XOR

@ 데이터 암호화를 위해 논리 연산의 XOR를 상용한 소스이다.


 public class XorSample  
 {  
   public final static byte[] KEY = "4d2dg".getBytes( );  
   public static void main( String[] args )  
   {  
     XorSample sample = new XorSample( );  
     sample.exec( );  
   }  
   public void exec( )  
   {  
     StringBuffer data = new StringBuffer( );  
     for ( int i = 0 ; i < 30 ; i++ )  
     {  
       data.append( i );  
       data.append( "안녕하세요!" );  
       data.append( i );  
     }  
     byte[] encrypt = xor( data.toString( ).getBytes( ) , KEY );  
     System.out.println( encrypt.length );  
     System.out.println( "변환:" + new String( encrypt ) );  
     byte[] decrypt = xor( encrypt , KEY );  
     System.out.println( "재변환:" + new String( decrypt ) );  
     System.out.println( decrypt.length );  
   }  
   public byte[] xor( byte[] data , byte[] key )  
   {  
     final int orgLength = data.length;  
     final int keyLength = key.length;  
     final byte[] converted = new byte[orgLength];  
     for ( int i = 0 , j = 0 ; i < orgLength ; i++ , j++ )  
     {  
       converted[ i ] = ( byte ) ( data[ i ] ^ key[ j ] );  
       if ( j >= ( keyLength - 1 ) )  
       {  
         j = 0;  
       }  
     }  
     return converted;  
   }  
 }  

댓글 없음:

댓글 쓰기