Skip to content

Commit

Permalink
APIヘルパーの暗号化アルゴリズムをAESに変更
Browse files Browse the repository at this point in the history
  • Loading branch information
y-yamamoto-yworks committed Jan 30, 2022
1 parent 50fd36f commit 5e4c61c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
29 changes: 26 additions & 3 deletions src/vacancy_mgr/api/api_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from django.utils.http import urlsafe_base64_encode, urlsafe_base64_decode
from django.utils.encoding import force_bytes, force_text
from django.utils import timezone
from Crypto.Cipher import DES3
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad
from lib.convert import *
from lib.functions import *
Expand All @@ -21,7 +21,7 @@ class ApiHelper:
def get_key():
"""APIキーの取得"""
company = Company.objects.get(pk=settings.COMPANY_ID)
return ApiHelper.get_3des_encrypt(company.internal_api_key, company.api_key)
return ApiHelper.get_aes_encrypt(company.internal_api_key, company.api_key)

@staticmethod
def check_key(key: str):
Expand All @@ -30,7 +30,7 @@ def check_key(key: str):
company = Company.objects.get(pk=settings.COMPANY_ID)

try:
key = ApiHelper.get_3des_decrypt(key, company.api_key)
key = ApiHelper.get_aes_decrypt(key, company.api_key)
except:
key = None

Expand All @@ -39,6 +39,27 @@ def check_key(key: str):

return ans

@staticmethod
def get_aes_encrypt(target, crypt_key):
key = crypt_key.lower()[:16].encode("utf-8")
iv = crypt_key.lower()[-16:].encode("utf-8")
cipher = AES.new(key, AES.MODE_CBC, iv)
data = pad(str(target).encode("utf-8"), AES.block_size)
cipher_text = cipher.encrypt(data)
return cipher_text.hex()

@staticmethod
def get_aes_decrypt(target, crypt_key):
cipher_text = bytes.fromhex(target)
key = crypt_key.lower()[:16].encode("utf-8")
iv = crypt_key.lower()[-16:].encode("utf-8")
cipher = AES.new(key, AES.MODE_CBC, iv)
plain_text = unpad(cipher.decrypt(cipher_text), AES.block_size)
return plain_text.decode()

"""
暗号化アルゴリズムを3DESからAESに変更
@staticmethod
def get_3des_encrypt(target, crypt_key):
key = crypt_key.lower()[:24].encode("utf-8")
Expand All @@ -56,3 +77,5 @@ def get_3des_decrypt(target, crypt_key):
cipher = DES3.new(key, DES3.MODE_CBC, iv)
plain_text = unpad(cipher.decrypt(cipher_text), DES3.block_size)
return plain_text.decode()
"""
6 changes: 3 additions & 3 deletions src/vacancy_mgr/api/tests/test_api_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ def setUp(self):
def test_get_key(self):
self.assertEqual(
ApiHelper.get_key(),
'd3f001762f51e18a0241bde03544d91cdcf0efb154b792d739ccfa36e1138577520af5a81240ceca'
'f7e815ad8b88969d48a2e43f81e0b1aee28523f9f1330e345cc172747cc811d02ec6531855dece38dbe3692104c34c67'
)

def test_check_key(self):
self.assertTrue(ApiHelper.check_key(
'd3f001762f51e18a0241bde03544d91cdcf0efb154b792d739ccfa36e1138577520af5a81240ceca'
'f7e815ad8b88969d48a2e43f81e0b1aee28523f9f1330e345cc172747cc811d02ec6531855dece38dbe3692104c34c67'
))
self.assertFalse(ApiHelper.check_key(
'54b792d739ccfa36e1138577520af5a81240cecad3f001762f51e18a0241bde03544d91cdcf0efb1'
'5cc172747cc811d02ec6531855dece38dbe3692104c34c67f7e815ad8b88969d48a2e43f81e0b1aee28523f9f1330e34'
))

0 comments on commit 5e4c61c

Please sign in to comment.