Presentation is loading. Please wait.

Presentation is loading. Please wait.

2 : 대칭암호화 소제목 : 기본적인 대칭암호화.

Similar presentations


Presentation on theme: "2 : 대칭암호화 소제목 : 기본적인 대칭암호화."— Presentation transcript:

1 2 : 대칭암호화 소제목 : 기본적인 대칭암호화

2 이 장에서 다룰 내용 2 1 대칭 암호화 2 2 javax.crypto.Cipher 2 3 javax.crypto.KeyGenrator 2 4 예제 #1(TripleDES) 2 5 예제 #2(Blowfish)

3 대칭 암호화 사용/적용 대칭 암호화는 다양한 분야에서 응용할 수 있다.
대칭 암호화는 비대칭 암호화나 공개 키 암호화보다 훨씬 빠르다, 이런 이유로 많은 양의 데이터가 전송되는 환경에서 자주 쓰인다. 파일 암호화, 네트워크 암호화, 데이터베이스 암호화 등에 많이 쓰인다. 하지만 중요한 약점은 키가 같다는 점이다. 암복화에서 같은 키가 사용된다. 만약 대칭 암호화된 메시지를 보내려면 송신자와 수신자 모두가 키를 맞춰야 한다. 이런 이유로 많은 양의 데이터를 대칭 암호화를 사용하여 암호화 하고, 대칭 키를 비대칭 암호화를 사용하여 암호화하는 것이 좋다. Key 문제가 제일 단점

4 javax.crypto.Cipher 자바에서 제공하는 암복호화 엔진이다.
getInstance() 생성자 대신 객체 생성시 이용한다. 예) Cipher cipher = Cipher.getInstance(“Desede/ECB/PKCS5Padding”); 첫번째 매개변수는 알고리즘 이름이다. 두번째 인자는 모드이다. 세번째 인자는 패딩이다. 모드와 패딩은 뒤에서 설명하겠다.. new 연산자를 사용하지 않고 getInstance를 이용해서 객체화 한다. new 를 사용하지 않는 이유는 new를 사용하면 여러 사람이 동시에 메모리에 올려서 사용할 수 있지만 getInstance 메소드를 이용해서 “싱글톤” 기법을 이용. => 객체가 없을 때만 메모리에 올리고 이미 메모리에 올라가있다면 메모리에 올리지 않기 때문에 해당 객체는 하나만 생성된 상태에서 작업됨.(static 메소드임) init() 객체 생성후 메소드 초기화하는 작업이다. 예) cipher.init(Cipher.ENCRYPT_MODE, mykey); ENCRYPT_MODE 혹은 DECRYPT_MODE로 작동방식을 결정한다. 두번째는 미리 정의 한 키를 적어준다.

5 getInstance() -> init() -> update() -> doFinal()
javax.crypto.Cipher update() 암복호화 할 대상을 바이트 배열의 형태로 데이터를 사이퍼에 넘겨줘야 한다. 예) String myString = “Hello Cipher”; byte[] plaintext = myString.getBytes(“UTF8”); byte[] ciphertext = cipher.update(plaintext); 인코딩을 지정해 주지 않으면 기본적으로 사용하는 인코딩이 명시 되는데 암호화와 복호화시 다른 인코딩 방식이 지정될 수 있으니 꼭 명시하여 사용하자! 인코딩을 언급 해주지 않으면 암호화한 컴퓨터, 복호화하는 컴퓨터가 다를 경우 충돌로 인해 원하는 결과가 나오지 않을 수 있다. doFinal() 사이퍼로 부터 암호화된 데이터를 얻었으므로 doFinal()로 암호화된 데이터가 있는 바이트 배열을 구한다. 예) byte[] ciphertext = cipher.doFinal(); getInstance() -> init() -> update() -> doFinal() 4가지 메소드를 사용하므로 꼭 알아 두자

6 javax.crypto.KeyGenrator
KeyGenerator를 이용하여 대칭 암호화해 쓸 키를 생성할수 있다. 3개의 메소드를 기본으로 한다. getInstance() 생성자 대신 객체 생성시 이용한다. 예) KeyGenerator keyGenerator = KeyGenerator.getInstance(“DESede”); DESede 키를 생성할 키 생성자를 생성할수 있다. 알고리즘 init() KeyGenerator의 객체를 초기화 할때 키의 크기를 정한다. 사용하는 알고리즘 마다의 키가 틀린것을 명시해서 사용한다. 예) keyGenerator.init(168);->TripleDes는 168비트로 사용 generateKey() 이 메소드는 Cipher의 인스턴스에 쓰일 Key객체를 실제로 만들어낸다. 예) Key myKey = keyGenerator.generateKey(); * Key 클래스는 java.security.Key 패키지에 있다. 실제 Key 생성

7 예제 #1 import java.security.*; import javax.crypto.*;
public class SimpleExample { public static void main (String[] args) throws Exception String text = “Hello Java Security!!”; System.out.println(“Start TripleDES key..."); KeyGenerator keyGenerator = KeyGenerator.getInstance("TripleDES"); keyGenerator.init(168); // need to initialize with the keysize Key key = keyGenerator.generateKey(); System.out.println("Done generating the key."); Cipher cipher = Cipher.getInstance("TripleDES/ECB/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, key); //암호화 모드 Key 생성

8 예제 #1 byte[] plaintext = text.getBytes("UTF8");
System.out.println("\nPlaintext: "); for (int i=0; i<plaintext.length; i++) { System.out.print(plaintext[i]+" "); } byte[] ciphertext = cipher.doFinal(plaintext); System.out.println("\n\nCiphertext: "); for (int i=0;i<ciphertext.length;i++) { System.out.print(ciphertext[i]+" "); cipher.init(Cipher.DECRYPT_MODE, key); //->복호화모드 byte[] decryptedText = cipher.doFinal(ciphertext); String output = new String(decryptedText,"UTF8"); System.out.println("\n\nDecrypted text: "+output); plaintext 는 string 형태 가능 ciphertext 는 string 형태 불가능

9 예제 #2 import java.security.*; import javax.crypto.*;
public class BlowfishExample { public static void main (String[] args) throws Exception String text = “Hello Java Security!!”; System.out.println("Generating a Blowfish key..."); KeyGenerator keyGenerator = KeyGenerator.getInstance("Blowfish"); keyGenerator.init(128); // keysize 128bit Key key = keyGenerator.generateKey(); System.out.println("Done generating the key."); Cipher cipher = Cipher.getInstance("Blowfish/ECB/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, key); byte[] plaintext = text.getBytes("UTF8");

10 예제 #2 System.out.println("\nPlaintext: ");
for (int i=0;i<plaintext.length;i++) { System.out.print(plaintext[i]+" "); } byte[] ciphertext = cipher.doFinal(plaintext); System.out.println("\n\nCiphertext: "); for (int i=0;i<ciphertext.length;i++) { System.out.print(ciphertext[i]+" "); cipher.init(Cipher.DECRYPT_MODE, key); // Perform the decryption byte[] decryptedText = cipher.doFinal(ciphertext); String output = new String(decryptedText,"UTF8"); System.out.println("\n\nDecrypted text: "+output);

11 Thank You !


Download ppt "2 : 대칭암호화 소제목 : 기본적인 대칭암호화."

Similar presentations


Ads by Google