ç°ãªãè¨èªéã§ã®æå·åã¨å¾©å·
ä»äºã§ãã¼ã¿ãæå·åãã¦ä¿åããå¿
è¦ãåºã¦ããã®ã§è²ã
調ã¹ã¦ã¿ãã
ã¡ã¤ã³ã®ã·ã¹ãã ã¯PHPã§ä½ã£ã¦ãããã ãã©ãJavaãªã©ã絡ãã§ããã®ã§ããäºããå¦çã§ããæå·æ¹å¼ã§ãªããã°ãããªãã
ä»æ§ããæ確ã«ãã¦ããã°ãªã¬ãªã¬ã¢ã«ã´ãªãºã ã§ããããã ãã©ãä»åã¯ããªãéè¦ãªãã¼ã¿ãæ±ãã®ã§ä¸éã§ãããªãã«ä½¿ããã¦ããæå·æ¹å¼ã使ããã¨ã«ããã
æå·ã®ç¨®é¡
èªåãæå·ã«ãããªè©³ããããã§ã¯ãªããã©ãããã¼ã¿ããã¹ã¯ã¼ãã§æå·åãããã§ããï¼ããããã«ããæã£ã¦ãªã人ã¯ãã£ã¨å¤§å¤ãªæããããã
ä»ã®è©±ã§ç»å ´ããã®ã¯
- æå·åããããã¼ã¿
- æå·ã®ç¨®é¡ï¼ã¢ã«ã´ãªãºã ï¼
- ãã¹ã¯ã¼ãï¼æå·å¦çã«ä½¿ããã¼ï¼
ã®3ã¤ã ãã©ããããããããã¨ãã¦ããããã¯æå·ã§ã¯
- æå·åããããã¼ã¿
- æå·ã®ç¨®é¡ï¼ã¢ã«ã´ãªãºã ï¼
- æå·å¦çã«ä½¿ããã¼ï¼ãã¹ã¯ã¼ãã¨ããããã¤ãåï¼
- æå·å©ç¨ã¢ã¼ãï¼ãããã¯å¦çã®ç¨®é¡ï¼
- åæåãã¯ãã«ï¼IV=Initialization Vectorï¼
- ããã£ã³ã°æ¹å¼
ã¨ã6種é¡ãã®è¦ç´ ã絡ãã§ããããªã«ããããã©ããããï¼
æå·ã®ç¨®é¡ã¯ãDESã¨ãAESã¨ãBlowfishã¨ããããã¤ã
ãã¼ã¯ããããã¯æå·å¦çã«ä½¿ãå
±ééµã
æå·å©ç¨ã¢ã¼ãã¯ããã®ãããã¯æå·ãæ±ããããã¯é·ãããé·ããã¼ã¿ãæå·åããã¨ãã«ã©ããã£ã¦å¦çãããã¨ããæ¹å¼ã
åæåãã¯ãã«ã¯ããããã¯*1ãå¦çããæã«åãå¹³æãåãéµã§ãç°ãªãæå·æã«ããããã«å¿
è¦ãªãã¼ã¿ãã¾ããä¹±æ°åæåæã®seedã¿ãããªããï¼
ããã£ã³ã°æ¹å¼ã¯ããããã¯ã®ãµã¤ãºã«æºããªããã¼ã¿ãå¦çããæã«ãããã¯ãµã¤ãºã¾ã§ãã¼ã¿ãè©°ããæ¹å¼ã
PHPã®mcrypt
PHPã§ã¯MCryptããµãã¼ãããã¦ãã*2ã®ã§ããã使ããã¨ã«ããã
MCryptã使ãã¨Rijndael(AES)ã¨ãBlowfishã¨ãDESã¨ã3DESã¨ãâ¦â¦ã¾ãã¨ã«ãã主è¦ãªæå·ã¢ã«ã´ãªãºã ã§æå·å¦çããããã¨ãã§ããã
mcryptã®ä¸è¬çãªä½¿ãæ¹ã¯
mcrypt_module_open()
ã§ã¢ã¸ã¥ã¼ã«ããªã¼ãã³ããmcrypt_generic_init()
ã§åæåããmcrypt_generic()
ã§æå·åããããmdecrypt_generic()
*3ã§å¾©å·ãããããmcrypt_deinit()
ã§å¾å§æ«ããmcrypt_module_close()
ã§ã¢ã¸ã¥ã¼ã«ãã¯ãã¼ãºãã
ãããªæãã§çµæ§é¢åãããã*4
openã¨initã¨ããdeinitã¨closeã¨ãã²ã¨ã¤ã«ãªããªãã£ãã®ï¼ã¨ãæããªãã§ããªãã
é¢åãããã¨æããæ¹ã«ã¯é¢æ°ä¸çºã§å¦çã§ããæ¹æ³ãããã
mcrypt_encrypt()
ã§æå·åmcrypt_decrypt()
ã§å¾©å·
openãinitæã«æ¸¡ãã¦ãããã©ã¡ã¼ã¿ãå
¨ã¦æ¸¡ããã¨ã«ãªãã®ã§å¼æ°ãå¤ããªããããããªãä¸çºã§å¦çã§ããã
ããããã®æ¹æ³ã ã¨ç´°ããå¶å¾¡ãã§ããªãã£ããã¨ã©ã¼ãèµ·ããæã«ç´°ããæ
å ±ã¯åããªãã
ãã¯ããã¡ãã¨å¶å¾¡ãããæã«ã¯åè
ã®æ¹æ³ã使ããã¨ã«ãªãã
ã¨ããããã§ãããã使ãã°ä¸éä¸è¬ã§ä½¿ããã¦ããæå·å¦çã¯ã§ããããã«ãªããã®ã ãâ¦â¦
mcryptã®ããã£ã³ã°æ¹å¼
mcryptã¯ããã£ã³ã°æ¹å¼ãé¸ã¹ãªãã
ãããã¯é·ã«æºããªãå ´åã¯NULLãã¤ãã§åããããï¼ZeroBytePaddingï¼ã
ãããä¸è¬çãªããã£ã³ã°æ¹å¼ã¨éã£ã¦ãããã¯é·ã´ã£ãããªå ´åã¯ããã£ã³ã°ãããªãã®ã§å°ãã
ãã®æ¹å¼ã¯ã©ããä¸è¬çã§ã¯ãªãã¿ããâ¦â¦ã¨ãããä½ãèããã«åãã¦ãããã«ããæããªãã®ã§ããããåºæ¥ãªãããã¶ã復å·æã«å°ãã
0x00ã§åããããããã¨ãã£ã¦å¾©å·ããã¨ãã«æ«å°¾ã®0x00ãåãé¤ãã°ããããã§ã¯ãªãããã
å
ã
ã®ãã¼ã¿ã«0x00ãå«ã¾ãã¦ããã®ãããã£ã³ã°ãããã®ãå¤å¥ã§ããªãã®ã§ã
ä¸éã§æå·å¦çã®ãµã³ãã«ãæ¼ãã¨ãBase64ãserializeã§ä¸æ®µéãããã§ããæå·æãããããªãµã³ãã«ãããããåºã¦ãããã©ãããã ã¨ãµã¤ãºããµãããããã®ã§ããããåºæ¥ãªãã
ãããªãã¨ããããããªããã¼ã¿ã®å
é ãæ«å°¾ã«ãã¼ã¿é·ãåãããããã®ãã¨ããã¦ã»ããã
ãããããªãã¨ãããããããªãããä¸æ©é²ãã§PKCS#5 Paddingã¨ããããã£ã³ã°æ¹å¼ãèªåã§å®è£
ããã»ãããã£ã¨ãããç°¡åã ãã
PKCS#5 Padding
ããã£ã³ã°æ¹å¼ã¨ãä»ååãã¦ç¥ã£ãã®ã§èªåãããããã£ã¦ãªããã ãã©ãPKCS#5 Paddingã¯ãã¶ãæ¨æºçã«ä½¿ããã¦ããæ¹å¼ã ã¨æãã
Perlã®Crypt::CBCãªãã¦padding=standardã§PKCS#5 Paddingã ããã£ã¨ã¹ã¿ã³ãã¼ããªæ¹å¼ãªãã ãããããããã£ã¨ããï¼
ãã®æ¹å¼ã¯åãæ¨ã¦ãã¹ããµã¤ãºï¼ï¼åãããµã¤ãºï¼ã®å¤ã表ããã¤ãå¤ã§è¶³ããªãåãåããã¨ããæ¹å¼ã
ãããã¯é·ã8ãã¤ãã§ãã¼ã¿ãµã¤ãºã5ãã¤ããªã0x03ã§è¶³ããªã3ãã¤ãååããã
æ»ãæã¯æ«å°¾ã®1ãã¤ããè¦ãã°åãæ¨ã¦ãã¹ããµã¤ãºããããã
ã¡ãã£ã¨æ³¨æãå¿
è¦ãªã®ã¯ãã¼ã¿ããããã¯é·ã«ã´ã£ããã ã£ãå ´åããã®å ´åã¯1ãããã¯å丸ãã¨ããã£ã³ã°ã«ãªããã¤ã¾ã0x08ã8ã¤ä»ããæ»ãæã®ãã¨ãèããã°ãããããï¼
ã¤ã¡ã¼ã¸ããããªã人ã¯ãã¡ãã®ããã°ãè¦ã¦ãããã¨ä¸çºã§ãããã
http://wp.serpere.info/archives/432
PHPã§ãã®å¦çãããã³ã¼ãã¯McryptのUser Contributed Notesã«è¼ã£ã¦ããã®ã§ããã使ãã°ããã
ç°¡åã ããèªåã§ãå®è£
ã§ãããã©ãã
Javaã§ã®æå·å¦ç
ãã¦ãPHPã§ã®æå·åã¯ã§ããã®ã§æ¬¡ã«ãããJavaã§å¾©å·ãã¦ã¿ããã¨ããããã ãâ¦â¦Javaä¹ ãã¶ãããã¦ããããï½
Javaã§ã®æå·å¦çã¯java.securityããã±ã¼ã¸ã¨javax.cryptoããã±ã¼ã¸ããããä¸å¿ã«ãªã£ã¦ããããã ã
ãã¡ãã®æµãã¯ã ããããããªæãã
java.security.Key
ãç¨æããjavax.security.spec.AlgorithmParameterSpec
ãç¨æããjavax.crypto.Cipher.getInstance()
ããCipher.init()
ã§åæåããCipher#update()
ã¨ãCipher#doFinal()
ã§æå·åã»å¾©å·ãã
ä»åã¯ã¨ããããBlowfishã§ãéµãæ®éã«ãã¹ã¯ã¼ãã¿ããã«æååã§è©¦ãã¦ã¿ããã£ãã®ã§ãããªæãããªã
éµé·ã¨IVã®ãµã¤ãºã¯ã¢ã«ã´ãªãºã ã«ãã£ã¦éãã®ã§æ³¨æã
java.security.Key key = new javax.crypto.SecretKeySpec("password".getBytes(), "Blowfish"); java.security.spec.AlgorithmParameterSpec iv = new javax.crypto.spec.IvParameterSpec("_initiv_".getBytes());
ã§ããã¨ã¯Cipherãåæåãã¦update()
, update()
, doFinal()
!!
javax.crypto.Cipher cipher = javax.crypto.Cipher.getInstance("Blowfish/CBC/PKCS5Padding"); cipher.init(javax.crypto.Cipher.DECRYPT_MODE, key, iv); byte[] decrypted = cipher.doFinal(encrypted);
ã¾ãå®éã«ã¯ãããªãdoFinal()
ã ãã§çµäºãããã©ï½
è¦å´ããã¨ãã
ãã®ï¼ã
ã¾ãä½ãããæå·ã«å¯¾ããç¥èã絶対çã«ä¸è¶³ãã¦ãããã¨ï½
ãã®ï¼ã
PHPã ã¨ãã¼ã¨ããã£ã¦ããã ã®stringï¼ä¸èº«ã¯ãã¤ããªãããããªããã©ï¼ãªãã ãã©ãJavaã§å¦çãããã¨ãããDESKeySpecã¨ãPBEKeySpecã¨ããã¼ã®ç¨®é¡ãè²ã
ãã£ã¦ã©ãã使ãã°ããã®ãããããªãã£ããã¾ãBlowfishãªã®ã§DESã¯éãããªã¨ã¯æã£ããã©ã
çµå±MCryptã®ãµã¤ããè¦ããKey Typeã®ã¨ãããSecretã¨ãªã£ã¦ããã®ã§ãSecretKeySpecã使ãã°ããã®ããªï¼ã¨ã
ãã®ï¼ã
IVã®è¨å®ãæåã¯ã©ãããã°ããã®ãããããªãã£ãã
æå·ã¢ã«ã´ãªãºã ã«å¯¾ããè¨å®ã¯å
¨ã¦AlgorithmParametersã¨ããæ¬ãã«ãªã£ã¦ãã¦IVã¨ããååãã¾ã£ããåºã¦ããªãã®ã§ãIvParameterSpecã«ãã©ãçãã¾ã§ã ãã¶ããã£ãã
ãã®ï¼ã
æå·åããããã¼ã¿ã¯æ®éãã¤ããªãªãã ãã©ãä»åã¯16é²æ°æååã§ããã¨ããããã®ã§ãããã®ã¤ã³ã¿ã¼ãã§ã¤ã¹ã¨ãä½ãæ¹ãå´ã£ã¦è¦å´ããã
ãããããããªç°¡åãªãã¨ãåºæ¥ãªãã»ã©ã«Javaå¿ãã¦ãã®ãâ¦â¦ã¨è»½ãæ³£ããããªã£ãï½
ã¡ãªã¿ã«PHPã®å ´åã¯bin2hex($bin)
ã¨pack('H*', $hex)
ã§ãï½