'Cipher'에 해당되는 글 4건

  1. 2015.11.05 MAC
  2. 2015.11.05 RSA 암호화
  3. 2015.11.05 대칭키 암호화
  4. 2015.11.05 패스워드 기반 암호화

MAC

UP!/Crypto 2015. 11. 5. 10:59

메시지 인증 코드(MAC)

 

     내용

가)    키가 있는 메시지 다이제스트

나)    안전하지 않은 네트워크를 통해 수신된 데이터를 인증할 때

다)    수신자와 송신자가 비밀 키를 교환하여 이 키를 이용하여 검증

라)    SSL에 많이 사용됨

마)    비밀 키를 공유해야 하는 것이 문제

바)    수신측에서 데이터 검증

사)    HMAC(Hashed Message Authentication Code)라 불리는 암호화 해쉬 함수에 기반

아)    Sun JCE에서 제공하는 HMAC 함수

     HmacMD5

     HmacSHA1

 

     사용방법

가)    javax.crypto.Mac

     Cipher MessageDigest가 합쳐진 것

 

나)    예제

import java.security.SecureRandom;

import javax.crypto.Mac;

import javax.crypto.spec.SecretKeySpec;

import com.Ostermiller.util.Base64;

 

public class MACExample {

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

             SecureRandom random = new SecureRandom();

             byte[] keyBytes = new byte[20];

             random.nextBytes(keyBytes);

             SecretKeySpec key = new SecretKeySpec(keyBytes, "HMACSHA1");

             System.out.println("Key: " + Base64.encode(key.getEncoded()));

             //Mac 인스턴스를 획득하고 초기화한다.

             Mac mac = Mac.getInstance("HmacSHA1");

             mac.init(key);

             mac.update("I LOVE YOU!".getBytes("UTF8"));

             byte[] result = mac.doFinal();

            

             System.out.println("MAC: " + Base64.encode(result));

       }

}

 

'UP! > Crypto' 카테고리의 다른 글

MessageDigest  (0) 2015.11.05
Session key  (0) 2015.11.05
대칭키 암호화  (0) 2015.11.05
패스워드 기반 암호화  (0) 2015.11.05
JCA, JCE  (0) 2015.11.05
Posted by 으랏차
,

RSA 암호화

카테고리 없음 2015. 11. 5. 10:57

RSA 파일 암호화

 

     RSA 키 쌍 생성

 

     공개 키로 파일을 암호화

가)    RSA 키의 인코딩

     X.509를 사용하여 인코딩 사용(공개 키와 인증서를 전달할 때 사용하는 공개 형식)

     개인 키는 PKCS#8로 인코딩

     공개/개인 키든 getEncoded()를 호출하면 자동으로 인코딩 됨

나)    RSA 키의 디코딩

     인코딩된 키의 스펙 객체를 생성

     KeyFactory를 사용하여 적절한 키 타입의 인스턴스를 활성화

     공개 키 디코딩

X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);

KeyFactory keyFactory = KeyFactory.getInstance(“RSA”);

PublicKey publicKey = keyFactory.generatePublic(keySpec);

 

     개인 키의 디코딩

PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);

KeyFactory keyFactory = KeyFactory.getInstance(“RSA”);

PrivateKey privateKey = keyFactory.generatePrivate(keySpec);

 

다)    암호화된 파일 형식

     포맷

키의 길이

암호화된 키

초기화백터(16바이트)

암호문서

 

라)    예제

private static void createKey(String password) throws Exception {

             // RSA 생성

             KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");

             keyPairGenerator.initialize(1024);

             KeyPair keyPair = keyPairGenerator.genKeyPair();

            

             //공개키를 파일에 쓴다.

             byte[] publicKeyBytes = keyPair.getPublic().getEncoded();

             FileOutputStream fos = new FileOutputStream("c:\\publicKey");

             fos.write(publicKeyBytes);

             fos.close();

            

             // 개인 키를 암호화한 후에 파일에 쓴다.

             byte[] privateKeyBytes = passwordEncrypt(password.toCharArray(), keyPair.getPrivate().getEncoded());

             fos = new FileOutputStream("c:\\privateKey");

             fos.write(publicKeyBytes);

             fos.close();

       }

      

       private static byte[] passwordEncrypt(char[] password, byte[] plaintext) throws Exception {

             // salt 생성

             byte[] salt = new byte[9];

             Random random = new Random();

             random.nextBytes(salt);

            

             // PBE 키와 사이퍼 생성

             PBEKeySpec keySpec = new PBEKeySpec(password);

             SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithSHAAndTwofish-CBC");

             SecretKey key = keyFactory.generateSecret(keySpec);

             PBEParameterSpec paramSpec = new PBEParameterSpec(salt, ITERATIONS);

            

             Cipher cipher = Cipher.getInstance("PBEWithSHAAndTwofish-CBC");

             cipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);

            

             byte[] cipherText = cipher.doFinal(plaintext);

            

             ByteArrayOutputStream baos = new ByteArrayOutputStream();

             baos.write(salt);

             baos.write(cipherText);

            

            

             return baos.toByteArray();

       }

      

       private static void encrypt(String fileInput) throws Exception {

             String publicKeyFileName = "c:\\publicKey";

             // 공개 키가 저장된 파일로부터 keyByte 바이트 배열을 생성한다.

             FileInputStream fis = new FileInputStream(publicKeyFileName);

             ByteArrayOutputStream baos = new ByteArrayOutputStream();

            

             int theByte = 0;

             while((theByte = fis.read()) != -1)

                    baos.writeTo(baos);

             fis.close();

            

             byte[] keyBytes = baos.toByteArray();

             baos.close();

            

             // 인코딩된 키를 RSA 공개 키의 인스턴스로 바꾼다.

             X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);

             KeyFactory keyFactory = KeyFactory.getInstance("RSA");

             PublicKey publicKey = keyFactory.generatePublic(keySpec);

            

             String fileOutput = fileInput + ENCRYPTED_FILENAME_SUFFIX;

             DataOutputStream output = new DataOutputStream(new FileOutputStream(fileOutput));

            

             // RSA 공개 키를 이용하여 세션 키를 암호화할 사이퍼를 생성한다.

             Cipher rsaCipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");

             rsaCipher.init(Cipher.ENCRYPT_MODE, publicKey);

            

             // 세션 생성

             KeyGenerator rijndaelKeyGenerator = KeyGenerator.getInstance("Rijndael");

             rijndaelKeyGenerator.init(256);

             Key rijndaelKey = rijndaelKeyGenerator.generateKey();

            

             // RSA 사이퍼를 이용하여 세션 키를 암호화 하고 파일에 저장한다.

             // 키의 길이, 인코딩된 세션 형식이다.

             byte[] encodedKeyBytes = rsaCipher.doFinal(rijndaelKey.getEncoded());

             output.writeInt(encodedKeyBytes.length);

             output.write(encodedKeyBytes);

            

             // 초기화 벡터

             SecureRandom random = new SecureRandom();

             byte[] iv = new byte[16];

             random.nextBytes(iv);

            

             //IV 파일에 쓴다

             output.write(iv);

            

             //IV 생성한 세션 키를 이용하여 파일의 내용을 암호화한다.

             IvParameterSpec spec = new IvParameterSpec(iv);

             Cipher symmetricCipher = Cipher.getInstance("Rijndael/CBC/PKCS5Padding");

             symmetricCipher.init(Cipher.ENCRYPT_MODE, rijndaelKey, spec);

             CipherOutputStream cos = new CipherOutputStream(output, symmetricCipher);

            

             FileInputStream input = new FileInputStream(fileInput);

             theByte = 0;

             while((theByte = input.read()) != -1)

                    cos.write(theByte);

            

             input.close();

             cos.close();

             return;

       }

 

     개인 키로 복호화

가)    예제

private static byte[] passwordDecrypt(char[] password, byte[] ciphertext) throws Exception {

             // salt 읽는다. 개인키는 8byte salt 사용했다.

             byte[] salt = new byte[8];

             ByteArrayInputStream bais = new ByteArrayInputStream(ciphertext);

             bais.read(salt, 0 ,8);

            

             byte[] remainingCiphertext = new byte[ciphertext.length-8];

             bais.read(remainingCiphertext, 0, ciphertext.length-8);

            

             //PBE 사이퍼를 생성하여 세션 키를 복원한다.

             PBEKeySpec keySpec = new PBEKeySpec(password);

             SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithSHAAndTwofish-CBC");

             SecretKey key = keyFactory.generateSecret(keySpec);

             PBEParameterSpec paramSpec = new PBEParameterSpec(salt, ITERATIONS);

             Cipher cipher = Cipher.getInstance("PBEWithSHAAndTwofish-CBC");

            

             // 복호화

             cipher.init(Cipher.DECRYPT_MODE, key, paramSpec);

             return cipher.doFinal(remainingCiphertext);

       }

      

       private static void decrypt(String password, String fileInput) throws Exception {

             String privateKeyFilename = "c:\\privateKey";

             // 파일로부터 개인 키를 읽어들인다.

             FileInputStream fis = new FileInputStream(privateKeyFilename);

             ByteArrayOutputStream baos = new ByteArrayOutputStream();

            

             int theByte = 0;

             while((theByte = fis.read()) != -1)

                    baos.write(theByte);

             fis.close();

             byte[] keyByte = baos.toByteArray();

             baos.close();

            

              // 암호화된 개인 바이트를 복원한다.

             keyByte = passwordDecrypt(password.toCharArray(), keyByte);

            

             // RSA 개인 키를 복원한다.

             PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyByte);

             KeyFactory keyFactory = KeyFactory.getInstance("RSA");

             PrivateKey privateKey = keyFactory.generatePrivate(keySpec);

            

             // 개인 키를 이용하여 사이퍼를 생성하고 세션 키를 복호화한다

             Cipher rsaCipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");

             DataInputStream dis = new DataInputStream(new FileInputStream(fileInput));

             byte[] encryptedKeyBytes = new byte[dis.readInt()];

             dis.readFully(encryptedKeyBytes);

            

             rsaCipher.init(Cipher.DECRYPT_MODE, privateKey);

             byte[] rijndaelKeyByte = rsaCipher.doFinal(encryptedKeyBytes);

            

             SecretKey rijndaelKey = new SecretKeySpec(rijndaelKeyByte, "Rijndael");

            

             byte[] iv = new byte[16];

             dis.readFully(iv);

             IvParameterSpec spec = new IvParameterSpec(iv);

            

             Cipher cipher = Cipher.getInstance("Rijndael/CBC/PKCS5padding");

             cipher.init(Cipher.DECRYPT_MODE, rijndaelKey, spec);

             CipherInputStream cis = new CipherInputStream(dis, cipher);

            

             FileOutputStream fos = new FileOutputStream(fileInput + DECRYPTED_FILENAME_SUFFIX);

            

             theByte = 0;

             while((theByte = cis.read()) != -1)

                    fos.write(theByte);

            

             cis.close();

             fos.close();

            

             return;     

       }

 

나)    복호화 과정

 

     세션 키 암호화를 사용해야 하는 이유

가)    해킹에 대비해, 개인 키를 저장할 때 암호화할 필요가 있다.

나)    세션 키를 사용하면 속도가 훨씬 빨라진다.

다)    RSA는 특정 메시지에 대해서 매우 취약하다.

 

Posted by 으랏차
,

대칭키 암호화

UP!/Crypto 2015. 11. 5. 10:55

     키의 저장

파일 시스템 안에 키를 저장할 경우 이에 대한 보안이 필요

가)    키 래핑(Wrapping)과 언래핑(Unwrapping)

     wrap() 함수 : Cipher 클래스에 있는 함수로 키를 매개변수로 받아서 암호화된 키의 바이트 배열을 반환

cipher.init(Cipher.WRAP_MODE, passwordKey, paramSpec);

byte[] encryptedKeyBytes = cipher.wrap(secretKey);

 

     unwrap() : 암호화된 키의 바이트 배열을 Key로 반환

cipher.init(Cipher.UNWRAP_MODE, passwordKey, paramSpec);

Key key = cipher.unwrap(encryptedKeyBytes, “Blowfish”, Cipher.SECRET_KEY);

 

나)    래핑을 하지 않는 키 암호화

     wrap() unwrap()이 없다면 Key.getEncoded(), SecretKeySpec을 이용해 암호화

     java.security.Key : 암호화된 키의 바이트 배열을 반환

byte[] keyBytes = myKey.getBytes();

cipher.init(Cipher.ENCRYPT_MODE, passwordKey, paramSpec);

byte[] encryptedKeyBytes = cipher.doFinal(keyBytes);

 

     SecretKeySpec : 키를 복호화

cipher.init(Cipher.DECRYPT_MODE, passwordKey, paramSpec);

byte[] keyBytes = cipher.doFinal(encryptedKeyBytes);

SecretKeySpec myKey = new SecretKeySpec(keyBytes, “Blowfish”);

 

     사이퍼 스트림(CipherStream)

가)    표준 입/출력 스트림을 암/복호화를 위한 래퍼 클래스를 제공

     CipherInputStream

     CipherOutputStream

 

나)    예제

 FileInputStream input = new FileInputStream(“plaintext_filename”);

FileOutputStream output = new FileOutputStream(“ciphertext_filename”);

CipherOutputStram cipherOutput = new CipherOutputStream(output, cipher);

int r = 0;

while((r=input.read()) != null)

   cipherOutput.write(r);

cipherOutput.close();

output.close();

input.close();

 


     IV로 사이퍼 초기화

초기화 백터의 바이트를 생성하기 위해 java.security.SecureRandom을 이용

가)    SecureRandom

    

 byte[] randomBytes = new byte[8];

SecureRandom random = new SecureRandom();

random.nextBytes(randomBytes);

 

 

          나) IV 생성과 사용

     생성

IVParameterSpec iv = new IVParameterSpec(randomBytes);

 

     사용

Cipher cipher = Cipher.getInstance(“Blowfish/CBC/PCS5PADDing”);

cipher.init(Cipher.ENCRYPT_MODE, key, iv);

 

다)    IV 추적

     복호화 할 때에도 동일한 IV가 사용되어야 함

     salt와 매우 비슷하며 숨길 필요 없이 암호문에 붙여서 사용함

 

     사이퍼 스트림 예제 파일 인코더

가)    사이퍼 스트림을 이용해 파일을 암/복호화

나)    키는 패스워드 기반 암호화로 파일에 저장 및 복호화

다)    4개의 기능을 포함

     createKey() : AES 키를 생성 하고 패스워드를 이용하여 암호화하여 파일에 저장

/**

        * 1. 256bit AES(Rijndael)키를  생성

        * 2. PBE 이용하여 생성한 키를 암호화하여 파일에 저장

        */

       private static void createKey(char[] password) {

             System.out.println("Generating a RijnDael key...");

             // AES 대칭 생성기 객체 생성

             KeyGenerator keyGenerator = null;

             try {

                    keyGenerator = KeyGenerator.getInstance("Rijndael");

             } catch (NoSuchAlgorithmException e) {}

             // AES 생성

             keyGenerator.init(256);

             Key key = keyGenerator.generateKey();

             System.out.println("Done generating the key");

            

             // PBE 시작

             // salt 생성

             byte[] salt = new byte[8];

             SecureRandom random = new SecureRandom();

             random.nextBytes(salt);

            

             // password PBEKeySpec 생성

             PBEKeySpec pbeKeySpec = new PBEKeySpec(password);

             SecretKeyFactory keyFactory = null;;

             SecretKey pbeKey = null;

             try {

                    keyFactory = SecretKeyFactory.getInstance("PBEWithSHAAndTwofish-CBC");

                    pbeKey = keyFactory.generateSecret(pbeKeySpec);

             } catch (NoSuchAlgorithmException e) {}

             catch (InvalidKeySpecException e) {}

             // PBEParameterSpec 생성

             PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, ITERATIONS);

             // Cipher 생성 초기화

             Cipher cipher = null;

             byte[] encryptedKeyBytes = null;

             try {

                    cipher = Cipher.getInstance("PBEWithSHAAndTwofish-CBC");

                    cipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParamSpec);

                   

                    // AES 암호화

                    encryptedKeyBytes = cipher.doFinal(key.getEncoded());

             } catch (NoSuchAlgorithmException e) {}

             catch (NoSuchPaddingException e) {}

             catch (InvalidKeyException e) {}

             catch (InvalidAlgorithmParameterException e) {}

             catch (IllegalBlockSizeException e) {}

             catch (BadPaddingException e) {}

            

             FileOutputStream fos;

             try {

                    fos = new FileOutputStream(KEY_FILENAME);

                    //salt 암호화된 바이트를 쓴다.

                    fos.write(salt);

                    fos.write(encryptedKeyBytes);

                    fos.close();

             } catch (FileNotFoundException e) {}

             catch (IOException e) {}

       }

 

     loadKey() : 키가 저장된 파일로부터 읽어들여 복호화하여 키를 생성

private static Key loadKey(char[] password) throws Exception {

             // 파일로부터 읽어들임

             FileInputStream fis = new FileInputStream(KEY_FILENAME);

             ByteArrayOutputStream baos = new ByteArrayOutputStream();

             int i = 0;

             while((i=fis.read()) != -1)

                    baos.write(i);

             fis.close();

             byte[] saltAndKeyBytes = baos.toByteArray();

             baos.close();

             // salt 분리함. Base64 인코딩을 하지 않았기에 8byte 그대로 유지됨

             byte[] salt = new byte[8];

             System.arraycopy(saltAndKeyBytes, 0, salt, 0, 8);

            

             // key 분리함

             int length = saltAndKeyBytes.length - 8;

             byte[] encryptedKeyBytes = new byte[length];

             System.arraycopy(saltAndKeyBytes, 8, encryptedKeyBytes, 0, length);

            

             // PBE 시작

             PBEKeySpec pbeKeySpec = new PBEKeySpec(password);

             SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithSHAAndTwofish-CBC");

             SecretKey pbeKey = keyFactory.generateSecret(pbeKeySpec);

             PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, ITERATIONS);

             // Cipher 생성 초기화

             Cipher cipher = Cipher.getInstance("PBEWithSHAAndTwofish-CBC");

             cipher.init(Cipher.DECRYPT_MODE, pbeKey, pbeParamSpec);

             // 복호화

             byte[] decryptedKeyBytes = cipher.doFinal(encryptedKeyBytes);

             SecretKeySpec key = new SecretKeySpec(decryptedKeyBytes, "Rijndael");

            

             return key;

       }

 

     encrypt() : 사이퍼 스트림을 이용하여 암호화 수행

private static void encrypt(char[] passowrd, String fileInput, String fileOutput) throws Exception {

             // key 로드

             System.out.println("Loading the key.");

             Key key = loadKey(passowrd);

             System.out.println("Loaded the key.");

            

             // 사이퍼 생성

             Cipher cipher = Cipher.getInstance("Rijndael/CBC/PKCS5Padding");

             System.out.println("Initializing SecureRandom...");

             // SecureRandom 생성하여 IV 초기화

             SecureRandom random = new SecureRandom();

             byte[] iv = new byte[16];

             random.nextBytes(iv);

             // 입력 파일과 출력 파일의 stream 연다

             FileInputStream fis = new FileInputStream(fileInput);

             FileOutputStream fos = new FileOutputStream(fileOutput);

            

             // 먼저 출력파일에 iv 쓴다.

             fos.write(iv);

             // IvParameterSpec 생성하고 사이퍼를 생성 초기화한다.

             IvParameterSpec spec = new IvParameterSpec(iv);

             System.out.println("Initializing the cipher...");

             cipher.init(Cipher.ENCRYPT_MODE, key, spec);

             // 출력 스트림과 사이퍼를 인자로 사이퍼 스트림을 생성한다.

             CipherOutputStream cos = new CipherOutputStream(fos, cipher);

            

             System.out.println("Encrypting the file");

             // 입력 파일로부터 읽어들여 사이퍼 스트림에다 쓴다.

             int theByte = 0;

             while((theByte = fis.read()) != -1)

                    cos.write(theByte);

             // 스트림을 모두 닫아준다.

             fis.close();

             cos.close();

       }

 

     decrypt() : 사이퍼 스트림을 이용하여 복호화 수행

private static void decrypt(char[] password, String fileInput, String fileOutput) throws Exception {

             // key 로드

             System.out.println("Loading the key");

             Key key = loadKey(password);

             System.out.println("Loaded the key.");

             // 사이퍼 생성

             Cipher cipher = Cipher.getInstance("Rijndael/CBC/PKCS5Padding");

             // 입력 파일과 출력 파일의 stream 연다

             FileInputStream fis = new FileInputStream(fileInput);

             FileOutputStream fos = new FileOutputStream(fileOutput);

             // 입력파일에서 초기화 벡터 16byte 읽어들인다.

             // Base64인코딩을 하지 않았으므로 길이는 그대로다.

             byte[] iv = new byte[16];

             fis.read(iv);

             // IvParameterSpec 생성 사이퍼 초기화

             IvParameterSpec spec = new IvParameterSpec(iv);

             System.out.println("Initializing the cipher...");

             cipher.init(Cipher.DECRYPT_MODE, key, spec);

             // 입력파일과 사이퍼를 인자로 사이퍼 스트림을 생성한다.

             CipherInputStream cis = new CipherInputStream(fis, cipher);

             System.out.println("Decrypting the file...");

             // 입력파일로 부터 읽어들여 출력파일에 쓴다.

             int theByte = 0;

             while((theByte = cis.read()) != -1)

                    fos.write(theByte);

             // 스트림을 모두 닫아준다.

             cis.close();

             fos.close();

       }

 

     봉인 객체

가)    사이퍼 또는 키로 객체를 암호화할 수 있는 방법을 제공

나)    암호화 객체 또는 봉인 객체라 불림

다)   

import java.io.IOException;

import java.security.InvalidKeyException;

import java.security.Key;

import java.security.NoSuchAlgorithmException;

 

import javax.crypto.BadPaddingException;

import javax.crypto.Cipher;

import javax.crypto.IllegalBlockSizeException;

import javax.crypto.KeyGenerator;

import javax.crypto.NoSuchPaddingException;

import javax.crypto.SealedObject;

 

public class SealedObjectExample {

 

       public static void main(String[] args) {

             String creatiCard = "1234567890";

            

             try {

                    KeyGenerator keyGenerator = KeyGenerator.getInstance("DESede");

                   

                    Key key = keyGenerator.generateKey();

                    Cipher cipher = Cipher.getInstance("DESede");

                    cipher.init(Cipher.ENCRYPT_MODE, key);

                   

                    System.out.println("Encrypting the Object");

                    SealedObject so = new SealedObject(creatiCard, cipher);

                    System.out.println("Decrypting the Object");

                    //String decryptedCrediteCard = (String) so.getObject(key);

                   

                    cipher.init(Cipher.DECRYPT_MODE, key);

                    String decryptedCrediteCard = (String) so.getObject(cipher);

                    System.out.println("Credit card Number : " + decryptedCrediteCard);

                   

             } catch (NoSuchAlgorithmException e) {

                    // TODO Auto-generated catch block

                    e.printStackTrace();

             } catch (NoSuchPaddingException e) {

                    // TODO Auto-generated catch block

                    e.printStackTrace();

             } catch (InvalidKeyException e) {

                    // TODO Auto-generated catch block

                    e.printStackTrace();

             } catch (IllegalBlockSizeException e) {

                    // TODO Auto-generated catch block

                    e.printStackTrace();

             } catch (IOException e) {

                    // TODO Auto-generated catch block

                    e.printStackTrace();

             } catch (ClassNotFoundException e) {

                    // TODO Auto-generated catch block

                    e.printStackTrace();

             } catch (BadPaddingException e) {

                    // TODO Auto-generated catch block

                    e.printStackTrace();

             }

 

       }

 

}

 

 

 

'UP! > Crypto' 카테고리의 다른 글

MessageDigest  (0) 2015.11.05
Session key  (0) 2015.11.05
패스워드 기반 암호화  (0) 2015.11.05
JCA, JCE  (0) 2015.11.05
인증, 전자서명  (0) 2015.11.05
Posted by 으랏차
,

패스워드 기반 암호화(PBE : Password-Based Encryption)

 

1  설명

   가) 패스워드를 키로 사용하는 방법

   나) 키 스페이스가 TripleDES나 Blowfish보다 훨씬 작아 안전하지 못함

   다) 사전공격에 취약함

   라) 해쉬와 일반적인 대칭 암호화를 사용함

   마) 미리 패스워드 해쉬 목록을 만들어 놓고 복호화 할 수 있음

   바) salting와 iteration counts를 도입하여 보안성 강화

 

2  방법

   가) 패스워를 MD5로 해쉬를 구한 다음 이 값을 암호화 키 값으로 사용한다. 

 

3  Salt

   가) 패스워드를 해쉬하기 전에 랜덤값을 추가하여 키 스페이스를 늘린다.

   나) salt는 암호문과 함께 저장된다.

   다) 복호화할 때는 암호화된 데이터로부터 salt를 분리해야 한다.

   라) salt는 매번 다르게 생성되며 동일한 문장에 대해서도 다른 값이 생성된다.

 

4  Iteration count

   가) 해쉬의 횟수를 의미함

   나) Iteration count가 1000이면 1000번을 해쉬한다는 의미

 

5  암호화/복호화 방법

   가) 패스워드와 salt를 생성하여 보통문서를 암호화하고 생성된salt를 암호문서 앞에 붙인다.

 

   나) 암호화된 데이타를 salt와 암호문서로 나누어 패스워드와 salt로 암호 문서를 복호화 한다.

 

 

6  주요 클래스 사용법

   가) PBEKeySpec : SecretKeyFactory의 인스턴스를 이용해서 패스워드기반 키를 생성한다.

 char[] password = “iloveyou”.toCharArry();

PBEKeySpec keySpec = new PBEKeySpec(password)

 

   나) SecretKeyFactory : PBEKeySpec을 실제 키로 사용하기 위해 generateSecret()를 실행시켜 줘야 한다.

 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(“PBEWithShAAndTwoFish-CBC”);

SecretKey theKey = keyFactory.generateSecret(keySpec);

 

   다) PBEParameterSpec : salt와 interation count를 위한 레퍼런스

 PBEParameterSpec paramSpec = new PBEParameterSpec(salt, iterations);

Cipher cipher = Cipher.getInstance(“PBEWithShAAndTwoFish-CBC”);

Cipher.init(Cipher.ENCRYPT_MODE, theKey, paramSpec);

 

7  PBE 알고리즘

   가) PBEWithMD5AndDES 

   나) PBEWithSHAAndBlowfish

   다) PBEWithSHAAnd128BitRC4

   라) PBEWithSHAAndDEA-CBC

   마) PBEWithSHAAnd3-KeyTripleDES-CBC

 

8  PBE 예제

 

 import java.util.Random;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;

import com.Ostermiller.util.Base64;

public class PBE {

 private static int ITERATIONS = 1000;
 
 public static void main(String[] args) {
  char[] password = "iloveyou".toCharArray();
  String text = "i love you! but you don't. i sad b.b";
  String output = null;
  
  System.out.println("평문 : " + text);
  try {
   output = encrypt(password, text);
   System.out.println("암호문 : " + output);
   System.out.println("복호문 : " + decrypt(password, output));
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
 
 private static String encrypt(char[] password, String plaintext) throws Exception {
  // 1. salt를 8byte 랜덤 생성
  byte[] salt = new byte[8];
  Random random = new Random();
  random.nextBytes(salt);
  
  // 2. 패스워드를 이용하여 PBEKeySpec 생성
  PBEKeySpec keySpec = new PBEKeySpec(password);
  SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithSHAAndTwoFish-CBC");
  
  // 3. 비밀키 생성
  SecretKey key = keyFactory.generateSecret(keySpec);
  
  // 4. salt, iteration count를 위한 PBEParameterSpec 생성
  PBEParameterSpec paramSpec = new PBEParameterSpec(salt, ITERATIONS);
  
  // 5. Cipher 생성 및 초기화
  Cipher cipher = Cipher.getInstance("PBEWithSHAAndTwoFish-CBC");
  cipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
  
  // 6. 암호문 생성
  byte[] ciphertext = cipher.doFinal(plaintext.getBytes());
  
  // 7. salt와 암호문을 Base64 인코딩 후 결합하여 최종 결과물 생성
  String saltString = Base64.encodeToString(salt);
  String ciphertextString = Base64.encodeToString(ciphertext);
  
  return saltString+ciphertextString;
  
 }
 
 private static String decrypt(char[] password, String text) throws Exception {
  
  // 1. 8byte를 Base64인코딩하면 12byte가 생성된다.
  String salt = text.substring(0, 12);
  String ciphertext = text.substring(12, text.length());
  
  // 2. salt와 암호문을 Base64 디코딩하여 원래 상태로 되돌린다.
  byte[] saltArry = Base64.decodeToBytes(salt);
  byte[] ciphertextArry = Base64.decodeToBytes(ciphertext);
  
  // 3. 패스워드를 이용하여 PBEKeySpec 생성
  PBEKeySpec keySpec = new PBEKeySpec(password);
  SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithSHAAndTwoFish-CBC");
  
  // 4. 비밀 키 생성
  SecretKey key = keyFactory.generateSecret(keySpec);
  
  // 5. salt, iteration count를 위한 PBEParameterSpec 생성
  PBEParameterSpec paramSpec = new PBEParameterSpec(saltArry, ITERATIONS);
  
  // 6. Cipher 생성 및 초기화
  Cipher cipher = Cipher.getInstance("PBEWithSHAAndTwoFish-CBC");
  cipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
  
  // 7. 평문생성
  byte[] plaintextArry = cipher.doFinal(ciphertextArry);
  
  return new String(plaintextArry);
 }

}

 

 

'UP! > Crypto' 카테고리의 다른 글

Session key  (0) 2015.11.05
대칭키 암호화  (0) 2015.11.05
JCA, JCE  (0) 2015.11.05
인증, 전자서명  (0) 2015.11.05
대칭 비대칭 암호화  (0) 2015.11.05
Posted by 으랏차
,