Friday, November 14, 2008

PBE - Eveet yine hiç anlamadığım konuda iki satır yazdım hoşuma gitti

PBE password based encryption demek. bu ne demek şifreli şifreleme metodu. yani "abc" diye bi stringimiz var biz bunu şifrelemek istiyoruz ve kendimiza ait bi şifreyle bunu yapmak istiyoruz. mesele "123" e göre şifrelencek. neyse daha önceki yazımda jasypt ve javasından bahsetmiştim. decrytpinide yaptım koyim bi yerde dursun dedim hoşuma gitti şukadarcık kod cirlop gibide çalışıo.


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

namespace StandardPBEStringEncryptor
{
public static class CryptoLib
{
public static string EncryptId(string ID)
{
string result = "";
ID = "abc" + ID + "xyz";
byte[] salt = new byte[8];

RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
rng.GetBytes(salt);

System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
PKCSKeyGenerator kp = new PKCSKeyGenerator();
ICryptoTransform crypt = kp.Generate(
"kunteper",
salt,
1000,
1);

byte[] sonuc = crypt.TransformFinalBlock(encoding.GetBytes(ID), 0, encoding.GetBytes(ID).Length);

foreach (byte item in salt)
{
result += item.ToString("X2");
}
foreach (byte item in sonuc)
{
result += item.ToString("X2");
}
return result;
}
public static byte[] fromHexadecimal(String message)
{
if (message == null)
{
return null;
}
if ((message.Length % 2) != 0)
{
throw new Exception("bu iş olmaz mesaj bozuk");
}
try
{
byte[] result = new byte[message.Length / 2];
for (int i = 0; i < message.Length; i = i + 2)
{
int first = Convert.ToInt32("" + message[i], 16);
int second = Convert.ToInt32("" + message[i + 1], 16);
result[i / 2] = (byte)(0x0 + ((first & 0xff) << 4) + (second & 0xff));
}
return result;
}
catch (Exception e)
{
throw e;
}
}
public static string DecryptId(string ID)
{
byte[] pnlContent = fromHexadecimal(ID);
byte[] salt = new byte[8];
byte[] encryptedMessageKernel = new byte[24];
Array.Copy(pnlContent, salt, 8);
Array.Copy(pnlContent, 8, encryptedMessageKernel, 0, 24);

PKCSKeyGenerator kp = new PKCSKeyGenerator();
ICryptoTransform crypt = kp.Generate(
"kunteper"
, salt
, 1000
, 1);

byte[] sonuc = kp.Decryptor.TransformFinalBlock(encryptedMessageKernel, 0, encryptedMessageKernel.Length);
return Encoding.ASCII.GetString(sonuc).Replace("abc","").Replace("xyz","");
}
}

public class PKCSKeyGenerator
{
byte[] key = new byte[8], iv = new byte[8];
DESCryptoServiceProvider des = new DESCryptoServiceProvider();

public byte[] Key { get { return key; } }
public byte[] IV { get { return iv; } }
public ICryptoTransform Encryptor { get { return des.CreateEncryptor(key, iv); } }
public ICryptoTransform Decryptor { get { return des.CreateDecryptor(key, iv); } }

public PKCSKeyGenerator() { }
public PKCSKeyGenerator(String keystring, byte[] salt, int md5iterations, int segments)
{
Generate(keystring, salt, md5iterations, segments);
}

public ICryptoTransform Generate(String keystring, byte[] salt, int md5iterations, int segments)
{
int HASHLENGTH = 16;
byte[] keymaterial = new byte[HASHLENGTH * segments];
byte[] psbytes;
psbytes = Encoding.UTF8.GetBytes(keystring);

byte[] data00 = new byte[psbytes.Length + salt.Length];
Array.Copy(psbytes, data00, psbytes.Length);
Array.Copy(salt, 0, data00, psbytes.Length, salt.Length);

MD5 md5 = new MD5CryptoServiceProvider();
byte[] result = null;
byte[] hashtarget = new byte[HASHLENGTH + data00.Length];
for (int j = 0; j < segments; j++)
{
if (j == 0) result = data00;
else
{
Array.Copy(result, hashtarget, result.Length);
Array.Copy(data00, 0, hashtarget, result.Length, data00.Length);
result = hashtarget;
}

for (int i = 0; i < md5iterations; i++)
result = md5.ComputeHash(result);

Array.Copy(result, 0, keymaterial, j * HASHLENGTH, result.Length);
}

Array.Copy(keymaterial, 0, key, 0, 8);
Array.Copy(keymaterial, 8, iv, 0, 8);

return Encryptor;
}
}
}



biliyorum kırılır belkide kıranınız vardır bana fark etmez :) ben sevdim bu olayı.

sanırım bende çok okumadım olay bu pkcs#5

No comments:

apple iphone 16 return in UK Glasgow

 After I saw that WhatsApp desktop is not working(I tried 2 different laptops, uninstalled or resetting the app but did not work) and uBlock...