我试图完成SOAP身份验证过程,但我无法对秘密进行加密/解密。 SOAP服务是用.NET构建的,我尝试在PHP中集成服务。
服务的示例以C#语法提供,但我需要这样做"翻译" PHP语法。
我收到的数据示例:
stdClass Object
(
[TicketGrantingTicketResponse] => stdClass Object
(
[Status] => stdClass Object
(
[Code] => Success
)
[TicketGrantingTicket] => stdClass Object
(
[Token] => 0187cde6cae1416a9a903065fef2ffeb
[Secret] => d724P3/dPHutvEfiPMX1dQ==
[Timestamp] => 2013-02-20T09:00:45
[Expires] => 2013-02-20T09:01:45
)
)
)
C#示例代码(仅以解密开头;))
// save the token for later use
this.tgtToken = rs.TicketGrantingTicket.Token;
// extract the secret from response
string secret = rs.TicketGrantingTicket.Secret;
// Now the decoding/decryption of the secret will have to be performed
// First we get the plain encoded bytes by converting the BASE64 secret
byte[] decodedSecret = Convert.FromBase64String(secret);
// Second, we create an MD5 hash of our passphrase which will serve as
// base data for the decryption.
// Note that in C# the md5 hash creation requires a byte array as input.
// The pass phrase is required to be a UTF8-encoded string, so we use
// the UTF8 encoding to convert to plain bytes.
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
byte[] md5PassPhraseHash = md5.ComputeHash(Encoding.UTF8.GetBytes(opPassPhrase));
// Now we have to prepare the TripleDES decryption. TripleDES requires us
// to pass a key and initialization vector. We use the 128bit md5 hash as key.
// The initialization vector is created with the leading 8 bytes (first 64bit)
// from the md5 hash.
byte[] cKey = md5PassPhraseHash;
byte[] cIV = new byte[8];
Array.Copy(cKey, cIV, 8);
// Decrypt with TripleDES
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
tdes.Key = cKey;
tdes.IV = cIV;
byte[] decryptedSecret = null;
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, tdes.CreateDecryptor(), CryptoStreamMode.Write))
{
cs.Write(decodedSecret, 0, decodedSecret.Length);
cs.FlushFinalBlock();
cs.Close();
}
decryptedSecret = ms.ToArray();
}
// finally convert the decrypted bytes back to a UTF8 encoded string
string decryptedResult = Encoding.UTF8.GetString(decryptedSecret);
我现在用PHP来解决这个问题,但我不确定我是否正走在正确的道路上:
// Get token.
$sToken = $oTicketGrantingTicketResponse->TicketGrantingTicketResponse->TicketGrantingTicket->Token;
// Get secret.
$sSecret = $oTicketGrantingTicketResponse->TicketGrantingTicketResponse->TicketGrantingTicket->Secret;
// Get BASE64 decoded secret.
$sSecretDecoded = base64_decode($sSecret);
// Create MD5 hash of passphrase (just an example passphrase).
$sPassPhraseHash = md5('sj93fhdsfd', true);
// Decode secret with 3DES(CBC/192bit) using the MD5 passphrase.
$sSecretDecoded = @mcrypt_decrypt(MCRYPT_3DES, $sPassphraseMD5, $sSecretBASE64Decoded, MCRYPT_MODE_CBC);
但是解码后的值($ sSecretDecoded)与从C#程序获得的原始值不同。我无法将cIV值添加到我的php 3des解密中。
任何帮助将不胜感激。
谢谢!