메뉴 건너뛰기

컴' 게시판

본문시작

IT/개발
2021.01.22 09:34

자바스크립트 php 암호화

조회 수 4520 추천 수 0 댓글 0
<JAVASCRIPT>

class Encryption {

    get encryptMethod() {
        return 'AES-256-CBC';
    }

    get encryptMethodLength() {
        var encryptMethod = this.encryptMethod;
        var aesNumber = encryptMethod.match(/\d+/)[0];
        return parseInt(aesNumber);
    }

    get encryptKeySize() {
        var aesNumber = this.encryptMethodLength;
        return parseInt(aesNumber / 8);
    }

    encrypt(string, key) {
        var iv = CryptoJS.lib.WordArray.random(16);

        var salt = CryptoJS.lib.WordArray.random(256);
        var iterations = 999;
        var encryptMethodLength = (this.encryptMethodLength/4);
        var hashKey = CryptoJS.PBKDF2(key, salt, {'hasher': CryptoJS.algo.SHA512, 'keySize': (encryptMethodLength/8), 'iterations': iterations});

        var encrypted = CryptoJS.AES.encrypt(string, hashKey, {'mode': CryptoJS.mode.CBC, 'iv': iv});
        var encryptedString = CryptoJS.enc.Base64.stringify(encrypted.ciphertext);

        var output = {
            'ciphertext': encryptedString,
            'iv': CryptoJS.enc.Hex.stringify(iv),
            'salt': CryptoJS.enc.Hex.stringify(salt),
            'iterations': iterations
        };

        return CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(JSON.stringify(output)));
    }

    decrypt(encryptedString, key) {
        var json = JSON.parse(CryptoJS.enc.Utf8.stringify(CryptoJS.enc.Base64.parse(encryptedString)));

        var salt = CryptoJS.enc.Hex.parse(json.salt);
        var iv = CryptoJS.enc.Hex.parse(json.iv);

        var encrypted = json.ciphertext;

        var iterations = parseInt(json.iterations);
        if (iterations <= 0) {
            iterations = 999;
        }
        var encryptMethodLength = (this.encryptMethodLength/4);
        var hashKey = CryptoJS.PBKDF2(key, salt, {'hasher': CryptoJS.algo.SHA512, 'keySize': (encryptMethodLength/8), 'iterations': iterations});

        var decrypted = CryptoJS.AES.decrypt(encrypted, hashKey, {'mode': CryptoJS.mode.CBC, 'iv': iv});

        return decrypted.toString(CryptoJS.enc.Utf8);
    }

}

 

 

 

<PHP>

 

public function js_decrypt($encryptedString, $key = 'DsntechLogsaver')
{
   $json = json_decode(base64_decode($encryptedString), true);

   try {
      $salt = hex2bin($json["salt"]);
      $iv = hex2bin($json["iv"]);
   } catch (Exception $e) {
      return null;
   }

   $cipherText = base64_decode($json['ciphertext']);

   $iterations = (int)abs($json['iterations']);
   if ($iterations <= 0) {
      $iterations = 999;
   }
   $hashKey = hash_pbkdf2('sha512', $key, $salt, $iterations, ($this->encryptMethodLength() / 4));
   unset($iterations, $json, $salt);

   $decrypted= openssl_decrypt($cipherText , 'AES-256-CBC', hex2bin($hashKey), OPENSSL_RAW_DATA, $iv);
   unset($cipherText, $hashKey, $iv);

   return $decrypted;
}

public function encryptMethodLength(): int
{
   $number = filter_var('AES-256-CBC', FILTER_SANITIZE_NUMBER_INT);

   return (int)abs($number);
}

 


  1. 07
    Jun 2024
    12:45

    test

    Category개인자료 By꿈자 Reply0 Views7 Votes0 file
    Read More
  2. 21
    May 2023
    10:09

    여행

    Category개인자료 By꿈자 Reply0 Views0 Votes0 secret
    Read More
  3. 22
    Jan 2021
    09:34

    자바스크립트 php 암호화

    CategoryIT/개발 By꿈자 Reply0 Views4520 Votes0
    Read More
  4. 06
    Dec 2020
    20:16

    MAC Parallels 네트워크 연...

    CategoryIT/개발 By꿈자 Reply0 Views0 Votes0 secret
    Read More
  5. 26
    Nov 2020
    09:10

    워드프레스 보안 강화

    CategoryIT/개발 By꿈자 Reply0 Views0 Votes0 secret
    Read More
  6. 25
    Nov 2020
    10:00

    ubuntu 보안강화

    CategoryIT/개발 By꿈자 Reply0 Views0 Votes0 secret
    Read More
  7. 29
    Mar 2020
    17:40

    ES timeout 설정

    CategoryIT/개발 By꿈자 Reply0 Views0 Votes0 secret
    Read More
  8. 07
    May 2018
    10:30

    VR View 코드

    Category개인자료 By꿈자 Reply0 Views0 Votes0 secret
    Read More
  9. 27
    Apr 2018
    10:54

    워드프레스 쇼핑몰에 필요...

    CategoryIT/개발 By꿈자 Reply0 Views0 Votes0 secret
    Read More
  10. 17
    Apr 2018
    16:29

    Linux 파일 및 폴더 이벤트...

    CategoryIT/개발 By꿈자 Reply0 Views0 Votes0 secret
    Read More
Board Pagination Prev 1 2 3 4 5 ... 17 Next
/ 17