在常见的PHP安装中进行双向加密的最简单方法是什么?
我需要能够使用字符串密钥加密数据,并使用相同的密钥在另一端解密.
安全性并不像代码的可移植性那么重要,因此我希望能够尽可能简化事情.目前,我正在使用RC4实现,但如果我能找到本机支持的东西,我想我可以节省很多不必要的代码.
我的意思是:
Original String + Salt or Key --> Encrypted String
Encrypted String + Salt or Key --> Decrypted (Original String)
Run Code Online (Sandbox Code Playgroud)
也许是这样的:
"hello world!" + "ABCD1234" --> Encrypt --> "2a2ffa8f13220befbe30819047e23b2c" (may be, for e.g)
"2a2ffa8f13220befbe30819047e23b2c" --> Decrypt with "ABCD1234" --> "hello world!"
Run Code Online (Sandbox Code Playgroud)
尝试使用Crypt_Blowfish,但它对我不起作用.
我在PHP中找到了en/decode字符串的示例.起初它看起来非常好,但它不会工作:-(
有谁知道问题是什么?
$Pass = "Passwort";
$Clear = "Klartext";
$crypted = fnEncrypt($Clear, $Pass);
echo "Encrypted: ".$crypted."</br>";
$newClear = fnDecrypt($crypted, $Pass);
echo "Decrypted: ".$newClear."</br>";
function fnEncrypt($sValue, $sSecretKey) {
return trim(base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $sSecretKey, $sDecrypted, MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND))));
}
function fnDecrypt($sValue, $sSecretKey) {
return trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $sSecretKey, base64_decode($sEncrypted), MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND)));
}
Run Code Online (Sandbox Code Playgroud)
结果是:
加密: boKRNTYYNp7AiOvY1CidqsAn9wX4ufz/D9XrpjAOPk8=
解密: —‚(ÑÁ ^ yË~F'¸®Ó–í œð2Á_B‰Â—
多年来,我一直在我的php应用程序中大量使用mcrypt,无论是在win/IIS还是在linux上.虽然我在我的linux服务器上运行PHP 5.4.28,但我刚刚在Windows 8.1 IIS框中升级到PHP 5.6.11.并且mcrypt不再有效.它不会抛出我能看到的任何错误; 它只是不起作用.这是我的加密功能:
function Encrypt($text){
global $salt;
if($text != "")
return trim(base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $salt, $text, MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND))));
else
return "";
}
Run Code Online (Sandbox Code Playgroud)
这在我的Linux服务器上工作正常,但在我的本地Windows框中返回空白.根据我的阅读,mcrypt内置于php 5.6 for windows中,因此不应该使用扩展或ini文件.
我错过了什么?
我正在与客户进行数据交换集成,他们发送给我的数据使用他们的C#encrypt方法加密(如下).
我的应用程序运行PHP 5.3,我需要一个等效的代码来解密他们发送的数据.我有PHP代码,但它不能正确解密客户端数据.
很明显,我在加密/解密方法,IV键或其他方面犯了一些错误.谁能发现错误?
谢谢.
C#代码(来自我的客户):
using System;
using System.Security.Cryptography;
using System.Text;
using System.IO;
public class Program
{
public static void Main()
{
var text = "this is a plain string";
var enc = Program.Encrypt(text);
Console.WriteLine(enc);
Console.WriteLine(Program.Decrypt(enc));
}
public static string Encrypt(string clearText)
{
var EncryptionKey = "1234567890123456";
byte[] clearBytes = Encoding.Unicode.GetBytes(clearText);
using (Aes encryptor = Aes.Create())
{
byte[] IV = new byte[15];
var rand = new Random();
rand.NextBytes(IV);
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, IV);
encryptor.Key = pdb.GetBytes(32); …Run Code Online (Sandbox Code Playgroud)