如何对字符串中的数字进行编码并随后对其进行解码?

发布于 2024-09-13 15:47:53 字数 2083 浏览 6 评论 0原文

我正在寻找将数字(0、1、2、...)编码为字符串的加密/解密函数,这样这些字符串看起来是随机的,稍后可以从字符串中解码回数字。

例如:3将被加密为ABFQWEMasdEE,6将被加密为poad_Asd#@sad。

如果我可以控制加密字符串中的字符数,以及其中可以出现哪些字符,那就太好了!


更新

我最终得到了这个解决方案:

<?php    

$key = 'secret_password';

for ($i = 100; $i < 110; $i++) {
    $text = "$i";
    $encrypted = encrypt($text, $key);
    $decrypted = decrypt($encrypted, $key);
    $decrypted = rtrim($decrypted, "\0");
    $ok = ($text === $decrypted);
    if (!$ok) {
        exit('********** BUG BUG BUG BUG BUG ***********');
    }
    echo '[' . $text . '] [' . $encrypted . '] [' . $decrypted . '] ' . ($ok ? 'OK' : 'BUG BUG BUG BUG BUG BUG BUG') . '<br />';
}
exit('***** OK ******');

function encrypt($data, $key) {     
    $td = mcrypt_module_open('cast-256', '', 'ecb', '');
    $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
    mcrypt_generic_init($td, $key, $iv);
    $encrypted_data = mcrypt_generic($td, $data);
    mcrypt_generic_deinit($td);
    mcrypt_module_close($td);
    return base64_encode($encrypted_data);
}   

function decrypt($encoded_64, $key) {
    $td = mcrypt_module_open('cast-256', '', 'ecb', '');
    $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
    mcrypt_generic_init($td, $key, $iv);
    $decrypted_data = mdecrypt_generic($td, base64_decode($encoded_64));
    mcrypt_generic_deinit($td);
    mcrypt_module_close($td);
    return $decrypted_data;
}  

?>

它提供了以下输出:

[100] [9UA0Maq3MGp0CzMOWcpOpg==] [100] OK
[101] [Y5WKH7J1+k0bFqsGw1jmrA==] [101] 好的
[102] [NqV2opJc7CNq5O3lcuWKMw==] [102] 好的
[103] [1FpJhHe+vrK6aKA54VR53Q==] [103] 好的
[104] [MHQoYCqL4yCI9jKg1U0UYw==] [104] 好的
[105] [6Qq9aXEn46xpDgv8CvnK7Q==] [105] 好的
[106] [UGk1/byT7wpoFM59Uy/pdg==] [106] 好的
[107] [39kyPA49zAZsCFx3pt6bYw==] [107] 好的
[108] [YccDSimEf3C0NKDaVOf4kA==] [108] 好的
[109] [PfmvLfVR4+gi9y9v/6efZQ==] [109] 好的
***** 好的 ******

字符串看起来是随机的(除了末尾的 == )并且大小相同。不是完美的解决方案,但足够好!

谢谢大家!!

I'm looking for encrypt/decrypt functions that will encode numbers (0, 1, 2, ...) into strings, such that these strings will look random, and later the number could be decodes back from the string.

For example: 3 will be encrypted to ABFQWEMasdEE, and 6 will be encrypted to poad_Asd#@sad.

If I could control the number of characters in the encrypted string, and also which characters can appear there, it could be great !


UPDATE

I end up with this solution:

<?php    

$key = 'secret_password';

for ($i = 100; $i < 110; $i++) {
    $text = "$i";
    $encrypted = encrypt($text, $key);
    $decrypted = decrypt($encrypted, $key);
    $decrypted = rtrim($decrypted, "\0");
    $ok = ($text === $decrypted);
    if (!$ok) {
        exit('********** BUG BUG BUG BUG BUG ***********');
    }
    echo '[' . $text . '] [' . $encrypted . '] [' . $decrypted . '] ' . ($ok ? 'OK' : 'BUG BUG BUG BUG BUG BUG BUG') . '<br />';
}
exit('***** OK ******');

function encrypt($data, $key) {     
    $td = mcrypt_module_open('cast-256', '', 'ecb', '');
    $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
    mcrypt_generic_init($td, $key, $iv);
    $encrypted_data = mcrypt_generic($td, $data);
    mcrypt_generic_deinit($td);
    mcrypt_module_close($td);
    return base64_encode($encrypted_data);
}   

function decrypt($encoded_64, $key) {
    $td = mcrypt_module_open('cast-256', '', 'ecb', '');
    $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
    mcrypt_generic_init($td, $key, $iv);
    $decrypted_data = mdecrypt_generic($td, base64_decode($encoded_64));
    mcrypt_generic_deinit($td);
    mcrypt_module_close($td);
    return $decrypted_data;
}  

?>

which provides the following output:

[100] [9UA0Maq3MGp0CzMOWcpOpg==] [100] OK
[101] [Y5WKH7J1+k0bFqsGw1jmrA==] [101] OK
[102] [NqV2opJc7CNq5O3lcuWKMw==] [102] OK
[103] [1FpJhHe+vrK6aKA54VR53Q==] [103] OK
[104] [MHQoYCqL4yCI9jKg1U0UYw==] [104] OK
[105] [6Qq9aXEn46xpDgv8CvnK7Q==] [105] OK
[106] [UGk1/byT7wpoFM59Uy/pdg==] [106] OK
[107] [39kyPA49zAZsCFx3pt6bYw==] [107] OK
[108] [YccDSimEf3C0NKDaVOf4kA==] [108] OK
[109] [PfmvLfVR4+gi9y9v/6efZQ==] [109] OK
***** OK ******

The strings looks random (except the == at the end) and all of the same size. Not perfect solution, but good enough !

Thank you all !!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

风追烟花雨 2024-09-20 15:47:53

您可以使用 PHP 的加密函数之一 (mcrypt) 来加密您的数据,然后使用 base64_encode 将其编码为可以发送为的字符串文本。

You can use one of PHP's encryption functions (mcrypt) to encrypt your data, then use base64_encode to encode it into a string that can be sent as text.

一城柳絮吹成雪 2024-09-20 15:47:53

查看 Crypt_XXTEA Pear 包。您可以用它加密/解密任意字符串。它使用 XXTEA 分组密码算法(请参阅维基百科上的 XXTEA)。如果您想要一个“好的”格式,您还可以对加密的输出进行 uuencode。

Have a look at the Crypt_XXTEA Pear Package. You can encrypt/decrypt arbitrary Strings with it. It uses the XXTEA block cipher algorithm (see XXTEA at Wikipedia). If you want to have a "nice" format, you can additionally uuencode the encrpted output.

吾家有女初长成 2024-09-20 15:47:53

使用像 rot13 这样简单的东西,并向每个数字附加一组额外的字符。是的,这是一种非常弱的加密形式,但它解决了问题,您可以使输出与您想要的相匹配。

如果用户想要更复杂的东西,那么尝试使用 tweber 的答案,但是这样你就无法真正控制字符串输出的长度或随机性。

Use something simple like rot13 and append a extra set of characters to each number. Yes this is a really weak form of encryption but it solves the question and you can make the output match what ever you want.

If the user wants something more complex then try using tweber's answer , but then you cant really control the length or randomness of the string output.

虫児飞 2024-09-20 15:47:53

查看base64_encodebase64_decode

look at base64_encode and base64_decode.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文