博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C#/PHP Compatible Encryption (AES256) ZZ
阅读量:7241 次
发布时间:2019-06-29

本文共 3273 字,大约阅读时间需要 10 分钟。

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.
structure
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

  1. function encrypt($text, $pkey)
  2. {
  3. $key = $pkey;
  4. $IV = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC), MCRYPT_RAND);
  5. return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_CBC, $IV)."-[--IV-[-".$IV);
  6. }

Decryption

  1. function decrypt($text, $pkey)
  2. {
  3. $key = $pkey;
  4. $text = base64_decode($text);
  5. $IV = substr($text, strrpos($text, "-[--IV-[-") + 9);
  6. $text = str_replace("-[--IV-[-".$IV, "", $text);
  7. return rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_CBC, $IV), "\0");
  8. }

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

  1. public static string EncryptMessage(byte[] text, string key)
  2. {
  3. RijndaelManaged aes = new RijndaelManaged();
  4. aes.KeySize = 256;
  5. aes.BlockSize = 256;
  6. aes.Padding = PaddingMode.Zeros;
  7. aes.Mode = CipherMode.CBC;
  8. aes.Key = Encoding.Default.GetBytes(key);
  9. aes.GenerateIV();
  10. string IV = ("-[--IV-[-" + Encoding.Default.GetString(aes.IV));
  11. ICryptoTransform AESEncrypt = aes.CreateEncryptor(aes.Key, aes.IV);
  12. byte[] buffer = text;
  13. return
  14. Convert.ToBase64String(Encoding.Default.GetBytes(Encoding.Default.GetString(AESEncrypt.TransformFinalBlock(buffer, 0, buffer.Length)) + IV));
  15. }

 
Decryption

    1. public static string DecryptMessage(string text, string key)
    2. {
    3. RijndaelManaged aes = new RijndaelManaged();
    4. aes.KeySize = 256;
    5. aes.BlockSize = 256;
    6. aes.Padding = PaddingMode.Zeros;
    7. aes.Mode = CipherMode.CBC;
    8. aes.Key = Encoding.Default.GetBytes(key);
    9. text = Encoding.Default.GetString(Convert.FromBase64String(text));
    10. string IV = text;
    11. IV = IV.Substring(IV.IndexOf("-[--IV-[-") + 9);
    12. text = text.Replace("-[--IV-[-" + IV, "");
    13. text = Convert.ToBase64String(Encoding.Default.GetBytes(text));
    14. aes.IV = Encoding.Default.GetBytes(IV);
    15. ICryptoTransform AESDecrypt = aes.CreateDecryptor(aes.Key, aes.IV);
    16. byte[] buffer = Convert.FromBase64String(text);
    17. return Encoding.Default.GetString(AESDecrypt.TransformFinalBlock(buffer, 0, buffer.Length));
    18. }

转载地址:http://hfybm.baihongyu.com/

你可能感兴趣的文章
前端面试查漏补缺--Index篇(12万字符合集)
查看>>
大白话说java并发工具类-CountDownLatch,CyclicBarrier
查看>>
玩转 iOS 开发:Aggregate 脚本版本《模拟器与真机静态 Framework 合成教程》
查看>>
String、StringBuffer与StringBuilder
查看>>
ReactNative源码篇:启动流程
查看>>
vue-awesome-swiper 小案例
查看>>
聊聊Dubbo(四):核心源码-切入Spring
查看>>
Koa 源码浅析
查看>>
dpdk-lvs的一次线上故障排查报告
查看>>
记一次ios系统select标签第一个选项不能被选中的bug
查看>>
即构科技金健忠:回顾20年音视频技术演进
查看>>
tcpkill工作原理分析
查看>>
Kubernetes性能测试实践
查看>>
使用有限状态机原理实现英文分词
查看>>
阿里十年,只剩下这套Java开发体系了
查看>>
你的文案老是错别字,能不能改改?
查看>>
netty源码分析之服务端启动全解析
查看>>
js数组API--温故知新
查看>>
深入理解苹果系统(Unicode)字符串的排序方法
查看>>
为什么在Go语言中要慎用interface{}
查看>>