2010. 2. 8. 11:10 java
암호화 관련 간단 테스트 코드
http://nogun.tistory.com/64
귀찮아서 간단히 한 코덱 소스
package com.futurenuri.common.util;
import java.security.InvalidKeyException;
import java.security.Key;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import org.apache.commons.codec.binary.Base64;
public class LocalEncrypter {
// private static Log log = LogFactory.getLog(LocalEncrypter.class);
private String algorithm = "DESede";
private Key key;
private Cipher cipher;
public LocalEncrypter() throws Exception {
key = KeyGenerator.getInstance( algorithm ).generateKey();
cipher = Cipher.getInstance( algorithm );
}
/**
* 엔코드 한 스트링을 돌려줌
* @param input
* @return
* @throws InvalidKeyException
* @throws BadPaddingException
* @throws IllegalBlockSizeException
*/
public String getEncrypt(String input) throws InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
cipher.init( Cipher.ENCRYPT_MODE, key );
byte [] inputBytes = input.getBytes();
byte [] encryptionBytes = cipher.doFinal( inputBytes );
return Base64.encodeBase64String(encryptionBytes);
}
/**
* 디코드한 스트링을 돌려줌
* @param encodeString
* @return
* @throws InvalidKeyException
* @throws BadPaddingException
* @throws IllegalBlockSizeException
*/
public String getDecrypt(String encodeString) throws InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
byte [] encryptionBytes = Base64.decodeBase64(encodeString);
cipher.init( Cipher.DECRYPT_MODE, key );
byte [] recoveredBytes = cipher.doFinal( encryptionBytes );
return new String( recoveredBytes );
}
}
우연히 eclipse 에서 프로젝트를 수행하는데, 어디선가 에러가 나서 확인해보았다.
Access restriction: The constructor BASE64Decoder() is not
accessible due to restriction on required library
C:\Java\jdk1.5.0_17\jre\lib\rt.
음.. 왜 그럴까 검색을 해보았더니, 문서가 나와있었다.
sun.* package에 있는 라이브러리는 pure 자바로 개발된 코드가 아니어서 플랫폼 독립적이지 않다고 하네.
마지막 문구에는 대놓고 쓰지말라는
In general, writing java
programs that rely on sun.* is risky: they are not portable, and are not
supported.
그래서 어떻게 Base64 Encoding/Decoding 을 처리하느냐...
친절하게 apache commons에서 제공하는 codec 라이브러리를 이용하라고 답을
주시네..허허허..
내가 개발한게 아니어서..이클립스에서 해당 관련 사항은 Warning으로 처리하도록 변경~~~
이놈의 이기주의..ㅡㅡ;
관련자료 : http://nogun.tistory.com/65
귀찮아서 간단히 한 코덱 소스
package com.futurenuri.common.util;
import java.security.InvalidKeyException;
import java.security.Key;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import org.apache.commons.codec.binary.Base64;
public class LocalEncrypter {
// private static Log log = LogFactory.getLog(LocalEncrypter.class);
private String algorithm = "DESede";
private Key key;
private Cipher cipher;
public LocalEncrypter() throws Exception {
key = KeyGenerator.getInstance( algorithm ).generateKey();
cipher = Cipher.getInstance( algorithm );
}
/**
* 엔코드 한 스트링을 돌려줌
* @param input
* @return
* @throws InvalidKeyException
* @throws BadPaddingException
* @throws IllegalBlockSizeException
*/
public String getEncrypt(String input) throws InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
cipher.init( Cipher.ENCRYPT_MODE, key );
byte [] inputBytes = input.getBytes();
byte [] encryptionBytes = cipher.doFinal( inputBytes );
return Base64.encodeBase64String(encryptionBytes);
}
/**
* 디코드한 스트링을 돌려줌
* @param encodeString
* @return
* @throws InvalidKeyException
* @throws BadPaddingException
* @throws IllegalBlockSizeException
*/
public String getDecrypt(String encodeString) throws InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
byte [] encryptionBytes = Base64.decodeBase64(encodeString);
cipher.init( Cipher.DECRYPT_MODE, key );
byte [] recoveredBytes = cipher.doFinal( encryptionBytes );
return new String( recoveredBytes );
}
}
'java' 카테고리의 다른 글
자바 관련 좋은 사이트 및 서적 (0) | 2010.04.15 |
---|---|
ssh 관련 메모 (0) | 2010.02.17 |
톰캣, 자바 메모리 관련 (0) | 2010.02.03 |
파일 다운 로드 관련 (0) | 2010.02.03 |
URL 컨텐츠 얻어 오는 소스 테스트 (0) | 2009.12.22 |