Delphi Dünyası Facebook'ta

Kodbank İndir

! CODEBANK 2012 !

İNDİRMEK&DETAYLI BİLGİ ALMAK İÇİN BURAYI TIKLAYINIZ.

Gönderen Konu: querystring i şifrelemek  (Okunma sayısı 2101 defa)

0 Üye ve 1 Ziyaretçi konuyu incelemekte.

Çevrimdışı sairadam

  • Delphi 1 Level 3
  • ***
  • İleti: 28
  • Rep: +0/-0
  • Cinsiyet: Bay
    • Sair Adam
querystring i şifrelemek
« : 15 Kasım 2008 23:30:51 »
merhabalar. malum querystringlerimiz adres çubugunda görülebiliyor. Bunu gözlerden gizlemek için nasıl şifreleriz. Webde epey bir araştırdım. bir kaç yoldan bahsediliyor. bana en mantıklı gelen App_Code klasörüne eklenen bir class dosyasındaki fonksiyon ile şifreleyip deşifreleme oldu. Ancak bunu delphiye entegre etme noktasında sıkıntım var. :D. Hem vakit darlığı hemde c# ile mesafeli arkadaşlığım sebebiyle sizlere sormak istedim. Çözüm üretenlere şimdiden teşekkürler.
Kod: [Seç]
#region Using

using System;
using System.IO;
using System.Web;
using System.Text;
using System.Security.Cryptography;

#endregion

/// <summary>
/// Summary description for QueryStringModule
/// </summary>
public class QueryStringModule : IHttpModule
{

  #region IHttpModule Members

  public void Dispose()
  {
    // Nothing to dispose
  }

  public void Init(HttpApplication context)
  {
    context.BeginRequest += new EventHandler(context_BeginRequest);
  }

  #endregion

  private const string PARAMETER_NAME = "enc=";
  private const string ENCRYPTION_KEY = "key";

  void context_BeginRequest(object sender, EventArgs e)
  {
    HttpContext context = HttpContext.Current;
    if (context.Request.Url.OriginalString.Contains("aspx") && context.Request.RawUrl.Contains("?"))
    {
      string query = ExtractQuery(context.Request.RawUrl);
      string path = GetVirtualPath();

      if (query.StartsWith(PARAMETER_NAME, StringComparison.OrdinalIgnoreCase))
      {
        // Decrypts the query string and rewrites the path.
        string rawQuery = query.Replace(PARAMETER_NAME, string.Empty);
        string decryptedQuery = Decrypt(rawQuery);
        context.RewritePath(path, string.Empty, decryptedQuery);
      }
      else if (context.Request.HttpMethod == "GET")
      {
        // Encrypt the query string and redirects to the encrypted URL.
        // Remove if you don't want all query strings to be encrypted automatically.
        string encryptedQuery = Encrypt(query);
        context.Response.Redirect(path + encryptedQuery);
      }
    }
  }

  /// <summary>
  /// Parses the current URL and extracts the virtual path without query string.
  /// </summary>
  /// <returns>The virtual path of the current URL.</returns>
  private static string GetVirtualPath()
  {
    string path = HttpContext.Current.Request.RawUrl;
    path = path.Substring(0, path.IndexOf("?"));
    path = path.Substring(path.LastIndexOf("/") + 1);
    return path;
  }

  /// <summary>
  /// Parses a URL and returns the query string.
  /// </summary>
  /// <param name="url">The URL to parse.</param>
  /// <returns>The query string without the question mark.</returns>
  private static string ExtractQuery(string url)
  {
    int index = url.IndexOf("?") + 1;
    return url.Substring(index);
  }

  #region Encryption/decryption

  /// <summary>
  /// The salt value used to strengthen the encryption.
  /// </summary>
  private readonly static byte[] SALT = Encoding.ASCII.GetBytes(ENCRYPTION_KEY.Length.ToString());

  /// <summary>
  /// Encrypts any string using the Rijndael algorithm.
  /// </summary>
  /// <param name="inputText">The string to encrypt.</param>
  /// <returns>A Base64 encrypted string.</returns>
  public static string Encrypt(string inputText)
  {
    RijndaelManaged rijndaelCipher = new RijndaelManaged();
    byte[] plainText = Encoding.Unicode.GetBytes(inputText);
    PasswordDeriveBytes SecretKey = new PasswordDeriveBytes(ENCRYPTION_KEY, SALT);

    using (ICryptoTransform encryptor = rijndaelCipher.CreateEncryptor(SecretKey.GetBytes(32), SecretKey.GetBytes(16)))
    {
      using (MemoryStream memoryStream = new MemoryStream())
      {
        using (CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
        {
          cryptoStream.Write(plainText, 0, plainText.Length);
          cryptoStream.FlushFinalBlock();
          return "?" + PARAMETER_NAME + Convert.ToBase64String(memoryStream.ToArray());
        }
      }
    }
  }

  /// <summary>
  /// Decrypts a previously encrypted string.
  /// </summary>
  /// <param name="inputText">The encrypted string to decrypt.</param>
  /// <returns>A decrypted string.</returns>
  public static string Decrypt(string inputText)
  {
    RijndaelManaged rijndaelCipher = new RijndaelManaged();
    byte[] encryptedData = Convert.FromBase64String(inputText);
    PasswordDeriveBytes secretKey = new PasswordDeriveBytes(ENCRYPTION_KEY, SALT);

    using (ICryptoTransform decryptor = rijndaelCipher.CreateDecryptor(secretKey.GetBytes(32), secretKey.GetBytes(16)))
    {
      using (MemoryStream memoryStream = new MemoryStream(encryptedData))
      {
        using (CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
        {
          byte[] plainText = new byte[encryptedData.Length];
          int decryptedCount = cryptoStream.Read(plainText, 0, plainText.Length);
          return Encoding.Unicode.GetString(plainText, 0, decryptedCount);
        }
      }
    }
  }

  #endregion

}
« Son Düzenleme: 15 Kasım 2008 23:53:52 by sairadam »
Hobim kod yazmak sevdamsa sensin.
Haydi bir el atında şu neşem düzelsin.

Çevrimdışı Kocaturk

  • Administrator
  • *****
  • İleti: 2.474
  • Rep: +56/-5
  • Cinsiyet: Bay
    • Delphi Dünyası
Ynt: querystring i şifrelemek
« Yanıtla #1 : 17 Kasım 2008 01:50:22 »
Kod:  (Unknown Language)
  1. uses System.IO, System.Web, System.Text, System.Security.Cryptography;
  2.  
  3. type
  4.   TArrayOfByte = array of Byte;
  5.  
  6.   QueryStringModule = class(IHttpModule)
  7.   strict private
  8.     PARAMETER_NAME: string;
  9.     ENCRYPTION_KEY: string;
  10.     SALT: TArrayOfByte;
  11.   public
  12.     procedure Dispose;
  13.     procedure Init(context: HttpApplication);
  14.   strict private
  15.     procedure context_BeginRequest(sender: System.Object; e: EventArgs);
  16.     class function GetVirtualPath: string;static;
  17.     class function ExtractQuery(url: string): string;static;
  18.   public
  19.     class function Encrypt(inputText: string): string;static;
  20.     class function Decrypt(inputText: string): string;static;
  21.   end;
  22.  
  23. implementation
  24.  
  25. {$AUTOBOX ON}
  26. {$HINTS OFF}
  27. {$WARNINGS OFF}
  28.  
  29. procedure QueryStringModule.Dispose;
  30. begin
  31. end;
  32.  
  33. procedure QueryStringModule.Init(context: HttpApplication);
  34. begin
  35.   Include(context.BeginRequest, context_BeginRequest);
  36. end;
  37.  
  38. procedure QueryStringModule.context_BeginRequest(sender: System.Object; e: EventArgs);
  39. var
  40.   encryptedQuery: string;
  41.   decryptedQuery: string;
  42.   rawQuery: string;
  43.   path: string;
  44.   query: string;
  45.   context: HttpContext;
  46. begin
  47.   context := HttpContext.Current;
  48.   if (context.Request.Url.OriginalString.Contains('aspx') and context.Request.RawUrl.Contains('?')) then
  49.   begin
  50.     query := ExtractQuery(context.Request.RawUrl);
  51.     path := GetVirtualPath;
  52.     if query.StartsWith(PARAMETER_NAME, StringComparison.OrdinalIgnoreCase) then
  53.     begin
  54.       rawQuery := query.Replace(PARAMETER_NAME, Empty);
  55.       decryptedQuery := Decrypt(rawQuery);
  56.       context.RewritePath(path, Empty, decryptedQuery);
  57.     end
  58.     else
  59.       if (context.Request.HttpMethod = 'GET') then
  60.       begin
  61.         encryptedQuery := Encrypt(query);
  62.         context.Response.Redirect((path + encryptedQuery));
  63.       end;
  64.   end;
  65. end;
  66.  
  67. class function QueryStringModule.GetVirtualPath: string;
  68. var
  69.   path: string;
  70. begin
  71.   path := HttpContext.Current.Request.RawUrl;
  72.   path := path.Substring(0, path.IndexOf('?'));
  73.   path := path.Substring((path.LastIndexOf('/') + 1));
  74.   Result := path;
  75. end;
  76.  
  77. class function QueryStringModule.ExtractQuery(url: string): string;
  78. var
  79.   index: Integer;
  80. begin
  81.   index := (url.IndexOf('?') + 1);
  82.   Result := url.Substring(index);
  83. end;
  84.  
  85. class function QueryStringModule.Encrypt(inputText: string): string;
  86. var
  87.   SecretKey: PasswordDeriveBytes;
  88.   plainText: TArrayOfByte;
  89.   rijndaelCipher: RijndaelManaged;
  90. begin
  91.   rijndaelCipher := RijndaelManaged.Create;
  92.   plainText := Encoding.Unicode.GetBytes(inputText);
  93.   SecretKey := PasswordDeriveBytes.Create(ENCRYPTION_KEY, SALT);
  94. end;
  95.  
  96. class function QueryStringModule.Decrypt(inputText: string): string;
  97. var
  98.   secretKey: PasswordDeriveBytes;
  99.   encryptedData: TArrayOfByte;
  100.   rijndaelCipher: RijndaelManaged;
  101. begin
  102.   rijndaelCipher := RijndaelManaged.Create;
  103.   encryptedData := Convert.FromBase64String(inputText);
  104.   secretKey := PasswordDeriveBytes.Create(ENCRYPTION_KEY, SALT);
  105. end;

Bunun için babelcode u kullanabilirsin, kodu tekrar gözden geçirmek gerekebilir.

Çevrimdışı sairadam

  • Delphi 1 Level 3
  • ***
  • İleti: 28
  • Rep: +0/-0
  • Cinsiyet: Bay
    • Sair Adam
Ynt: querystring i şifrelemek
« Yanıtla #2 : 17 Kasım 2008 15:27:29 »
Bu kodu uyarlamak v.s. uğraşak vaktim yok. Ancak bu mesele çok önemli bir konu. Ben kendimce iki fonksiyonla olayı çözdüm. Bu fonksiyonları yazarken bir kaç koddan esinlendim ama sonuçta iş gören iki fonksiyon çıktı. benim hazırladığım proje çok dilli olduğu için unicode kullanmam gerekti. siz kendinize göre bu kodlama sistemini değiştirebilirsiniz. Uses a System.Security.Cryptography,System.Text iekliyoruz. Ve fonksiyonlarimiz calisiyor :) kodları aşağıdadır.
Kod: [Seç]
function fonks.sifrele(const yazi: string): string;
var
   alg:SHA1Managed;
   sifrelenen : array of Byte;
   deger:string;
begin
   alg:=SHA1Managed.Create;
   Convert.ToBase64String(alg.ComputeHash(Encoding.Unicode.GetBytes(yazi)));
   sifrelenen:=UnicodeEncoding.UTF8.GetBytes(yazi);
   deger:=Convert.ToBase64String(sifrelenen);
 Result := deger;
end

Kod: [Seç]
function fonks.sifrecoz(const yazi: string): string;
var
   sifrelenen: array of Byte;
   deger:string;
begin
   sifrelenen:=Convert.FromBase64String(yazi);
   deger:=UnicodeEncoding.UTF8.GetString(sifrelenen);
   Result := deger;
end
Hobim kod yazmak sevdamsa sensin.
Haydi bir el atında şu neşem düzelsin.