Created
June 11, 2020 05:08
-
-
Save httpspace/ab3fb1ac73024d6e7776442c72c2a69d to your computer and use it in GitHub Desktop.
qrcode加密範例
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Text; | |
using System.Security.Cryptography; | |
using System.Web; | |
namespace Rextester | |
{ | |
public class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
String mlResult = ""; | |
byte[] mlAesEncDataByte = null; | |
byte[] mlKeyByte = HexStringToByteArray("5E8B6E1998F421204C6576544FE1A26B44FC775982D8CE2E");//加密基碼 | |
byte[] mlIcvByte = Encoding.GetEncoding("utf-8").GetBytes("FOCASAPIFOCASAPI");//IV值 | |
String mlData = "TWQRP%3A%2F%2F%E6%98%9F%E4%B9%9D%E5%AE%A2%E5%92%96%E5%95%A1%2F158%2F01%2FV1%3FD1%3D12500%26D3%3DAVnVbcN9xxRv%26D10%3D901%26D11%3D00%2C00600611122233344400000001%3B01%2C00600611122233344400000001%3B04%2C00800899887766554400000001%26D12%3D20170630130000%26OprodNumber%3D%E6%8B%BF%E9%90%B5";//UTF-8 URLEncode後的加密前電文 (以台灣Pay QR Code為例) | |
byte[] mlDataByte = Encoding.GetEncoding("utf-8").GetBytes(mlData); | |
//取得電文長度10進位int -> 轉換成16進位字串 -> 右靠左補0成4碼HEX字串 -> 轉換成長度為2的byte[] | |
int mlDataLen = mlDataByte.Length; | |
String mlDataLen16 = "0000" + Convert.ToString(mlDataLen, 16); | |
byte[] mlDataLenByte = HexStringToByteArray(mlDataLen16.Substring(mlDataLen16.Length - 4, 4)); | |
//組合轉換後的準備加密資料 | |
int mlDataAllLen = mlDataLen + 2; | |
byte[] mlDataAllByte = new byte[mlDataAllLen]; | |
//前兩位放電文長度轉換成的byte[] mlDataLenByte | |
mlDataAllByte[0] = mlDataLenByte[0]; | |
mlDataAllByte[1] = mlDataLenByte[1]; | |
//之後放mlDataByte | |
for (int i = 0; i < mlDataLen; i++) | |
{ | |
mlDataAllByte[2 + i] = mlDataByte[i]; | |
} | |
//AES加密 Padding直接使用Zeros就好 不須手動填補 | |
AesCryptoServiceProvider aes = new AesCryptoServiceProvider(); | |
aes.IV = mlIcvByte; | |
aes.Key = mlKeyByte; | |
aes.Mode = CipherMode.CBC; | |
aes.Padding = PaddingMode.Zeros; | |
mlAesEncDataByte = aes.CreateEncryptor().TransformFinalBlock(mlDataAllByte, 0, mlDataAllByte.Length); | |
if (mlAesEncDataByte != null) | |
{ | |
//加密結果byte[] 在最前方再補一碼byte=加密基碼版本 0x02 | |
byte[] mlAesAllByte = new byte[mlAesEncDataByte.Length + 1]; | |
mlAesAllByte[0] = (byte)0x02; | |
for (int k = 0; k < mlAesEncDataByte.Length; k++) | |
{ | |
mlAesAllByte[1 + k] = mlAesEncDataByte[k]; | |
} | |
//Base64轉換 | |
mlResult = Convert.ToBase64String(mlAesAllByte); | |
//URLEncode | |
String encQRCode = HttpUtility.UrlEncode(mlResult); | |
//Console.WriteLine(encQRCode); | |
} | |
} | |
public static byte[] HexStringToByteArray(String piHexString) | |
{ | |
byte[] mlResult = null; | |
StringBuilder olHexStringBuilder = new StringBuilder(piHexString); | |
if (piHexString.Length % 2 == 1) //奇數位左補0 | |
{ | |
olHexStringBuilder.Insert(0, '0'); | |
} | |
byte[] olByteArray = new byte[(piHexString.Length + 1) / 2]; | |
for (int idxI = 0; idxI < olByteArray.Length; idxI++) | |
{ | |
olByteArray[idxI] = (byte)Convert.ToInt32(olHexStringBuilder.ToString(idxI * 2, 2), 16); | |
} | |
mlResult = olByteArray; | |
return mlResult; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment