http://blog.naver.com/genesung?Redirect=Log&logNo=130082920571 님감사해요.. (__ ) 


사실 채팅 프로그램에 대단한 보안이 필요한건 아니고.. 

대충 보아하니.. des인데.. 그럼 서버측, 클라측 코드에 String Key 하나씩 심어놔야하나?

어디다가 숨겨놔야 안전할까...끙 -_-;;;  그나저나 text.getBytes()하고 있네.. 그나마 반갑다.

내가 암호,복호화할 데이터도.. directByteBuffer님까 -_-;



import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

 

private static final String _cipherAlgorithm = "DES";

 

String encryptText(String text, String key)
{
    String encrypted;
        
    try
    {
        SecretKeySpec ks = new SecretKeySpec(generateKey(key), _cipherAlgorithm);
        Cipher cipher = Cipher.getInstance(_cipherAlgorithm);
        cipher.init(Cipher.ENCRYPT_MODE, ks);
        byte[] encryptedBytes = cipher.doFinal(text.getBytes());
        encrypted = new String(Base64Coder.encode(encryptedBytes));
    }
    catch (Exception e)
    {
        e.printStackTrace();
        encrypted = text;
    }
        
    return encrypted;
}

 

String decryptText(String text, String key)
{
    String decrypted;

    try
    {
        SecretKeySpec ks = new SecretKeySpec(generateKey(key), _cipherAlgorithm);
        Cipher cipher = Cipher.getInstance(_cipherAlgorithm);
        cipher.init(Cipher.DECRYPT_MODE, ks);
        byte[] decryptedBytes = cipher.doFinal(Base64Coder.decode(text));
        decrypted = new String(decryptedBytes);
    }
    catch (Exception e)
    {
        decrypted = text;
    }

    return decrypted;
}

 

byte[] generateKey(String key)
{
    byte[] desKey = new byte[8];
    byte[] bkey = key.getBytes();
        
    if (bkey.length < desKey.length)
    {
        System.arraycopy(bkey, 0, desKey, 0, bkey.length);
            
        for (int i = bkey.length; i < desKey.length; i++)
            desKey[i] = 0;
    }
    else
        System.arraycopy(bkey, 0, desKey, 0, desKey.length);
        
    return desKey;
}


'프로그래밍 > 보안' 카테고리의 다른 글

jvm의 입장?  (0) 2014.01.02
"보안" 관련 문서(2013.12.11 자바 보안객체 사용 추가)  (0) 2014.01.01
by givingsheart 2014. 1. 1. 16:35