02

02/06

CSharp – Rijndael/AES symetric Encryption and Decryption

18:15 by gernot. Filed under: Coding

A short tutorial how to encrypt, respectivelly decrypt a string in C# with Rijndael/AES.

NOTE: The Keygeneration is done in the encryption and decryption methods. For usage in a ‘real’ application this should be outsourced.

Encryption

using System.Security.Cryptography;

// Input Values:
// pwdHash = “MD5″ / “SHA1″
// iv = “16 ASCII Characters”
// keySize = 128 / 192 / 256

public string Encrypt(string text, string pwdPhrase, string pwdSalt, string pwdHash,
  int pwdIterations, string iv, int keySize)
{
  byte[] ivBytes = Encoding.ASCII.GetBytes(iv);
  byte[] pwdSaltBytes = Encoding.ASCII.GetBytes(pwdSalt);
  byte[] textBytes = Encoding.UTF8.GetBytes(text);
  PasswordDeriveBytes pwd
     = new PasswordDeriveBytes (pwdPhrase, pwdSaltBytes, pwdHash, pwdIterations);
  byte[] keyBytes = pwd.GetBytes( keySize / 8 );
  RijndaelManaged symmKey = new RijndaelManaged ();
  symmKey.Mode = CipherMode.CBC;
  ICryptoTransform enc
     = symmKey.CreateEncryptor(keyBytes, ivBytes);
  MemoryStream mem = new MemoryStream ();
  CryptoStream cry
     = new CryptoStream (mem, enc, CryptoStreamMode.Write);
  cry.Write(textBytes, 0, textBytes.Length);
  cry.FlushFinalBlock();
  byte[] cipBytes = mem.ToArray();
  mem.Close();
  cry.Close();
  return Convert.ToBase64String(cipBytes);
}

Decryption

using System.Security.Cryptography;

// Input Values:
// pwdHash = “MD5″ / “SHA1″
// iv = “16 ASCII Characters”
// keySize = 128 / 192 / 256

public string Decrypt(string cip, string pwdPhrase, string pwdSalt, string pwdHash,
  int pwdIterations, string iv, int keySize)
{
  byte[] ivBytes = Encoding.ASCII.GetBytes(iv);
  byte[] pwdSaltBytes = Encoding.ASCII.GetBytes(pwdSalt);
  byte[]cipTextBytes = Convert.FromBase64String(cip);
  PasswordDeriveBytes pwd
     = new PasswordDeriveBytes (pwdPhrase, pwdSaltBytes, pwdHash, pwdIterations);
  byte[] keyBytes = pwd.GetBytes( keySize / 8 );
  RijndaelManaged symmKey = new RijndaelManaged ();
  symmKey.Mode = CipherMode.CBC;
  ICryptoTransform dec
     = symmKey.CreateDecryptor(keyBytes, ivBytes);
  MemoryStream mem = new MemoryStream (cipTextBytes);
  CryptoStream cry
     = new CryptoStream (mem, dec, CryptoStreamMode.Read);
  byte[] textBytes = new byte[cipTextBytes.Length];
  int decByteCount
     = cry.Read(textBytes, 0, textBytes.Length);
  mem.Close();
  cry.Close();
  return Encoding.UTF8.GetString(textBytes, 0, decByteCount);
}

26

01/06

CSS – Dynamic Menus

00:04 by gernot. Filed under: Coding
Tags:

Forget javaScripts or other ‘stuff’ for making dynamic Menus, its easier and better with CSS.

NOTE: For IE usage download IE Hover Hack here: csshover.htc
Here you can see a preview of dynamic menu: goTo dynamic css menu

CSS Dynamic Menu Code

/*You need to set behavior in body so IE Hack can Work*/
body {
    font-size: 62.5%;
    font-family: ‘Lucida Grande’, Verdana, Arial, Sans-Serif;
    text-align: center;     
/*IE Hover Hack*/
    behavior:url(“csshover.htc”);
}

a:hover {
    background: yellow;
}

ul {
    margin: 0;
    padding: 0;
    display: inline;
}

ul li {
    list-style-type: none;
    position: relative;
    margin: 0;
    padding: 0;
    background: #FFE;
    width:10em;
    border: 1px solid #000;
    /*First list items float left*/
    float: left;
}

ul li a {
    display: block;
    padding: 5px 7px;
    text-decoration: none;
    background: #FFF;
}

ul li ul li {
    /*Stop floating left*/
    clear: both;
}

/*Here the hovering starts*/
ul li:hover ul {
    width: 10em;
    top: 2.3em;
    left: -0.1em;
}

ul ul, li:hover ul ul {
    display:none;
    width: 10em;
}

li:hover ul {
    display:block;
    position: absolute;
    left: 100%;
}

li:hover li:hover ul {
    display:block;
    position: absolute;
    top: -0.1em;
    left: 100%;
}

25

01/06

CSS – fluid vs. fixed Design

21:21 by gernot. Filed under: Coding

Everyone knows the problem: Webpages which have too much text on too small space or vica versa. The solution is either make the page fluid or fixed, and with the help of css this is pretty simple:

fixed

body {
    margin: 0;
    padding: 0;
    background-color: #000;
    color: #fff;
}
#page {
    margin: 0 auto;
    padding: 0;
    width: 760px;
    background: #fff;
}

click here for example Here the page never changes its size.
Try and change the size of the browser window.

fluid

body {
    margin: 0;
    padding: 0;
    background-color: #000;
    color: #fff;
}
#page {
    margin: auto;
    padding: 0;
    width: 90%;
    background: #fff;
}

click here for example Here the page always changes its size to fit to the resolution or browser window
Try and change the size of the browser window.

25

01/06

CSS – Kategorie, was soll das?

12:18 by gernot. Filed under: Coding
Tags: , ,

Nachdem ich mich seit längerer Zeit ein wenig mit CSS (CascadingStyleSheets) Webseitengestaltung auseinandersetzte und selbst immer alle möglichen Dinge vergesse möchte ich in dieser Rubrik eine kleine Sammlung an nützlichen css Snippets hervortun.