Click here to Skip to main content
15,884,388 members
Articles / Web Development / ASP.NET

Encryption and compression, native and managed

Rate me:
Please Sign up or sign in to vote.
4.74/5 (17 votes)
29 Jun 2012CPOL7 min read 111.1K   6.9K   92  
DLL for native encryption and compression (using Crypto++). Includes RSA Key Generator in C#, and encryption and compression in ASP.NET (C#).
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NETPCryptLib;
using System.Text;

namespace NetWebTest
{
    public partial class NetTestForm : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int izip = GetLineParam("Zip");
            int ienc = GetLineParam("Enc");
            int iout = GetLineParam("Out");

            string strpost = GetPost(Request);
            if (strpost == "") return;

            string strdec = "Added-" + Encode(false, strpost, izip, ienc, iout) + "-In-WebSite";

            String strenc = Encode(true, strdec, izip, ienc, iout);

            Response.Write(strenc);
            Response.End();
        }
        private string Encode(bool encode, string str, int izip, int ienc, int iout)
        {
            CGlobalKeys gkeys = new CGlobalKeys();
            CNETPInfoFormat info = gkeys.GetInfoFormat();

            byte[] btext = Encoding.ASCII.GetBytes(str);

            info.NETPBuf.optPNetzip = (TNETPEnumZip)izip;
            info.NETPBuf.optPNetencrypt = (TNETPEnumEncrypt)ienc;
            info.NETPBuf.optPNetoutput = (TNETPEnumOutput)iout;

            if (encode)
                info.NETPBuf.optPNetoper = TNETPEnumOperation.TNETPOperEncode;
            else
                info.NETPBuf.optPNetoper = TNETPEnumOperation.TNETPOperDecode;

            btext.CopyTo(info.NETPBuf.NETPbuffer, 0);
            info.NETPBuf.NETPbufferlen = btext.Length;
            info.OpClose();

            String strres = Encoding.ASCII.GetString(info.NETPBuf.NETPbuffer, 0, info.NETPBuf.NETPbufferlen);

            return strres;
        }
        private string GetPost(HttpRequest request)
        {
            System.IO.Stream body = request.InputStream;
            System.Text.Encoding encoding = request.ContentEncoding;
            System.IO.StreamReader reader = new System.IO.StreamReader(body, encoding);
            return reader.ReadToEnd();
        }
        private int GetLineParam(string param)
        {
            if (Request.Params.Get(param) != null)
                return int.Parse(Request.Params.Get(param));
            else
                return 0;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Argentina Argentina
System developer from Argentina.

Programmed in VB 5,6,.NET, C#, Java, PL-SQL, Transac-SQL, C, C++ and even some "calculator" language.

Love to build small, useful applications.
Usually building big and complicated apps based on solid, reliable components.

Hobbies: reading, photography, chess, paddle, running.

Comments and Discussions