'Crypto'에 해당되는 글 2건

  1. 2008.06.05 원문과 암호화된 길이가 같은 블록암호 by 파이팅야 2
  2. 2008.06.04 복호화 가능한 암호화 by 파이팅야 2

ㅇ. 소스코드
public
class CFB_OFB {

public static void encSSN(String input)

         throws Exception {

         // key값 구함

         KeyGenerator keygenerator = KeyGenerator.getInstance("DES");

         keygenerator.init(new SecureRandom());

         SecretKey key = keygenerator.generateKey();

 

         // key값으로 암호화함(CFB or OFB)

         Cipher cipher = Cipher.getInstance("DES/CFB/NoPadding");

         //Cipher cipher = Cipher.getInstance("DES/OFB/NoPadding");

         cipher.init(Cipher.ENCRYPT_MODE, key);

         byte[] plainTextBytes = input.getBytes("UTF-8");

         byte[] cipherTextBytes= cipher.doFinal(plainTextBytes);

 

         System.out.println("plainTextString[" + input + "]");

         System.out.println("plainTextBytes.length[" + plainTextBytes.length + "]");

         System.out.println("cipherTextBytes.length[" + cipherTextBytes.length + "]");

}

public static void main(String[] args) {

         // 파라미터 확인

         if (args.length != 1) {

                  System.out.println("해쉬 할 내용을 입력해 주세요. ex)plainText hoho");

                  return;

         }

         String arg = args[0];

         try {

                  encSSN(arg);

         } catch (Exception exception) {

                  exception.printStackTrace();

         }

}

}

ㅇ. 실행화면

사용자 삽입 이미지

Posted by 파이팅야
,

ㅇ. 소스코드
public
class CipherTest {

public static void encrypt(String filePath)

         throws Exception {

         int availableBytes = -1;

         byte[] plainTextBytes              = null;

         byte[] cipherTextBytes             = null;

         byte[] keyBytes                    = null;

         byte[] keyAndCipherTextBytes       = null;

         FileInputStream inputStream         = null;

         FileOutputStream outputStream       = null;

         inputStream = new FileInputStream(filePath);

         // 파일에 내용이 있는지 확인

         availableBytes = inputStream.available();

         if (availableBytes <= 0) {

                  System.out.println("해당파일에 처리할 내용이 없습니다.");

                  return ;

         }

         // plainText byte[]로 읽음

         plainTextBytes = new byte[availableBytes];

         inputStream.read(plainTextBytes);

         inputStream.close();

         // key값 구함

         KeyGenerator keygenerator = KeyGenerator.getInstance("DES");

         keygenerator.init(new SecureRandom());

         SecretKey key = keygenerator.generateKey();

         // key값으로 암호화함

         Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");

         cipher.init(Cipher.ENCRYPT_MODE, key);

         // 'key + 암호화한값'을 파일에 저장

         cipherTextBytes                    = cipher.doFinal(plainTextBytes);

         keyBytes                           = key.getEncoded();

         keyAndCipherTextBytes      = new byte[keyBytes.length + cipherTextBytes.length];

         System.arraycopy(keyBytes, 0,

                           keyAndCipherTextBytes, 0, keyBytes.length);

         System.arraycopy(cipherTextBytes, 0,

                           keyAndCipherTextBytes, keyBytes.length, cipherTextBytes.length);

 

         outputStream = new FileOutputStream(filePath);

         outputStream.write(keyAndCipherTextBytes);

         outputStream.close();

}

public static void decrypt(String filePath)

         throws Exception {

        

         int availableBytes = -1;

         SecretKey key = null;

         byte[] plainTextBytes              = null;

         byte[] keyBytes                    = new byte[8];

         byte[] cipherTextBytes             = null;

         FileInputStream inputStream        = null;

         FileOutputStream outputStream      = null;

         inputStream = new FileInputStream(filePath);

         // 파일에 내용이 있는지 확인

         availableBytes = inputStream.available();

         if (availableBytes <= 0) {

                  System.out.println("해당파일에 처리할 내용이 없습니다.");

                  return ;

         }

         // key값과 암호화값의 byte[]를 구함

         cipherTextBytes = new byte[availableBytes - keyBytes.length];

         inputStream.read(keyBytes);

         inputStream.read(cipherTextBytes);

         inputStream.close();

         // key값 구함

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

         // key값으로 복호화함

         Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");

         cipher.init(Cipher.DECRYPT_MODE, key);

         plainTextBytes = cipher.doFinal(cipherTextBytes);

         // 복호화한 값 파일에 저장

         outputStream = new FileOutputStream(filePath);

         outputStream.write(plainTextBytes);

         outputStream.close();

}

public static void main(String[] args)

         throws UnsupportedEncodingException {

         // 파라미터 확인

         if (args.length != 2

                           || (!args[1].equals("-e") && !args[1].equals("-d")) ) {

                  System.out.print("파일명과 암호화(-e)와 복호화(-d) 여부를  입력해 주세요. ");

                  System.out.println("ex)c:\\temp\\a.txt -e");

                  return;

         }

         // 파일이 있는지 확인

         String filePath            = args[0];

         String executeOption= args[1];

         if (executeOption.equals("-e")) {

                  try {

                           CipherTest.encrypt(filePath);

                  } catch (Exception exception) {

                           exception.printStackTrace();

                  }

         }else if(executeOption.equals("-d")) {

                  try {

                           CipherTest.decrypt(filePath);

                  } catch (Exception exception) {

                           exception.printStackTrace();

                  }

         }

}


ㅇ. 실행화면

상단은 encoding전, 하단은 encoding후

123

Posted by 파이팅야
,