Created
December 21, 2010 16:34
-
-
Save Eugeny/750171 to your computer and use it in GitHub Desktop.
This file contains hidden or 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.Collections; | |
using System.Collections.Generic; | |
using System.Text; | |
using System.Drawing; | |
using com.google.zxing.qrcode; | |
using com.google.zxing.common; | |
using com.google.zxing; | |
using com.google.zxing.qrcode.encoder; | |
using com.google.zxing.qrcode.decoder; | |
using System.Security.Cryptography; | |
using System.IO; | |
using System.Web; | |
namespace QRGenerator | |
{ | |
public static class QR | |
{ | |
public static Bitmap Generate( | |
string urlToEncode, | |
int width, | |
int height, | |
string userID, | |
Hashtable urlParams, | |
string encryptionKey) | |
{ | |
string data = urlToEncode + (urlToEncode.Contains("?") ? "&" : "?") + "userId=" + userID; | |
string enc = ""; | |
foreach (string key in urlParams.Keys) | |
{ | |
string value = (string)urlParams[key]; | |
enc+= "&" + key + "=" + HttpUtility.UrlEncode(value); | |
} | |
if (encryptionKey != null) | |
enc= HttpUtility.UrlEncode(Encrypt(data, encryptionKey)); | |
data += "&enc=" + enc; | |
QRCode qr = new QRCode(); | |
com.google.zxing.qrcode.encoder.Encoder.encode(data, ErrorCorrectionLevel.M, qr); | |
ByteMatrix bm = qr.Matrix; | |
Bitmap bmp = new Bitmap(width, height); | |
Graphics g = Graphics.FromImage(bmp); | |
int cellSizeX = width / bm.Width; | |
int cellSizeY = height / bm.Height; | |
for (int x = 0; x < bm.Width; x++) | |
for (int y = 0; y < bm.Height; y++) | |
if (bm.Array[y][x] == 1) | |
g.FillRectangle(Brushes.Black, x * cellSizeX, y * cellSizeY, cellSizeX, cellSizeY); | |
return bmp; | |
} | |
public static string Encrypt(string original, string key) | |
{ | |
byte[] data = Encoding.UTF8.GetBytes(original); | |
DES des = new DESCryptoServiceProvider(); | |
MemoryStream ms = new MemoryStream(); | |
CryptoStream encStream = new CryptoStream( | |
ms, | |
des.CreateEncryptor(Encoding.UTF8.GetBytes(key), IV), | |
CryptoStreamMode.Write | |
); | |
encStream.Write(data, 0, data.Length); | |
encStream.Close(); | |
return System.Convert.ToBase64String(ms.ToArray()); | |
} | |
public static string Decrypt(string crypted, string key) | |
{ | |
MemoryStream ms = new MemoryStream(System.Convert.FromBase64String(crypted)); | |
byte[] data = new byte[1024]; | |
DES des = new DESCryptoServiceProvider(); | |
CryptoStream decStream = new CryptoStream( | |
ms, | |
des.CreateDecryptor(Encoding.UTF8.GetBytes(key), IV), | |
CryptoStreamMode.Read | |
); | |
int l = decStream.Read(data, 0, data.Length); | |
decStream.Close(); | |
return Encoding.UTF8.GetString(data, 0, l); | |
} | |
static byte[] IV = ASCIIEncoding.ASCII.GetBytes("SmallKey"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment