- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我们一直在尝试的遗留系统翻译的第二部分。我们已经设法完全匹配Windows :: CryptHashData生成的初始二进制密码/密钥。
该密码/密钥将传递给:: CryptDeriveKey,在此它执行许多步骤来创建最终的密钥,以供:: CryptEncrypt使用。我的研究使我进入了CryptDeriveKey文档,该文档清楚地描述了为:: CryptEncrypt导出密钥所需的步骤,但到目前为止,我还无法在PHP方面获得它来解密文件。
https://docs.microsoft.com/en-us/windows/desktop/api/wincrypt/nf-wincrypt-cryptderivekey
根据:: CryptDeriveKey文档,对于我们特定的传统密钥大小,可能还有一些其他未记录的步骤,可能无法很好地理解。默认情况下,当前Windows :: CryptDeriveKey设置为ZERO SALT,这显然与NO_SALT有所不同。在这里查看盐值功能:
https://docs.microsoft.com/en-us/windows/desktop/SecCrypto/salt-value-functionality
我们的旧系统的CryptAPI上的参数如下:
提供程序类型:PROV_RSA_FULL
提供程序名称:MS_DEF_PROV
算法ID CALG_RC4
说明RC4流加密算法
密钥长度:40位。
盐长:88位。零盐
特别说明:但是,带有零值盐的40位对称密钥并不等同于没有盐的40位对称密钥。为了实现互操作性,必须在不添加盐的情况下创建密钥。出现此问题的原因是默认情况下,仅使用正好40位的密钥才发生这种情况。
我不是要导出密钥,而是重现创建最终加密密钥的过程,该过程将最终的加密密钥传递给RC4加密算法的:: CryptEncrypt,并使其与openssl_decrypt一起使用。
这是当前适用于加密的Windows代码。
try {
BOOL bSuccess;
bSuccess = ::CryptAcquireContextA(&hCryptProv,
CE_CRYPTCONTEXT,
MS_DEF_PROV_A,
PROV_RSA_FULL,
CRYPT_MACHINE_KEYSET);
::CryptCreateHash(hCryptProv,
CALG_MD5,
0,
0,
&hSaveHash);
::CryptHashData(hSaveHash,
baKeyRandom,
(DWORD)sizeof(baKeyRandom),
0);
::CryptHashData(hSaveHash,
(LPBYTE)T2CW(pszSecret),
(DWORD)_tcslen(pszSecret) * sizeof(WCHAR),
0);
::CryptDeriveKey(hCryptProv,
CALG_RC4,
hSaveHash,
0,
&hCryptKey);
// Now Encrypt the value
BYTE * pData = NULL;
DWORD dwSize = (DWORD)_tcslen(pszToEncrypt) * sizeof(WCHAR);
// will be a wide str
DWORD dwReqdSize = dwSize;
::CryptEncrypt(hCryptKey,
NULL,
TRUE,
0,
(LPBYTE)NULL,
&dwReqdSize, 0);
dwReqdSize = max(dwReqdSize, dwSize);
pData = new BYTE[dwReqdSize];
memcpy(pData, T2CW(pszToEncrypt), dwSize);
if (!::CryptEncrypt(hCryptKey,
NULL,
TRUE,
0,
pData,
&dwSize,
dwReqdSize)) {
printf("%l\n", hCryptKey);
printf("error during CryptEncrypt\n");
}
if (*pbstrEncrypted)
::SysFreeString(*pbstrEncrypted);
*pbstrEncrypted = ::SysAllocStringByteLen((LPCSTR)pData, dwSize);
delete[] pData;
hr = S_OK;
}
function cryptoDeriveKey($key){
//Put the hash key into an array
$hashKey1 = str_split($key,2);
$count = count($hashKey1);
$hashKeyInt = array();
for ($i=0; $i<$count; $i++){
$hashKeyInt[$i] = hexdec($hashKey1[$i]);
}
$hashKey = $hashKeyInt;
//Let n be the required derived key length, in bytes. CALG_RC4 = 40 bits key or 88 salt bytes
$n = 40/8;
//Let k be the length of the hash value that is represented by the input parameter hBaseData
$k = 16;
//Step 1 Form a 64-byte buffer by repeating the constant 0x36 64 times
$arraya = array_fill(0, 64, 0x36);
//Set the first k bytes of the buffer to the result of an XOR operation of the first k bytes of the buffer with the hash value
for ($i=0; $i<$k; $i++){
$arraya[$i] = $arraya[$i] ^ $hashKey[$i];
}
//Hash the result of step 1 by using the same hash algorithm as hBaseData
$arrayPacka = pack('c*', ...$arraya);
$hashArraya = md5($arrayPacka);
//Put the hash string back into the array
$hashKeyArraya = str_split($hashArraya,2);
$count = count($hashKeyArraya);
$hashKeyInta = array();
for ($i=0; $i<$count; $i++){
$hashKeyInta[$i] = hexdec($hashKeyArraya[$i]);
}
//Step 2 Form a 64-byte buffer by repeating the constant 0x5C 64 times.
$arrayb = array_fill(0, 64, 0x5C);
//Set the first k bytes of the buffer to the result of an XOR operation of the first k bytes of the buffer with the hash value
for ($i=0; $i<$k; $i++){
$arrayb[$i] = $arrayb[$i] ^ $hashKey[$i];
}
//Hash the result of step 2 by using the same hash algorithm as hBaseData
$arrayPackb = pack('c*', ...$arrayb);
$hashArrayb = md5($arrayPackb);
//Put the hash string back into the array
$hashKeyArrayb = str_split($hashArrayb,2);
$count = count($hashKeyArrayb);
$hashKeyIntb = array();
for ($i=0; $i<$count; $i++){
$hashKeyIntb[$i] = hexdec($hashKeyArrayb[$i]);
}
//Concatenate the result of step 3 with the result of step 4.
$combined = array_merge($hashKeyInta, $hashKeyIntb);
//Use the first n bytes of the result of step 5 as the derived key.
$finalKey = array();
for ($i=0; $i <$n; $i++){
$finalKey[$i] = $combined[$i];
}
$key = $finalKey;
return $key;
}
function decryptRC4($encrypted, $key){
$opts = OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING;
$cypher = ‘rc4-40’;
$decrypted = openssl_decrypt($encrypted, $cypher, $key, $opts);
return $decrypted;
}
最佳答案
以防万一其他人走这条路,这里是上述所有问题的答案。
您可以使用openssl在PHP上复制:: CryptDeriveKey,但必须首先在Windows端满足一些先决条件。
必须将CryptDeriveKey设置为CRYPT_NO_SALT,如下所示:
::CrypeDeriveKey(hCryptProv, CALG_RC4, hSaveHash, CRYPT_NO_SALT, &hCryptKey)
<?php
$random = pack('c*', 87,194,...........);
$origSecret = 'ASCII STRING OF CHARACTERS AS PASSWORD';
//Need conversion to match format of Windows CString or wchar_t*
//Windows will probably be UTF-16LE and LAMP will be UTF-8
$secret = iconv('UTF-8','UTF-16LE', $origSecret);
//Create hash key from Random and Secret
//This is basically a hash and salt process.
$hash = hash_init("md5");
hash_update($hash, $random);
hash_update($hash, $secret);
$key = hash_final($hash);
$key = cryptoDeriveKey($key);
//Convert the key hex array to a hex string for openssl_decrypt
$count = count($key);
$maxchars = 2;
for ($i=0; $i<$count; $i++){
$key .= str_pad(dechex($key[$i]), $maxchars, "0", STR_PAD_LEFT);
}
$opts = OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING;
//Convert key hex string to a string for openssl_decrypt
//Leave it as it is for openssl command line.
$key = hexToStr($key);
$cipher = 'rc4-40';
$encrypted = “the data you want to encrypt or decrypt”;
$decrypted = openssl_decrypt($encrypted, $cipher, $key, $opts);
echo $decrypted; //This is the final information you’re looking for
function cryptoDeriveKey($key){
//convert the key into hex byte array as int
$hashKey1 = str_split($key,2);
$count = count($hashKey1);
$hashKeyInt = array();
for ($i=0; $i<$count; $i++){
$hashKeyInt[$i] = hexdec($hashKey1[$i]);
}
$hashKey = $hashKeyInt;
//Let n be the required derived key length, in bytes. CALG_RC4 = 40 bits key with 88 salt bits
$n = 40/8;
//Chop the key down to the first 40 bits or 5 bytes.
$finalKey = array();
for ($i=0; $i <$n; $i++){
$finalKey[$i] = $hashKey[$i];
}
return $finalKey;
}
function hexToStr($hex){
$string='';
for ($i=0; $i < strlen($hex)-1; $i+=2){
$string .= chr(hexdec($hex[$i].$hex[$i+1]));
}
return $string;
}
?>
::CrypeDeriveKey(hCryptProv, CALG_RC4, hSaveHash, CRYPT_EXPORTABLE | CRYPT_NO_SALT, &hCryptKey)
openssl enc -d -rc4-40 -in testFile-NO_SALT-enc.txt -out testFile-NO_SALT-dec.txt -K "Hex Key Value" -nosalt -nopad
关于php - 将Windows RC4 CryptDeriveKey转换为OpenSSL的PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53953631/
我很好奇这两个模块在实践中有什么区别吗?如果不是,为什么会有这两个副本? 最佳答案 std::rc::Rc只是 alloc::rc::Rc 的再导出.你可以在src/std/lib.rs中看到整个rc
我注意到 rust 书推荐使用 Rc::clone(&rc)在rc.clone()如下: use std::rc::Rc; let five = Rc::new(5); // recommended
我需要将我现有的 Angular 2 RC 1 应用程序迁移到 Angular 2 RC 4。作为其中的一部分,我还需要将我现有的表单迁移到 Angular 2 RC 4 New Forms。 任何人
如何测试 Selenium RC 中哪个元素具有焦点? 最佳答案 当我遇到同样的问题时,出于各种原因,其他答案都不适合我。我最终做了以下操作(使用 Java 中的 Selenium 2.0 WebDr
Horse是一个实现 Animal 的结构特征。我有一个 Rc和一个需要接收 Rc 的函数, 所以我想从 Rc 转换至 Rc . 我这样做了: use std::rc::Rc; struct Hors
代码如下所示: // Simplified pub trait Field: Send + Sync + Clone { fn name(&self); } #[deriving(Clone)
在 /etc/rc[06].d/ 目录中的程序启动之后,系统的启动就已经完成。不过,我们总有一些程序是需要在系统启动之后随着系统一起启动的。这时我们并不需要自己把需要启动的服务链接到 /etc/rc3
我正在使用 selenium rc,我需要测试 flash。我需要单击 Flash 播放器上的允许按钮。 我只是需要一些帮助才能开始? 最佳答案 通常,除非在 Flash 开发人员的帮助下将 Sele
我知道 PhantomData旨在使用数据类型定义中的生命周期或类型参数,否则这些参数将不会被使用。我最近在查看 Rc 的定义在Rust std lib并注意到它似乎使用了 PhantomData ,
谁能给我解释一下为什么Rc<>不是 Copy ? 我正在编写使用大量共享指针的代码,并且必须输入 .clone()总是让我心烦意乱。 在我看来Rc<>应该只包含一个固定大小的指针,所以类型本身应该是
我想创建一个 Rc因为我想减少跟踪访问 Rc 的 2 个指针的间接访问需要。我需要使用 Rc因为我确实拥有共享所有权。我在 another question 中详细说明关于我的字符串类型的更具体的问题
Selenium IDE 和 Selenium RC 的功能有什么区别? 最佳答案 Selenium IDE 是一个 firefox 插件,它为您提供了用于记录测试的基本记录器。这些测试使用关键字记录
我正在尝试使用 Selenium RC (Java) 在 Paypal-Sandbox 上购买东西,但它不起作用。 我用 Selenium IDE (Firefox AddOn) 试过了,它确实有效。
我有一个 Rc>但需要得到一个Rc从中。像这样的东西: let rc_option: Rc> = Rc::new(Ok(value)); let ok_value: Rc = rc_option.ma
我知道在 matplotlib 中,您可以使用 rc 或 rcParams 来自定义绘图的样式。但是,这些函数似乎存在于两个级别,例如 matplotlib.rc 与 matplotlib.pyplo
Rust 文档涵盖 Rc>相当广泛,但不涉及 RefCell> ,我现在遇到了。 这些是否有效地给出了相同的结果?它们之间有重要区别吗? 最佳答案 Do these effectively give
我有一段代码是这样的: use std::cell::RefCell; use std::rc::Rc; struct A(bool); impl A { fn get_ref(&self)
Rust 文档涵盖 Rc>相当广泛,但不涉及 RefCell> ,我现在遇到了。 这些是否有效地给出了相同的结果?它们之间有重要区别吗? 最佳答案 Do these effectively give
我试图获得引用计数 Rc从 HashMap 并将其放入不同的容器 ( Vec )。 原以为这会起作用(通过增加引用计数),但我却收到了一个“expected struct std::rc::Rc ,
前言 我们在ubuntu下要把一个程序加入开机启动,一般可以通过修改rc.local来完成,但ubuntu下有两个rc.local文件。分别是/etc/rc.local和/etc/init.d/r
我是一名优秀的程序员,十分优秀!