본문으로 건너뛰기

"openssl" 태그로 연결된 2개 게시물개의 게시물이 있습니다.

모든 태그 보기

PHP 7.1에서 mcrypt 대체하기

· 약 3분
  • 암호화 함수인 mcrypt 가 PHP 7.1 버전부터 Deprecated 되었다.
  • 왜 사라졌는지는 링크에 자세하게 나와있다.

기존 소스

  • PHP 구버전에서는 mcryptMCRYPT_RIJNDAEL_128 알고리즘을 통해 AES128 이 구현되어 있을 것이다.

  • MCRYPT_RIJNDAEL_128AES 128동일하다.

  • 타 언어와는 조금 다른데, 호환을 위해선 pkcs5 padding 으로 변경해주는 작업이 필요하다.

  • php mcrypt function 의 기본 패딩은 zeros padding

<?php
// 이런 식이거나
$cipherText = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, pkcs5_pad($plainText), MCRYPT_MODE_CBC, $iv);

// 이런 식일 것
$td = mcrypt_module_open("rijndael-128", "", "cbc", "");
@mcrypt_generic_init($td, $key, $iv);
$cipherText = @mcrypt_generic($td, pkcs5_pad($plainText));
mcrypt_generic_deinit($td);
mcrypt_module_close($td);

function pkcs5_pad($text, $blockSize = 16) {
$pad = $blockSize - (strlen($text) % $blockSize);
return $text . str_repeat(chr($pad), $pad);
}

대안

  • PHP 5.3 부터 사용 가능한 openssl_encrypt 함수를 쓰면 된다.
  • openssl 확장 모듈이 설치되어야한다.

OPENSSL

Encrypt

<?php
$cipherText = openssl_encrypt($plainText, 'AES-128-CBC', $key, OPENSSL_RAW_DATA, $iv);
// decrypt에서 오류가 발생해 PKCS5로 패딩을 맞춰주고 보내야한다면.
$cipherText = openssl_encrypt(pkcs5_pad($plainText), 'AES-128-CBC', $key, OPENSSL_RAW_DATA, $iv);

Decrypt

<?php
$plainText = openssl_decrypt($cipherText, 'AES-128-CBC', $key, OPENSSL_RAW_DATA, $iv);
// unpad가 필요하다면
$plainText = pkcs5_unpad(openssl_decrypt($cipherText, 'AES-128-CBC', $key, OPENSSL_RAW_DATA, $iv));

function pkcs5_unpad($text) {
$pad = ord($text{strlen($text)-1});
if ($pad > strlen($text)) {
return $text;
}
if (!strspn($text, chr($pad), strlen($text) - $pad)) {
return $text;
}
return substr($text, 0, -1 * $pad);
}

PKCS5 vs PKCS7

  • openssl_encrypt의 default padding 은 PKCS7 padding 인데, 어떻게 호환이 되는 것일까?
  • 여기서 해답을 찾았다.

The difference between the PKCS#5 and PKCS#7 padding mechanisms is the block size; PKCS#5 padding is defined for 8-byte block sizes, PKCS#7 padding would work for any block size from 1 to 255 bytes. So fundamentally PKCS#5 padding is a subset of PKCS#7 padding for 8 byte block sizes. so, data encrypted with PKCS#5 is able to decrypt with PKCS#7, but data encrypted with PKCS#7 may not be able to decrypt with PKCS#5.

triple des

  • des-ede3-cbc가 triple des 알고리즘이다.
<?php
// encrypt
openssl_encrypt($text, "des-ede3-cbc", $key, OPENSSL_RAW_DATA, $iv);

// decrypt
openssl_decrypt($cipherText, "des-ede3-cbc", $key, OPENSSL_RAW_DATA, $iv);

패키지

  • 이 모든 걸 커버하는 라이브러리를 사용하자. phpseclib/mcrypt_compat
  • phpseclib 는 laravel/passport 에서도 사용되었다.

여담

카카오페이가 PHP 5 버전만 지원해 문의해봤지만 계획이 없어 이번 기회에 모듈을 다 뜯어봤는데 MCRYPT 만 만져주면 정상적으로 결제가 되었다.

곧 이 짓을 안해도 될 듯하다. KakaoPay 종료

이러다가 다시 선형대수학 공부하겠어.

로컬 웹서버 돌리기 - 4. HTTPS OpenSSL 설정

· 약 3분

3. Virtual Host 설정에서 이어집니다. 로컬에서 HTTPS 통신으로 프로젝트에 접근하시고 싶지 않으시다면 3 장으로 웹서버 구동은 완료됩니다.

모듈 활성화

Apache24\conf\httpd.conf에서 ssl_module 과 socache_shmcb_module 의 주석을 해제한다. image from hexo

설정 활성화

Include conf/extra/httpd-ssl.conf의 주석을 해제한다. image from hexo

SSL 설정 변경

디렉토리 생성

Apache24\conf 경로에서 ssl폴더를 만든 뒤 conf/openssl.cnf 파일을 ssl 폴더로 복사한다.

키 생성

ssl 폴더로 들어와 cmd 창을 연 뒤 키를 생성한다.

$ openssl genrsa -out domain.key 1024
$ openssl req -new -config openssl.cnf -days 365 -key domain.key -out domain.csr

위 명령어를 입력하면 입력 폼 형식이 나오는데, 주제에 맞게 입력하면 된다. (KR, Seoul 등)

생성된 domain.key, domain.csr 파일을 확인한 뒤 아래 명령어를 입력한다.

$ openssl x509 -in domain.csr -out domain.crt -req -signkey domain.key -days 365

완성된 파일 구조의 형태는 다음과 같아야한다. image from hexo

SSL 연동

httpd-ssl.conf를 열어 키 파일을 연동해준다. image from hexo 그 밖에 다른 경로로 연결된 설정이 있다면 바로 잡아주면 된다.

HTTPS 접속

Apache restart 후 접속해보자. image from hexo

여담

오류 발생시

오류가 발생할 경우 Apache24\logs\error.log 파일을 확인해가며 진행하면 된다.

Virtual Host 설정

Virtual Host 에 직접 key 파일을 지정할 수도 있다.

<VirtualHost *:443>
SSLEngine on
SSLCertificateFile "d:/Apache24/conf/ssl/domain.crt"
SSLCertificateKeyFile "d:/Apache24/conf/ssl/domain.key"

DocumentRoot "D:/workspace/test"
ServerName local.test.com
</VirtualHost>

👏👏👏 이로서 로컬 HTTPS 서버를 가지게 되었습니다. 다음 장에서는 서버 통신을 위해 CURL 설정을 해보겠습니다.