Bouncycasle + JCE = DES 알고리즘 사용 가능 Java


 우선 JCE 설정을 해야합니다.


    JCE 설정


그리고 일반적으로 자바에서 자주 사용되는 보안 라이브 러리인 Boucycastle 패키지를 다운로드 받습니다.



이후에 저처럼 생성된 프로젝트에 복사 후 붙여넣기를 하세요.






※ 이후 소스를 참고하세요.


import java.security.Security;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class MainClass {
public static void main(String[] args) throws Exception {

Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

byte[] input = "ks".getBytes();
byte[] keyBytes = new byte[]
{ 0x01, 0x23, 0x45, 0x67, (byte)0x89, (byte)0xab, (byte)0xcd, (byte)0xef };
byte[] ivBytes = new byte[]
{ 0x00, 0x01, 0x02, 0x03, 0x00, 0x00, 0x00, 0x01 };

SecretKeySpec key = new SecretKeySpec(keyBytes, "DES");
IvParameterSpec ivSpec = new IvParameterSpec(ivBytes);
Cipher cipher = Cipher.getInstance("DES/CTR/NoPadding", "BC");
System.out.println("input : " + new String(input));

// encryption pass
cipher.init(Cipher.ENCRYPT_MODE, key, ivSpec);
byte[] cipherText = new byte[cipher.getOutputSize(input.length)];
int ctLength = cipher.update(input, 0, input.length, cipherText, 0);
ctLength += cipher.doFinal(cipherText, ctLength);
System.out.println("cipher: " + new String(cipherText) + " bytes: " + ctLength);

// decryption pass
cipher.init(Cipher.DECRYPT_MODE, key, ivSpec);
byte[] plainText = new byte[cipher.getOutputSize(ctLength)];
int ptLength = cipher.update(cipherText, 0, ctLength, plainText, 0);
ptLength += cipher.doFinal(plainText, ptLength);
System.out.println("plain : " + new String(plainText) + " bytes: " + ptLength);
} }

프로젝트 통째로 다운 받기...는 아래 클릭






ps. Bouncycastle는 자바에서 사용하는 그대로 안드로이드에서 사용하질 못합니다.
     구글링을 해보니 일부 패키지가 안드로이드에 포함되어서 그렇다는데,
     정확한 근본적인 문제는 뭔지 모르겠군요.



공유하기 버튼

 

트랙백

이 글과 관련된 글 쓰기 (트랙백 보내기)
TrackbackURL : http://minamjun11.egloos.com/tb/740180 [도움말]

덧글

댓글 입력 영역



통계 위젯 (화이트)

105777
3066
158110