萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> ASP 程式設計

ASP 生成圖形驗證碼 (CAPTCHA):ADODB.Stream 組合 BMP 圖片像素級操作

原理概覽

  • 預先準備:head.fix (BMP 檔頭 54 bytes) + body.fix (10 個數字 0-9 的 10x100 像素圖像資料,每數字 320 bytes)
  • 執行時:產生 4 位隨機數 → 從 body.fix 提取對應數字像素 → 重新排列為橫向 40x100 圖像 → 組合檔頭 → 二進位輸出
  • 無需 GD/GDI+ 元件,純 ASP + ADODB.Stream 實作

檔案結構

/captcha/
├── code.asp       # 主程式
├── head.fix       # BMP 檔頭 (54 bytes)
├── body.fix       # 10 數字垂直排列圖像資料 (3200 bytes)
└── demo.asp       # 測試頁

核心程式:code.asp

<%@ Language=VBScript CodePage=65001 %>
<%
Option Explicit
Response.Buffer = True
Response.Expires = -1
Response.AddHeader "Pragma", "no-cache"
Response.AddHeader "Cache-Control", "no-cache, must-revalidate"
Response.ContentType = "image/bmp"

Dim zNum, i, j, zImg(3), nStr, pos
Dim adoSrc, adoDst

' 1. 產生 4 位隨機數 (1000-9999)
Randomize Timer
zNum = CInt(8999 * Rnd + 1000)
Session("CaptchaCode") = CStr(zNum)  ' 驗證時比對

' 2. 拆解為陣列
nStr = CStr(zNum)
For i = 0 To 3
    zImg(i) = CInt(Mid(nStr, i+1, 1))
Next

' 3. 開啟二進位串流
Set adoSrc = Server.CreateObject("ADODB.Stream")
adoSrc.Type = 1: adoSrc.Mode = 3: adoSrc.Open
Set adoDst = Server.CreateObject("ADODB.Stream")
adoDst.Type = 1: adoDst.Mode = 3: adoDst.Open

' 4. 載入 body.fix (10 數字 x 320 bytes = 3200 bytes)
adoSrc.LoadFromFile Server.MapPath("body.fix")
' 先寫入前 4 個數字佔位 (1280 bytes),稍後覆寫
adoDst.Write adoSrc.Read(1280)

' 5. 從來源提取 4 個隨機數字的像素區塊 (垂直條狀)
For i = 0 To 3
    ' 每數字 320 bytes,索引 9-zImg(i) 因圖像倒置存儲
    adoSrc.Position = (9 - zImg(i)) * 320
    adoDst.Position = i * 320
    adoDst.Write adoSrc.Read(320)
Next

' 6. 載入檔頭
adoSrc.LoadFromFile Server.MapPath("head.fix")
pos = LenB(adoSrc.Read())  ' 54
adoSrc.Position = pos
adoDst.Position = pos

' 7. 將垂直條狀重組為橫向圖像 (轉置)
' 來源: 4 個 320-byte 區塊 (每區塊 10 行 x 32 bytes)
' 目標: 100 行 x 120 bytes (40像素 x 3bytes + 0 padding)
For i = 0 To 9  ' 10 行 (每數字高 10px)
    For j = 0 To 3  ' 4 個數字
        ' 來源: 第 j 個數字的第 i 行 (30 bytes 像素 + 2 bytes padding = 32)
        adoSrc.Position = j * 320 + i * 32
        ' 目標: 第 i 行的第 j 個數字區段
        adoDst.Position = pos + i * 120 + j * 30
        adoDst.Write adoSrc.Read(30)  ' 只寫 30 bytes 像素 (無行尾 padding)
    Next
Next

' 8. 輸出
adoDst.Position = 0
Response.BinaryWrite adoDst.Read()

' 清理
adoSrc.Close: Set adoSrc = Nothing
adoDst.Close: Set adoDst = Nothing
%>

素材準備:建立 head.fix 與 body.fix

用任何圖片編輯器 (Paint.NET、Photoshop) 建立:

  1. head.fix:40x100 像素、24-bit BMP、檔頭 54 bytes。可用空白圖產生。
  2. body.fix:10x1000 像素 (10 個數字垂直堆疊),每數字 10x100、24-bit BMP、無檔頭、純像素資料 3200 bytes。

或用 Python 腳本程式化產生:

# gen_captcha_assets.py
from PIL import Image, ImageDraw, ImageFont
import struct

# 建立 head.fix (40x100 BMP header)
img = Image.new('RGB', (40, 100), 'white')
bmp_header = b'BM' + struct.pack('

驗證端:check_captcha.asp

<%
Dim inputCode
inputCode = Trim(Request("captcha"))
If StrComp(inputCode, Session("CaptchaCode"), vbTextCompare) = 0 Then
    Response.Write "驗證通過"
Else
    Response.Write "驗證碼錯誤"
End If
%>

前端整合

<img src="code.asp" id="captchaImg" style="vertical-align:middle;cursor:pointer" 
     onclick="this.src='code.asp?'+Math.random()" title="點擊更換">
<input name="captcha" maxlength="4" size="5">

現代化建議

  • 改用 ASP.NET Core / Node.js / Python + Pillow / SkiaSharp 產生 PNG/JPEG,支援干擾線、噪點、扭曲、字體隨機
  • 前端改用 reCAPTCHA v3 / hCaptcha / Turnstile 無感驗證
  • 若必須維護經典 ASP,考慮封裝為 COM 元件 (C++/C#) 提供 GenerateCaptcha(width, height, length) 介面
copyright © 萬盛學電腦網 all rights reserved