'PBE Password Based Encryption XML 암호화 Java 자바'에 해당되는 글 1건


출처: http://www.exampledepot.com/egs/javax.crypto/DesFile.html

두 프로그램간의 암호화된 XML 파일교환을 위하여 출처의 파일(DES기반)을 PBE기반으로 바꾸었다.

사용법은 보내는 쪽과 받는 쪽, 그리고 암호화 클래스(DesEncrypter)로 구성되어 있다.

이때 XML파일의 인코딩은 UTF-8을 사용한다.


보내는 쪽 (String을 파일에 암호화 시켜 저장) 
         String FILENAME = "test.xml";
         String str2Encryption = "aaaa";

         DesEncrypter encrypter = DesEncrypter.getInstance();
         
         ByteArrayInputStream in = new ByteArrayInputStream(str4Encryption .getBytes());
         
         encrypter.encrypt(in, new FileOutputStream(FILENAME));
            
         in.close();
받는 쪽 (암호화 된 파일을 풀어서 스트링으로 받기)
         String FILENAME = "test.xml";

         DesEncrypter decrypter = DesEncrypter.getInstance();
         
         ByteArrayOutputStream out = new ByteArrayOutputStream();

         decrypter.decrypt(new FileInputStream(FILENAME),out);
         
         String result = new String(out.toByteArray());

         out.close();     

PBE기반 암호화/복호화 클래스

// Singletone
public class DesEncrypter
{
 private static DesEncrypter INSTANCE;
 
 private final static String ENCRYPTION_SALT = "abcdefgh"; // must be 8 bytes
 private final static String ENCRYPTION_PASSWORD = "password";
 private final static String ENCRYPTION_ALGORITHM = "PBEWithMD5AndDES";
 private final static int  ENCRYPTION_ITERATION = 20;

    private Cipher ecipher;
    private Cipher dcipher;
 
 public static DesEncrypter getInstance()
 {
  if (INSTANCE == null)
  {
   INSTANCE = new DesEncrypter();
  }
  return INSTANCE;
 }
   
    private DesEncrypter() {
        // Create an 8-byte initialization vector
  PBEKeySpec    pbeKeySpec;
  PBEParameterSpec  pbeParameterSpec;
  SecretKeyFactory  secretKeyFactory;
  SecretKey    secretKey;

        try {
          
   pbeParameterSpec = new PBEParameterSpec(ENCRYPTION_SALT.getBytes(), ENCRYPTION_ITERATION);
   
   pbeKeySpec = new PBEKeySpec(ENCRYPTION_PASSWORD.toCharArray());

   secretKeyFactory = SecretKeyFactory.getInstance(ENCRYPTION_ALGORITHM);
   
   secretKey = secretKeyFactory.generateSecret(pbeKeySpec);
   
   dcipher = Cipher.getInstance(ENCRYPTION_ALGORITHM);
   ecipher = Cipher.getInstance(ENCRYPTION_ALGORITHM);
   
   dcipher.init(Cipher.DECRYPT_MODE, secretKey, pbeParameterSpec);
   ecipher.init(Cipher.ENCRYPT_MODE, secretKey, pbeParameterSpec);
           
        } catch (java.security.InvalidAlgorithmParameterException e) {
        } catch (javax.crypto.NoSuchPaddingException e) {
        } catch (java.security.NoSuchAlgorithmException e) {
        } catch (java.security.InvalidKeyException e) {
        } catch (InvalidKeySpecException e) {
  }
    }

    // Buffer used to transport the bytes from one stream to another
    byte[] buf = new byte[1024];

    public void encrypt(InputStream in, OutputStream out) {
        try {
            // Bytes written to out will be encrypted
            out = new CipherOutputStream(out, ecipher);

            // Read in the cleartext bytes and write to out to encrypt
            int numRead = 0;
            while ((numRead = in.read(buf)) >= 0) {
                out.write(buf, 0, numRead);
            }
            out.close();
        } catch (java.io.IOException e) {
        }
    }

    public void decrypt(InputStream in, OutputStream out) {
        try {
            // Bytes read from in will be decrypted
            in = new CipherInputStream(in, dcipher);

            // Read in the decrypted bytes and write the cleartext to out
            int numRead = 0;
            while ((numRead = in.read(buf)) >= 0) {
                out.write(buf, 0, numRead);
            }
            //out.close();
        } catch (java.io.IOException e) {
        }
    }
}



Posted by zennken
1

zennken

달력