在X处分割字符串,然后在Y值处再次分割。

4

我有下面这段代码:

$Field = $FW->Encrypt("Test");

echo "<pre>";
print_r($Field);
echo "</pre>";
#  $IV_Count = strlen($Field['IV']);
#  $Key_Count = strlen($Field['Key']);
#  $Cipher_Count = strlen($Field['CipheredText']);

foreach ($Field AS $Keys => $Values){
    echo $Keys."Count = ".strlen($Values)."<br><br>";
}

输出结果如下:
Array
(
    [CipheredText] => x2nelnArnS1e2MTjOrq+wd9BxT6Ouxksz67yVdynKGI=
    [IV] => 16
    [Key] => III#TcTf‡eB12T
)

CipheredTextCount = 44

IVCount = 2

KeyCount = 16

无论输入什么,IV/KeyCount总是返回相同的值。但是,CipheredTextCount根据输入而变化。例如:

 $Field = $FW->Encrypt("This is a longer string");
foreach循环返回:

CipheredTextCount = 64(密文计数= 64)

IVCount = 2(IV计数= 2)

KeyCount = 16(密钥计数= 16)

现在,我的问题是:以44为文本计数的第一个示例如何在implode(“”,$ Field); 后拆分字符串以显示为原始数组?例如:
 echo implode("",$Field);

输出结果为:

ijGglH/vysf52J5aoTaDVHy4oavEBK4mZTrAL3lZMTI=16III#TcTf‡eB12T

基于strlen的结果?
可以将第一个$Cipher_Count的计数存储在数据库中供参考。
我的当前设置涉及将密钥和IV存储在与加密文本字符串分开的列中。我需要这些内容包含在一个字段中,并且脚本处理所需的信息来执行以下操作:

检索长字符串>将字符串拆分为原始数组>将数组推送到另一个函数> 解密> 返回解码后的字符串。

2个回答

1

为什么不使用 serialize 呢?这样,当您从数据库中获取数据时,可以使用 unserialize 将其恢复为数组。


0
$Combined_Field =  implode("",$Field);

    echo $str1 = substr($Combined_Field, 0, $Cipher_Count)."<br><br>";
    echo $str2 = substr($Combined_Field,$Cipher_Count,$IV_Count)."<br><br>";
    echo $str3 = substr($Combined_Field, $IV_Count+$Cipher_Count,$Key_Count);

或者使用一个函数:

function Cipher_Split($Cipher, $Cipher_Count, $KeyCount, $IVCount){
            return array(
                "Ciphered Text" => substr($Cipher,0,$Cipher_Count),
                "IV" => substr($Cipher,$Cipher_Count,$IVCount),
                "Key" => substr($Cipher,$IVCount+$Cipher_Count,$KeyCount)

            );
        }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接