使用r解密在PHP中加密的消息
我的大脑很痛,我无法弄清楚,我将非常感谢您的帮助。我有一个数据库,可以使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,您非常接近。如果您仔细查看
y
:您将看到这些是原始消息的ASCII值,作为原始向量。因此,您只需要做:
Yes, you were incredibly close. If you look carefully at
y
:You will see these are the ascii values of your original message as a raw vector. So you need only do: