ویرگول
ورودثبت نام
aminjavaheri.ir
aminjavaheri.ir
خواندن ۳ دقیقه·۱ سال پیش

C# CAPTCHA

کپچا چیست؟

کلمه کبچا یا CAPTCHA مخفف جمله Completely Automated Public Turing Test To Tell Computers and Humans Apart بوده و به معنی “آزمایش اتوماتیک عمومی برای تشخیص انسان از کامپیوتر” می باشد. کپچا در سال 2000 و در دانشگاه Carneige Mellon توسط Luis von Ahn و همکارانش ساخته شد و برای اولین بار در سایت Yahoo مورد استفاده قرار گرفت.

بیشترین مورد استفاده از کپچا برای جلوگیری از ارسال درخواست های زیاد توسط ربات ها به سمت سرور ها است.به عنوان مثال سایت های زیادی در دنیا خدمات رایگان ارائه میدهند که نیازمند عضویت و ساخت اکانت از طرف افراد هستند. مهمترین آنها سیستم های ایمیل یاهو و گوگل هستند. تا چند سال پیش و قبل از بکارگیری کپچا بزرگترین مشکل آنها ساخت تعداد زیادی اکانت توسط روبات های اینترنتی بود که میتوانستند در چند دقیقه صدها ایمیل بسازند. استفاده از سیستم کپچا این امکان را فراهم کرد که تنها انسان ها قادر به تکمیل فرم و ثبت نام نهایی باشند و امروزه به یک ضرورت در سیستم های رایگان تبدیل شده است.


هدف از این مقاله

هدف از نوشتن این متن تنها آموزش ساخت یک کد تصویری برای استفاده در کپچا است که این کد توسط زبان C# قابل اجرا می باشد.

int iHeight = 80;
int iWidth = context.Request.QueryString[&quotiWidth&quot] != null ? int.Parse(context.Request.QueryString[&quotUID&quot]) : 190;
Random oRandom = new Random();
int[] aBackgroundNoiseColor = new int[] { 150, 150, 150 };
int[] aTextColor = new int[] { 0, 0, 0 };
int[] aFontEmSizes = new int[] { 15, 20, 25, 30, 35 };
string[] aFontNames = new string[]
{
&quotComic Sans MS&quot,
&quotArial&quot,
&quotTimes New Roman&quot,
&quotGeorgia&quot,
&quotVerdana&quot,
&quotGeneva&quot
};
FontStyle[] aFontStyles = new FontStyle[]
{
FontStyle.Bold,
FontStyle.Italic,
FontStyle.Regular,
FontStyle.Strikeout,
FontStyle.Underline
};
HatchStyle[] aHatchStyles = new HatchStyle[]
{
HatchStyle.BackwardDiagonal, HatchStyle.Cross,
HatchStyle.DashedDownwardDiagonal, HatchStyle.DashedHorizontal,
HatchStyle.DashedUpwardDiagonal, HatchStyle.DashedVertical,
HatchStyle.DiagonalBrick, HatchStyle.DiagonalCross,
HatchStyle.Divot, HatchStyle.DottedDiamond, HatchStyle.DottedGrid,
HatchStyle.ForwardDiagonal, HatchStyle.Horizontal,
HatchStyle.HorizontalBrick, HatchStyle.LargeCheckerBoard,
HatchStyle.LargeConfetti, HatchStyle.LargeGrid,
HatchStyle.LightDownwardDiagonal, HatchStyle.LightHorizontal,
HatchStyle.LightUpwardDiagonal, HatchStyle.LightVertical,
HatchStyle.Max, HatchStyle.Min, HatchStyle.NarrowHorizontal,
HatchStyle.NarrowVertical, HatchStyle.OutlinedDiamond,
HatchStyle.Plaid, HatchStyle.Shingle, HatchStyle.SmallCheckerBoard,
HatchStyle.SmallConfetti, HatchStyle.SmallGrid,
HatchStyle.SolidDiamond, HatchStyle.Sphere, HatchStyle.Trellis,
HatchStyle.Vertical, HatchStyle.Wave, HatchStyle.Weave,
HatchStyle.WideDownwardDiagonal, HatchStyle.WideUpwardDiagonal, HatchStyle.ZigZag
};
//Get Captcha in Session
int iNumber = oRandom.Next(100000, 999999);
context.Session[&quotCaptcha&quot] = iNumber.ToString();
string sCaptchaText = context.Session[&quotCaptcha&quot].ToString();
//Creates an output Bitmap
Bitmap oOutputBitmap = new Bitmap(iWidth, iHeight, PixelFormat.Format24bppRgb);
Graphics oGraphics = Graphics.FromImage(oOutputBitmap);
oGraphics.TextRenderingHint = TextRenderingHint.AntiAlias;
//Create a Drawing area
RectangleF oRectangleF = new RectangleF(0, 0, iWidth, iHeight);
Brush oBrush = default(Brush);
//Draw background (Lighter colors RGB 100 to 255)
oBrush = new HatchBrush(aHatchStyles[oRandom.Next
(aHatchStyles.Length - 1)], Color.FromArgb((oRandom.Next(100, 255)),
(oRandom.Next(100, 255)), (oRandom.Next(100, 255))), Color.White);
oGraphics.FillRectangle(oBrush, oRectangleF);
System.Drawing.Drawing2D.Matrix oMatrix = new System.Drawing.Drawing2D.Matrix();
int i = 0;
for (i = 0; i <= sCaptchaText.Length - 1; i++)
{
oMatrix.Reset();
int iChars = sCaptchaText.Length;
int x = iWidth / (iChars + 1) * i;
int y = iHeight / 2;
//Rotate text Random
oMatrix.RotateAt(oRandom.Next(-40, 40), new PointF(x, y));
oGraphics.Transform = oMatrix;
//Draw the letters with Random Font Type, Size and Color
oGraphics.DrawString
(
//Text
sCaptchaText.Substring(i, 1),
//Random Font Name and Style
new Font(aFontNames[oRandom.Next(aFontNames.Length - 1)],
aFontEmSizes[oRandom.Next(aFontEmSizes.Length - 1)],
aFontStyles[oRandom.Next(aFontStyles.Length - 1)]),
//Random Color (Darker colors RGB 0 to 100)
new SolidBrush(Color.FromArgb(oRandom.Next(0, 100),
oRandom.Next(0, 100), oRandom.Next(0, 100))),
x,
oRandom.Next(10, 40)
);
oGraphics.ResetTransform();
}
MemoryStream oMemoryStream = new MemoryStream();
oOutputBitmap.Save(oMemoryStream, System.Drawing.Imaging.ImageFormat.Png);
byte[] oBytes = oMemoryStream.GetBuffer();
oOutputBitmap.Dispose();
oMemoryStream.Close();
context.Response.ContentType = &quotimage/png"
context.Response.BinaryWrite(oBytes);
context.Response.End();


captchaکپچاc
کارشناس پدافند غیر عامل ، مدیریت پروژه ، برنامه نویس، پیاده سازی و تحلیلگری بانک های اطلاعاتی ساختاری و غیر ساختاری ،
شاید از این پست‌ها خوشتان بیاید