使用r解密在PHP中加密的消息

发布于 2025-01-28 20:58:32 字数 2302 浏览 2 评论 0原文

我的大脑很痛,我无法弄清楚,我将非常感谢您的帮助。我有一个数据库,可以使用PHP加密存储。我需要在R中解密它。我觉得自己很亲密,但我无法到达那里。

这是用于加密的PHP代码:

<?php

function str_encryptaesgcm($plaintext, $password) {
$iv = "1234567890123456";
$ciphertext = openssl_encrypt(
    $plaintext, 
    "AES-256-CBC", 
    hash('sha256', $password, true), 
    OPENSSL_RAW_DATA, 
    $iv);
return base64_encode($ciphertext);
}

function str_decryptaesgcm($ciphertext, $password) {
$iv = "1234567890123456";
return openssl_decrypt(
        base64_decode($ciphertext), 
        "AES-256-CBC", 
        hash('sha256', $password, true), 
        OPENSSL_RAW_DATA, 
        $iv);
}

$enc = str_encryptaesgcm("Hello, world!", "ABCDEFGHIJKLMNOPQRST");
echo $enc . " ";
$enc = "8FT21xlAENs0Q8GTDE5k0A==";
$dec = str_decryptaesgcm($enc, "ABCDEFGHIJKLMNOPQRST");
echo $dec;

“ 8ft21xlaens0q8gtde5k0a ==”是“ Hello,World!”的加密结果。我的数据库中有消息。如果我通过str_decryptaesgcm函数运行它,则吐出原始消息。 我需要在r。中对此进行解码,

以在R中复制此过程,需要运行以下代码:

library(openssl)
x <- aes_cbc_encrypt(serialize('Hello, world!', NULL), 
                     key = sha256(charToRaw('ABCDEFGHIJKLMNOPQRST')), 
                     iv = charToRaw('1234567890123456'))
y <- aes_cbc_decrypt(
  data = x, 
  key = sha256(charToRaw('ABCDEFGHIJKLMNOPQRST')), 
  iv = charToRaw('1234567890123456'))
unserialize(y)

使用Unerialize(Y)(Y)吐出原始消息。

但是,base64_encode(x)产生的字符串与php中产生的弦完全不同:“ 1y6cgay23ap+tqvxqkgsyyzwdmgj/gxehjyyrjyfonyrjufbfonyrjufbfierc4aj7/9udzrllc21q7v+1badtuse”。

i can 让R产生PHP字符串,但是我需要以不同的方式运行加密:

x <- aes_cbc_encrypt(charToRaw('Hello, world!'), 
                     key = sha256(charToRaw('ABCDEFGHIJKLMNOPQRST')), 
                     iv = charToRaw('1234567890123456'))

然后,base64_encode(x)生产“ 8ft21xlaens0q8gtde5k0a ==”。但是,将其插入解码器会产生垃圾(请注意,它确实是解码,不会丢弃错误),我无法转换:

> y <- aes_cbc_decrypt(
+   data = x, 
+   key = sha256(charToRaw('ABCDEFGHIJKLMNOPQRST')), 
+   iv = charToRaw('1234567890123456'))
> y
 [1] 48 65 6c 6c 6f 2c 20 77 6f 72 6c 64 21
> unserialize(y)
Error in unserialize(y) : unknown input format

所以问题是,我该怎么办才能转换“ 8ft21xlaens0q8gtde5k0a ===”回到原始消息?我真的很感谢一些建议。

my brain hurts, I can't figure it out, I would be tremendously thankful for your help. I have a database that gets encrypted at storage using PHP. I need to decrypt it in R. I feel like I am close, but I just can't get there.

Here's the PHP code used to encrypt:

<?php

function str_encryptaesgcm($plaintext, $password) {
$iv = "1234567890123456";
$ciphertext = openssl_encrypt(
    $plaintext, 
    "AES-256-CBC", 
    hash('sha256', $password, true), 
    OPENSSL_RAW_DATA, 
    $iv);
return base64_encode($ciphertext);
}

function str_decryptaesgcm($ciphertext, $password) {
$iv = "1234567890123456";
return openssl_decrypt(
        base64_decode($ciphertext), 
        "AES-256-CBC", 
        hash('sha256', $password, true), 
        OPENSSL_RAW_DATA, 
        $iv);
}

$enc = str_encryptaesgcm("Hello, world!", "ABCDEFGHIJKLMNOPQRST");
echo $enc . " ";
$enc = "8FT21xlAENs0Q8GTDE5k0A==";
$dec = str_decryptaesgcm($enc, "ABCDEFGHIJKLMNOPQRST");
echo $dec;

Here, "8FT21xlAENs0Q8GTDE5k0A==" is the encrypted result of the "Hello, world!" message that I have in my database. If I run it through the str_decryptaesgcm function it spits out the original message. I need to decode this in R.

To replicate this process in R one needs to run the following code:

library(openssl)
x <- aes_cbc_encrypt(serialize('Hello, world!', NULL), 
                     key = sha256(charToRaw('ABCDEFGHIJKLMNOPQRST')), 
                     iv = charToRaw('1234567890123456'))
y <- aes_cbc_decrypt(
  data = x, 
  key = sha256(charToRaw('ABCDEFGHIJKLMNOPQRST')), 
  iv = charToRaw('1234567890123456'))
unserialize(y)

with unserialize(y) spitting out the original message.

However, base64_encode(x) produces a string that is completely different from the one produced in PHP: "1y6CgaY23ap+tQVXQKGsYyZWDMGj/GxeHjyyFOnyRJufbfieRC4aJ7/9uDzRllC21Q7v+1bADtuzEfG83iakBw==".

I can get R to produce the PHP string, but I need to run the encryption differently:

x <- aes_cbc_encrypt(charToRaw('Hello, world!'), 
                     key = sha256(charToRaw('ABCDEFGHIJKLMNOPQRST')), 
                     iv = charToRaw('1234567890123456'))

Then, base64_encode(x) produces "8FT21xlAENs0Q8GTDE5k0A==". However, plugging this into the decoder produces garbage (note that it does decode, it doesn't throw an error) that I can't convert:

> y <- aes_cbc_decrypt(
+   data = x, 
+   key = sha256(charToRaw('ABCDEFGHIJKLMNOPQRST')), 
+   iv = charToRaw('1234567890123456'))
> y
 [1] 48 65 6c 6c 6f 2c 20 77 6f 72 6c 64 21
> unserialize(y)
Error in unserialize(y) : unknown input format

So the question is, what should I do to be able to convert "8FT21xlAENs0Q8GTDE5k0A==" back to the original message? I'd really appreciate some advice.

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

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

发布评论

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

评论(1

与酒说心事 2025-02-04 20:58:33

是的,您非常接近。如果您仔细查看y

y
#> [1] 48 65 6c 6c 6f 2c 20 77 6f 72 6c 64 21

您将看到这些是原始消息的ASCII值,作为原始向量。因此,您只需要做:

rawToChar(y)
#> [1] "Hello, world!"

Yes, you were incredibly close. If you look carefully at y:

y
#> [1] 48 65 6c 6c 6f 2c 20 77 6f 72 6c 64 21

You will see these are the ascii values of your original message as a raw vector. So you need only do:

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