Maison développement back-end tutoriel php PHP加密3DES报错 Call to undefined function: mcrypt_module_open() 的解决办法

PHP加密3DES报错 Call to undefined function: mcrypt_module_open() 的解决办法

Jun 13, 2016 pm 12:29 PM
input mcrypt php str

PHP加密3DES报错 Call to undefined function: mcrypt_module_open() 的解决方法

我也是PHP新手,通过w3cschool了解了一下php基本原理之后就开写了。但仍是菜鸟。

先不管3DES加密的方法对不对,方法都是网上的,在运行的时候报了个错,把小弟整死了。找来找去终于自己摸出了方法。

<span style="color: #000000;">php</span><span style="color: #008000;">/*</span><span style="color: #008000;">** * PHP版3DES加解密类** 可与java的3DES(DESede)加密方式兼容** @Author: Luo Hui (farmer.luo at gmail.com)** @version: V0.1 2008.12.04*</span><span style="color: #008000;">*/</span><span style="color: #0000ff;">class</span><span style="color: #000000;"> Crypt3Des{        </span><span style="color: #0000ff;">public</span> <span style="color: #800080;">$key</span>    = "01234567890123456789012345678912"<span style="color: #000000;">;    </span><span style="color: #0000ff;">public</span> <span style="color: #800080;">$iv</span>    = "23456789"; <span style="color: #008000;">//</span><span style="color: #008000;">like java: private static byte[] myIV = { 50, 51, 52, 53, 54, 55, 56, 57 };        //加密</span>    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> encrypt(<span style="color: #800080;">$input</span><span style="color: #000000;">)    {        </span><span style="color: #800080;">$input</span> = <span style="color: #800080;">$this</span>->padding( <span style="color: #800080;">$input</span><span style="color: #000000;"> );        </span><span style="color: #800080;">$key</span> = <span style="color: #008080;">base64_decode</span>(<span style="color: #800080;">$this</span>-><span style="color: #008080;">key</span><span style="color: #000000;">);        </span><span style="color: #800080;">$td</span> = mcrypt_module_open( MCRYPT_3DES, '', MCRYPT_MODE_CBC, ''<span style="color: #000000;">);        </span><span style="color: #008000;">//</span><span style="color: #008000;">使用MCRYPT_3DES算法,cbc模式</span>        mcrypt_generic_init(<span style="color: #800080;">$td</span>, <span style="color: #800080;">$key</span>, <span style="color: #800080;">$this</span>-><span style="color: #000000;">iv);        </span><span style="color: #008000;">//</span><span style="color: #008000;">初始处理</span>        <span style="color: #800080;">$data</span> = mcrypt_generic(<span style="color: #800080;">$td</span>, <span style="color: #800080;">$input</span><span style="color: #000000;">);        </span><span style="color: #008000;">//</span><span style="color: #008000;">加密</span>        mcrypt_generic_deinit(<span style="color: #800080;">$td</span><span style="color: #000000;">);        </span><span style="color: #008000;">//</span><span style="color: #008000;">结束</span>        mcrypt_module_close(<span style="color: #800080;">$td</span><span style="color: #000000;">);        </span><span style="color: #800080;">$data</span> = <span style="color: #800080;">$this</span>->removeBR(<span style="color: #008080;">base64_encode</span>(<span style="color: #800080;">$data</span><span style="color: #000000;">));        </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$data</span><span style="color: #000000;">;    }        </span><span style="color: #008000;">//</span><span style="color: #008000;">解密</span>    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> decrypt(<span style="color: #800080;">$encrypted</span><span style="color: #000000;">)    {        </span><span style="color: #800080;">$encrypted</span> = <span style="color: #008080;">base64_decode</span>(<span style="color: #800080;">$encrypted</span><span style="color: #000000;">);        </span><span style="color: #800080;">$key</span> = <span style="color: #008080;">base64_decode</span>(<span style="color: #800080;">$this</span>-><span style="color: #008080;">key</span><span style="color: #000000;">);        </span><span style="color: #800080;">$td</span> = mcrypt_module_open( MCRYPT_3DES,'',MCRYPT_MODE_CBC,''<span style="color: #000000;">);        </span><span style="color: #008000;">//</span><span style="color: #008000;">使用MCRYPT_3DES算法,cbc模式</span>        mcrypt_generic_init(<span style="color: #800080;">$td</span>, <span style="color: #800080;">$key</span>, <span style="color: #800080;">$this</span>-><span style="color: #000000;">iv);        </span><span style="color: #008000;">//</span><span style="color: #008000;">初始处理</span>        <span style="color: #800080;">$decrypted</span> = mdecrypt_generic(<span style="color: #800080;">$td</span>, <span style="color: #800080;">$encrypted</span><span style="color: #000000;">);        </span><span style="color: #008000;">//</span><span style="color: #008000;">解密</span>        mcrypt_generic_deinit(<span style="color: #800080;">$td</span><span style="color: #000000;">);        </span><span style="color: #008000;">//</span><span style="color: #008000;">结束</span>        mcrypt_module_close(<span style="color: #800080;">$td</span><span style="color: #000000;">);        </span><span style="color: #800080;">$decrypted</span> = <span style="color: #800080;">$this</span>->removePadding(<span style="color: #800080;">$decrypted</span><span style="color: #000000;">);        </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$decrypted</span><span style="color: #000000;">;    }        </span><span style="color: #008000;">//</span><span style="color: #008000;">填充密码,填充至8的倍数</span>    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> padding( <span style="color: #800080;">$str</span><span style="color: #000000;"> )    {        </span><span style="color: #800080;">$len</span> = 8 - <span style="color: #008080;">strlen</span>( <span style="color: #800080;">$str</span> ) % 8<span style="color: #000000;">;        </span><span style="color: #0000ff;">for</span> ( <span style="color: #800080;">$i</span> = 0; <span style="color: #800080;">$i</span> $len; <span style="color: #800080;">$i</span>++<span style="color: #000000;"> )        {            </span><span style="color: #800080;">$str</span> .= <span style="color: #008080;">chr</span>( 0<span style="color: #000000;"> );        }        </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$str</span><span style="color: #000000;"> ;    }        </span><span style="color: #008000;">//</span><span style="color: #008000;">删除填充符</span>    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> removePadding( <span style="color: #800080;">$str</span><span style="color: #000000;"> )    {        </span><span style="color: #800080;">$len</span> = <span style="color: #008080;">strlen</span>( <span style="color: #800080;">$str</span><span style="color: #000000;"> );        </span><span style="color: #800080;">$newstr</span> = ""<span style="color: #000000;">;        </span><span style="color: #800080;">$str</span> = <span style="color: #008080;">str_split</span>(<span style="color: #800080;">$str</span><span style="color: #000000;">);        </span><span style="color: #0000ff;">for</span> (<span style="color: #800080;">$i</span> = 0; <span style="color: #800080;">$i</span> $len; <span style="color: #800080;">$i</span>++<span style="color: #000000;"> )        {            </span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$str</span>[<span style="color: #800080;">$i</span>] != <span style="color: #008080;">chr</span>( 0<span style="color: #000000;"> ))            {                </span><span style="color: #800080;">$newstr</span> .= <span style="color: #800080;">$str</span>[<span style="color: #800080;">$i</span><span style="color: #000000;">];            }        }        </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$newstr</span><span style="color: #000000;">;    }        </span><span style="color: #008000;">//</span><span style="color: #008000;">删除回车和换行</span>    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> removeBR( <span style="color: #800080;">$str</span><span style="color: #000000;"> )     {        </span><span style="color: #800080;">$len</span> = <span style="color: #008080;">strlen</span>( <span style="color: #800080;">$str</span><span style="color: #000000;"> );        </span><span style="color: #800080;">$newstr</span> = ""<span style="color: #000000;">;        </span><span style="color: #800080;">$str</span> = <span style="color: #008080;">str_split</span>(<span style="color: #800080;">$str</span><span style="color: #000000;">);        </span><span style="color: #0000ff;">for</span> (<span style="color: #800080;">$i</span> = 0; <span style="color: #800080;">$i</span> $len; <span style="color: #800080;">$i</span>++<span style="color: #000000;"> )        {            </span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$str</span>[<span style="color: #800080;">$i</span>] != '\n' and <span style="color: #800080;">$str</span>[<span style="color: #800080;">$i</span>] != '\r'<span style="color: #000000;">)            {                </span><span style="color: #800080;">$newstr</span> .= <span style="color: #800080;">$str</span>[<span style="color: #800080;">$i</span><span style="color: #000000;">];            }        }            </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$newstr</span><span style="color: #000000;">;    }}</span><span style="color: #008000;">//</span><span style="color: #008000;">test</span><span style="color: #800080;">$input</span> = "1qaz2ws"<span style="color: #000000;">;</span><span style="color: #0000ff;">echo</span> "plainText:" . <span style="color: #800080;">$input</span>."<br>"<span style="color: #000000;">;</span><span style="color: #800080;">$crypt</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> Crypt3Des();</span><span style="color: #0000ff;">echo</span> "Encode:".<span style="color: #800080;">$crypt</span>->encrypt(<span style="color: #800080;">$input</span>)."<br>"<span style="color: #000000;">;</span><span style="color: #0000ff;">echo</span> "Decode:".<span style="color: #800080;">$crypt</span>->decrypt(<span style="color: #800080;">$crypt</span>->encrypt(<span style="color: #800080;">$input</span><span style="color: #000000;">));</span>?>
Copier après la connexion

代码可以不看,就看里面的一句:$td = mcrypt_module_open( MCRYPT_3DES, '', MCRYPT_MODE_CBC, '');报错的就是他。

我搜寻了一大堆解决方法,正确的方法应该是(仅用于windows系统哦):

当运行php的服务器端缺少libmcrypt.dll时使用函数mcrypt_module_open进行解密会出现此错误。 
在服务器上做如下设置可解决。

到网上下载一个php的mcrypt模块安装包,只需要libmcrypt.dll文件即可(一般官网上下载的,php目录下已经有的)

1.将libmcrypt.dll复制到system32目录或php安装目录下的extensions目录下 
2.将libmcrypt.dll复制到apache安装目录的bin目录下 
3.到windows目录下找到php.ini文件,打开它
4.找到; Directory in which the loadable extensions (modules) reside. 
extension_dir = "./"  如:extension_dir = "D:\php5\ext"
这两行,要使extension_dir指向的目录下能找到libmcrypt.dll,或系统path下有libmcrypt.dll 
5.找到;Windows Extensions 项下面的;extension=php_mcrypt.dll这一行和;extension=php_iconv.dll(我的没有,省略了)这两行,去掉前面的分号

ps:刚开始看网上的解决方法,有的说修改php安装目录下的php.ini,但是修改后是没用的。一定要修改windows目录下的php.ini!

Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn

Outils d'IA chauds

Undresser.AI Undress

Undresser.AI Undress

Application basée sur l'IA pour créer des photos de nu réalistes

AI Clothes Remover

AI Clothes Remover

Outil d'IA en ligne pour supprimer les vêtements des photos.

Undress AI Tool

Undress AI Tool

Images de déshabillage gratuites

Clothoff.io

Clothoff.io

Dissolvant de vêtements AI

Video Face Swap

Video Face Swap

Échangez les visages dans n'importe quelle vidéo sans effort grâce à notre outil d'échange de visage AI entièrement gratuit !

Outils chauds

Bloc-notes++7.3.1

Bloc-notes++7.3.1

Éditeur de code facile à utiliser et gratuit

SublimeText3 version chinoise

SublimeText3 version chinoise

Version chinoise, très simple à utiliser

Envoyer Studio 13.0.1

Envoyer Studio 13.0.1

Puissant environnement de développement intégré PHP

Dreamweaver CS6

Dreamweaver CS6

Outils de développement Web visuel

SublimeText3 version Mac

SublimeText3 version Mac

Logiciel d'édition de code au niveau de Dieu (SublimeText3)

Guide d'installation et de mise à niveau de PHP 8.4 pour Ubuntu et Debian Guide d'installation et de mise à niveau de PHP 8.4 pour Ubuntu et Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 apporte plusieurs nouvelles fonctionnalités, améliorations de sécurité et de performances avec une bonne quantité de dépréciations et de suppressions de fonctionnalités. Ce guide explique comment installer PHP 8.4 ou mettre à niveau vers PHP 8.4 sur Ubuntu, Debian ou leurs dérivés. Bien qu'il soit possible de compiler PHP à partir des sources, son installation à partir d'un référentiel APT comme expliqué ci-dessous est souvent plus rapide et plus sécurisée car ces référentiels fourniront les dernières corrections de bogues et mises à jour de sécurité à l'avenir.

Comment configurer Visual Studio Code (VS Code) pour le développement PHP Comment configurer Visual Studio Code (VS Code) pour le développement PHP Dec 20, 2024 am 11:31 AM

Visual Studio Code, également connu sous le nom de VS Code, est un éditeur de code source gratuit – ou environnement de développement intégré (IDE) – disponible pour tous les principaux systèmes d'exploitation. Avec une large collection d'extensions pour de nombreux langages de programmation, VS Code peut être c

Expliquez les jetons Web JSON (JWT) et leur cas d'utilisation dans les API PHP. Expliquez les jetons Web JSON (JWT) et leur cas d'utilisation dans les API PHP. Apr 05, 2025 am 12:04 AM

JWT est une norme ouverte basée sur JSON, utilisée pour transmettre en toute sécurité des informations entre les parties, principalement pour l'authentification de l'identité et l'échange d'informations. 1. JWT se compose de trois parties: en-tête, charge utile et signature. 2. Le principe de travail de JWT comprend trois étapes: la génération de JWT, la vérification de la charge utile JWT et l'analyse. 3. Lorsque vous utilisez JWT pour l'authentification en PHP, JWT peut être généré et vérifié, et les informations sur le rôle et l'autorisation des utilisateurs peuvent être incluses dans l'utilisation avancée. 4. Les erreurs courantes incluent une défaillance de vérification de signature, l'expiration des jetons et la charge utile surdimensionnée. Les compétences de débogage incluent l'utilisation des outils de débogage et de l'exploitation forestière. 5. L'optimisation des performances et les meilleures pratiques incluent l'utilisation des algorithmes de signature appropriés, la définition des périodes de validité raisonnablement,

Programme PHP pour compter les voyelles dans une chaîne Programme PHP pour compter les voyelles dans une chaîne Feb 07, 2025 pm 12:12 PM

Une chaîne est une séquence de caractères, y compris des lettres, des nombres et des symboles. Ce tutoriel apprendra à calculer le nombre de voyelles dans une chaîne donnée en PHP en utilisant différentes méthodes. Les voyelles en anglais sont a, e, i, o, u, et elles peuvent être en majuscules ou en minuscules. Qu'est-ce qu'une voyelle? Les voyelles sont des caractères alphabétiques qui représentent une prononciation spécifique. Il y a cinq voyelles en anglais, y compris les majuscules et les minuscules: a, e, i, o, u Exemple 1 Entrée: String = "TutorialSpoint" Sortie: 6 expliquer Les voyelles dans la chaîne "TutorialSpoint" sont u, o, i, a, o, i. Il y a 6 yuans au total

Expliquez la liaison statique tardive en PHP (statique: :). Expliquez la liaison statique tardive en PHP (statique: :). Apr 03, 2025 am 12:04 AM

Liaison statique (statique: :) ​​implémente la liaison statique tardive (LSB) dans PHP, permettant à des classes d'appel d'être référencées dans des contextes statiques plutôt que de définir des classes. 1) Le processus d'analyse est effectué au moment de l'exécution, 2) Recherchez la classe d'appel dans la relation de succession, 3) il peut apporter des frais généraux de performance.

Comment analysez-vous et traitez-vous HTML / XML dans PHP? Comment analysez-vous et traitez-vous HTML / XML dans PHP? Feb 07, 2025 am 11:57 AM

Ce tutoriel montre comment traiter efficacement les documents XML à l'aide de PHP. XML (Language de balisage extensible) est un langage de balisage basé sur le texte polyvalent conçu à la fois pour la lisibilité humaine et l'analyse de la machine. Il est couramment utilisé pour le stockage de données et

Quelles sont les méthodes PHP Magic (__construct, __ destruct, __ call, __get, __set, etc.) et fournir des cas d'utilisation? Quelles sont les méthodes PHP Magic (__construct, __ destruct, __ call, __get, __set, etc.) et fournir des cas d'utilisation? Apr 03, 2025 am 12:03 AM

Quelles sont les méthodes magiques de PHP? Les méthodes magiques de PHP incluent: 1. \ _ \ _ Construct, utilisé pour initialiser les objets; 2. \ _ \ _ Destruct, utilisé pour nettoyer les ressources; 3. \ _ \ _ Appel, gérer les appels de méthode inexistants; 4. \ _ \ _ GET, Implémentez l'accès à l'attribut dynamique; 5. \ _ \ _ SET, Implémentez les paramètres d'attribut dynamique. Ces méthodes sont automatiquement appelées dans certaines situations, améliorant la flexibilité et l'efficacité du code.

PHP et Python: comparaison de deux langages de programmation populaires PHP et Python: comparaison de deux langages de programmation populaires Apr 14, 2025 am 12:13 AM

PHP et Python ont chacun leurs propres avantages et choisissent en fonction des exigences du projet. 1.Php convient au développement Web, en particulier pour le développement rapide et la maintenance des sites Web. 2. Python convient à la science des données, à l'apprentissage automatique et à l'intelligence artificielle, avec syntaxe concise et adaptée aux débutants.

See all articles