C#/PHP Compatible Encryption (AES256) ZZ
Finding a way to encrypt messages in C# and decrypting them in PHP or vice versa seems to be a "challenge" for many users. I wrote this tutorial to provide some help with this: below, you can find how to encrypt / decrypt messages in C# / PHP using AES256 with CBC mode.
1.Basic Information
AES 256 with CBC mode requires 3 values: the message, a key (32 bytes long) and an initialization vector (IV). Note that you must use the same IV when encrypting / decrypting a message: otherwise the message is lost. Sending the IV with the message is perfectly safe but it always has to be a random value. Since it has a fixed size, I always place the IV at the end of the encrypted text.
The encrypted messages should be encoded using base64 before being sent.

Encryption steps:
- encrypt the text
- add the IV at the end
- encode everything (base64)
Decryption steps:
- decode the message
- get & remove the IV
- proceed to decypt
Ok, enough talking, let's see some code...
2.PHP Encryption/Decryption Code
PHP accepts keys that are not 32 bytes long and simply extends them to the correct length. Well...C# doesn't, so you'll have to use a key that is 32 bytes long.
Encryption
- function encrypt($text, $pkey)
- {
- $key = $pkey;
- $IV = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC), MCRYPT_RAND);
- return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_CBC, $IV)."-[--IV-[-".$IV);
- }
Decryption
- function decrypt($text, $pkey)
- {
- $key = $pkey;
- $text = base64_decode($text);
- $IV = substr($text, strrpos($text, "-[--IV-[-") + 9);
- $text = str_replace("-[--IV-[-".$IV, "", $text);
- return rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_CBC, $IV), "\0");
- }
3.C# Encryption/Decryption Code
As I said before, C# doesn't accept keys that aren't 32 bytes long - it will throw an error. Also, many people get tricked here because of the encoding (most of the times you have to use Encoding.Default).
Encryption
- public static string EncryptMessage(byte[] text, string key)
- {
- RijndaelManaged aes = new RijndaelManaged();
- aes.KeySize = 256;
- aes.BlockSize = 256;
- aes.Padding = PaddingMode.Zeros;
- aes.Mode = CipherMode.CBC;
- aes.Key = Encoding.Default.GetBytes(key);
- aes.GenerateIV();
- string IV = ("-[--IV-[-" + Encoding.Default.GetString(aes.IV));
- ICryptoTransform AESEncrypt = aes.CreateEncryptor(aes.Key, aes.IV);
- byte[] buffer = text;
- return
- Convert.ToBase64String(Encoding.Default.GetBytes(Encoding.Default.GetString(AESEncrypt.TransformFinalBlock(buffer, 0, buffer.Length)) + IV));
- }
Decryption
- public static string DecryptMessage(string text, string key)
- {
- RijndaelManaged aes = new RijndaelManaged();
- aes.KeySize = 256;
- aes.BlockSize = 256;
- aes.Padding = PaddingMode.Zeros;
- aes.Mode = CipherMode.CBC;
- aes.Key = Encoding.Default.GetBytes(key);
- text = Encoding.Default.GetString(Convert.FromBase64String(text));
- string IV = text;
- IV = IV.Substring(IV.IndexOf("-[--IV-[-") + 9);
- text = text.Replace("-[--IV-[-" + IV, "");
- text = Convert.ToBase64String(Encoding.Default.GetBytes(text));
- aes.IV = Encoding.Default.GetBytes(IV);
- ICryptoTransform AESDecrypt = aes.CreateDecryptor(aes.Key, aes.IV);
- byte[] buffer = Convert.FromBase64String(text);
- return Encoding.Default.GetString(AESDecrypt.TransformFinalBlock(buffer, 0, buffer.Length));
- }
C#/PHP Compatible Encryption (AES256) ZZ的更多相关文章
- 提供openssl -aes-256-cbc兼容加密/解密的简单python函数
原文链接:http://joelinoff.com/blog/?p=885 这里的示例显示了如何使用python以与openssl aes-256-cbc完全兼容的方式加密和解密数据.它是基于我在本网 ...
- [转载] TLS协议分析 与 现代加密通信协议设计
https://blog.helong.info/blog/2015/09/06/tls-protocol-analysis-and-crypto-protocol-design/?from=time ...
- Lync 2013安装中遇到的关于SQL Mirroring的一次报错的解决
Problem Description ================= Following the Lync Deployment Wizard to setup Database Mirrori ...
- vyos User Guide
vyos User Guide 来源 https://wiki.vyos.net/wiki/User_Guide The VyOS User Guide is focused on providing ...
- TLS协议分析
TLS协议分析 本文目标: 学习鉴赏TLS协议的设计,透彻理解原理和重点细节 跟进一下密码学应用领域的历史和进展 整理现代加密通信协议设计的一般思路 本文有门槛,读者需要对现代密码学有清晰而系统的理解 ...
- Corosync 配置描述
NAME corosync.conf - corosync executive configuration file SYNOPSIS /etc/corosync/corosync.conf DESC ...
- Cisco asa组建IPSEC for ikev1
IPSec的实现主要由两个阶段来完成:--第一阶段,双方协商安全连接,建立一个已通过身份鉴别和安全保护的通道.--第二阶段,安全协议用于保护数据的和信息的交换. IPSec有两个安全协议:AH和ESP ...
- 设置 cipher suite
https://man.openbsd.org/SSL_CTX_set_cipher_list.3#ECDHE SSL_CTX_set_cipher_list() sets the list of a ...
- 【原创】大叔经验分享(41)hdfs开启kerberos之后报错Encryption type AES256 CTS mode with HMAC SHA1-96 is not supported/enabled
hdfs开启kerberos之后,namenode报错,连不上journalnode 2019-03-15 18:54:46,504 WARN org.apache.hadoop.security.U ...
随机推荐
- Asp.net笔记(原创)
Repeater控件的使用:<asp:Label ID="Label1" runat="server" Text='<%#Eval("na ...
- O-C相关-07-@property关键字简介与使用
基本概念:在O-C中,创建完类之后还需要给一个类添加属性和方法,之前说过的set和get方法比较繁琐,因此引入了@property 这个编译器指令.@property 是一个编译器指令.所谓的编译器指 ...
- Linux网络应用编程之交换机概述
Packet Tracer入门 一,交换机概况 交换机工作在OSI(开放系统互联参考模型)数据链路层,接入交换机的任意两个网络节点(网络设备)都是独享带宽的. 二,交换机原理 交换机拥有一条很高带宽的 ...
- 网站开发常用jQuery插件总结(六)关键词说明插件cluetip
我们开发的网站,总有它一定的用途.如企业站用来宣传企业或展示产品,技术站用来分享自己的思路和经验.既然网站有了它的用途,那么就拥有了它本身的关键词(关键词说明网站的主要内容).例如企业站的关键词大部分 ...
- phpcms V9 添加模块(转)
转自:http://www.cnblogs.com/Braveliu/p/5101345.html 为phpcms创建一个模块的开发流程 [1]创建模块目录 通过前面的学习,我们已经知道phpcms ...
- 【转】成为Java顶尖程序员 ,看这11本书就够了
成为Java顶尖程序员 ,看这11本书就够了 转自:http://developer.51cto.com/art/201512/503095.htm 以下是我推荐给Java开发者们的一些值得一看的好书 ...
- VC菜菜鸟:建立第一个基于Visual C++的Windows窗口程序
建立第一个基于VisualC++的Windows窗口程序: 发表于:http://blog.csdn.net/it1988888/article/details/10306585 a)执行命令:新建 ...
- QT5.4 计算器程序 打包&发布,解决dll的最新解决方案
QT写界面还是很不错,就是打包会比较麻烦,折腾了一天总算是打包完成了. QT软件的打包发布一个难点是必备dll文件的识别,现在高版本QT自带了一个windeployqt工具,直接会把需要的dll生成一 ...
- Visual Studio 内置快速生产代码简写集合
工作之余,整理了一下,Visual Studio 里面的快速生产代码缩写集合,这个拿出来分享想一下,希望对您有所帮助. 文件下载地址:VS内置生产代码缩写集合文档.rar 首字母 简写 生成代码 a ...
- socket.io
http://www.cnblogs.com/fullhouse/archive/2011/07/18/2109936.html http://www.cnblogs.com/fullhouse/ar ...