首页 > 编程语言 >Decrypt string in C# that was encrypted with PHP openssl_encrypt

Decrypt string in C# that was encrypted with PHP openssl_encrypt

时间:2022-11-30 20:33:41浏览次数:66  
标签:encrypt string C# encrypted bytes key PHP out

Decrypt string in C# that was encrypted with PHP openssl_encrypt

回答1

Well this was fun to work out and required jumping into the PHP source code with some interesting results. Firstly PHP doesn't even use a key derivation algorithm it just takes the bytes of the passphrase and pads it out with zero's to the required length. That means the entire DeriveKeyAndIV method isn't necessary.

Because of the above that means the IV that is being used is a 16 length byte array containing zeros.

Finally the only other thing wrong with your code is that the source you copied it from used a salt in their implementation of encrypt which then had to be removed, PHP nor you are doing this so removing the salt bytes is incorrect.

So the all of this put together means you need to change the OpenSSLDecrypt method to this.

public static string OpenSSLDecrypt(string encrypted, string passphrase)
{
    //get the key bytes (not sure if UTF8 or ASCII should be used here doesn't matter if no extended chars in passphrase)
    var key = Encoding.UTF8.GetBytes(passphrase);

    //pad key out to 32 bytes (256bits) if its too short
    if (key.Length < 32)
    {
        var paddedkey = new byte[32];
        Buffer.BlockCopy(key, 0, paddedkey, 0, key.Length);
        key = paddedkey;
    }

    //setup an empty iv
    var iv = new byte[16];

    //get the encrypted data and decrypt
    byte[] encryptedBytes = Convert.FromBase64String(encrypted);
    return DecryptStringFromBytesAes(encryptedBytes, key, iv);
}

And very finally the resulting string has some extra chars at the end namely a set of 3 of the ETX char but these should be easy enough to filter out. I actually can't figure out where these are coming from.

 

Thanks to @neubert for pointing out the padding is a part of the standard PKCS padding if you want the framework to remove this just specify that as the padding mode when instantiating the RijndaelManaged object.

new RijndaelManaged { Padding = PaddingMode.PKCS7 };

 

标签:encrypt,string,C#,encrypted,bytes,key,PHP,out
From: https://www.cnblogs.com/chucklu/p/16939629.html

相关文章

  • sonarcqube
    ​​https://zhuanlan.zhihu.com/p/136723994​​  SonarQube实现自动化代码扫描 SonarQube之gitlab-plugin配合gitlab-ci完成每次commit代码检测 【Sonarqube】......
  • ServletContext-概述、获取
    ServletContext-概述1.概念:代表整个web应用,可以和程序的容器(服务器)来通信2.获取:1.通过request对象获取request.getServletContext()......
  • 洛谷 P4552 [Poetize6] IncDec Sequence(差分)
    题目分析直接贴一个洛谷上的题解,真的秀,讲的又清楚又好要使得序列的数全部相等,其实就是让他们之间的差全为0,也就是差分序列的除了第一项每一项都是0,为什么除了第一项呢,因......
  • Idea的hierarchy打开
    hierarchy:层级结构,在idea中功能为打开一个类的层级机构图如果想查看哪个类的层级结构图,就打开该类,然后按快捷键ctrl+H,或者negative-->typehierarchy......
  • Reack hooks useEffect 总结
    useEffect总结特性参数必须是一个回调函数与一个数组组件首次加载会执行一次useEffect的回调,之后依赖的值更新则会执行useEffect中的回调。如果第二个参数是一个空数......
  • 力扣 leetcode 153. 寻找旋转排序数组中的最小值
    问题描述已知一个长度为n的数组,预先按照升序排列,经由1到n次旋转后,得到输入数组。例如,原数组nums=[0,1,2,4,5,6,7]在变化后可能得到:若旋转4次,则可以得到[4......
  • scp 限速
    如题。scp的显示选项为-l?(kbits/s) 注意:器限速单位为K比特不是K字节!   1K比特/8=128字节=128Bytes  所以,限速的参数应为:你期望的值为:P(比如通......
  • SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) 被调用2次
    问题GAS技能输入绑定失效。直接原因在ListenServer的一端,ACharacter::SetupPlayerInputComponent在游戏开始后,被调用了2次,说明第1次输入绑定的InputComponent被移除(......
  • BoCloud博云微服务平台3.0正式发布:让微服务转型路径更清晰
    近日,BoCloud博云BeyondMicroservice微服务平台3.0版本正式发布。BeyondMircoservice3.0是博云微服务平台从“提供微服务治理功能”到“提供微服务转型整体建设方案”......
  • hive sql语句转换成mapreduce
    hivesql语句转换成mapreduce 转:https://www.cnblogs.com/w-j-q/p/14863034.html#autoid-2-5-01.hive是什么?2.MapReduce框架实现SQL基本操作的原理是什么?3.Hive怎......