Generate an Image with a Random Number

本文介绍了一种使用Visual Basic ASP.NET生成包含随机数字的验证码图片的方法,并展示了如何验证用户输入的验证码是否正确。

Introduction

We need to check if it is a real person who is registering on our Web site. So we decided to generate an image with a random code, hidden for robots, and feed it to the browser instead of a page response. We show it in our registration form, and let the user enter the code to check it.

Create the Web Project

  1. File -> New -> New project

Create a new CodeImage Visual Basic ASP.NET Web Application.

Change the Web Form

  1. Rename WebForm1.aspx to CodeImage.aspx. Open it.
  2. Switch to HTML View (Right click -> View HTML source).
  3. Remove all HTML code. Leave only the <%@ Page ... %> header.
  4. Switch to Code View (Right click -> View Code).

Change the Page_Load method with the code to generate the random code image. Also set Session("hiddenCode") to allow code checks later:

CodeImage.aspx

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="CodeImage.aspx.vb" Inherits="CodeImage" %>

CodeImage.aspx.vb

Partial Class CodeImage

    Inherits System.Web.UI.Page

 

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        'Put user code to initialize the page here

 

        ' Create a Bitmap image

        Dim ImageSrc As System.Drawing.Bitmap = New System.Drawing.Bitmap(155, 85)

 

        ' Fill it randomly with white pixels

        For iX As Integer = 0 To ImageSrc.Width - 1

            For iY As Integer = 0 To ImageSrc.Height - 1

                If Rnd() > 0.5 Then

                    ImageSrc.SetPixel(iX, iY, System.Drawing.Color.White)

                End If

            Next iY

        Next iX

 

        ' Create an ImageGraphics Graphics object from bitmap Image

        Dim ImageGraphics As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(ImageSrc)

 

        ' Generate random code.

        Dim hiddenCode As String = (Fix(Rnd() * 10000000)).ToString

        ' Set Session variable

        Session("hiddenCode") = hiddenCode

 

        ' Draw random code within Image

        Dim drawFont As New System.Drawing.Font("Arial", 20, Drawing.FontStyle.Italic)

        Dim drawBrush As New _

           System.Drawing.SolidBrush(System.Drawing.Color.Black)

        Dim x As Single = 5.0 + (Rnd() / 1) * (ImageSrc.Width - 120)

        Dim y As Single = 5.0 + (Rnd() / 1) * (ImageSrc.Height - 30)

        Dim drawFormat As New System.Drawing.StringFormat

        ImageGraphics.DrawString(hiddenCode, drawFont, drawBrush, _

            x, y, drawFormat)

 

        ' Change reponse content MIME type

        Response.ContentType = "image/jpeg"

 

        ' Sent Image using Response OutputStream

        ImageSrc.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg)

 

        ' Dispose used Objects

        drawFont.Dispose()

        drawBrush.Dispose()

        ImageGraphics.Dispose()

    End Sub

End Class

 

CodeImageDefault.aspx

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="CodeImageDefault.aspx.vb" Inherits="CodeImageDefault" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

    <title>Generate an Image with a Random Number</title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

    <img style="Z-INDEX: 101; LEFT: 152px; POSITION: absolute; TOP: 24px" height="85" alt="Hidden Code"

                   src="CodeImage.aspx" width="155"/>

              <div style="DISPLAY: inline; Z-INDEX: 102; LEFT: 88px; WIDTH: 304px; POSITION: absolute; TOP: 120px; HEIGHT: 19px"

                   ms_positioning="FlowLayout">Enter the Code in image:</div>

              <asp:TextBox id="TextBox1" style="Z-INDEX: 103; LEFT: 160px; POSITION: absolute; TOP: 144px"

                   runat="server"></asp:TextBox>

              <asp:Button id="Button1" style="Z-INDEX: 104; LEFT: 168px; POSITION: absolute; TOP: 176px" runat="server"

                   Text="Button" Width="128px" Height="32px"></asp:Button>

              <asp:Label id="Label1" style="Z-INDEX: 105; LEFT: 128px; POSITION: absolute; TOP: 224px" runat="server"

                   Width="232px" Height="32px"></asp:Label>

    </div>

    </form>

</body>

</html>

 

CodeImageDefault.aspx.vb

 

Partial Class CodeImageDefault

    Inherits System.Web.UI.Page

 

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

        If TextBox1.Text <> Session("hiddenCode") Then

            Label1.Text = "Wrong Code!"

        Else

            Label1.Text = "Correct Code!"

        End If

    End Sub

End Class

 
def add_hashtree_footer(self, image_filename, partition_size, partition_name, generate_fec, fec_num_roots, hash_algorithm, block_size, salt, chain_partitions, algorithm_name, key_path, public_key_metadata_path, rollback_index, flags, rollback_index_location, props, props_from_file, kernel_cmdlines, setup_rootfs_from_kernel, setup_as_rootfs_from_kernel, include_descriptors_from_image, calc_max_image_size, signing_helper, signing_helper_with_files, release_string, append_to_release_string, output_vbmeta_image, do_not_append_vbmeta_image, print_required_libavb_version, use_persistent_root_digest, do_not_use_ab, no_hashtree, check_at_most_once): """Implements the 'add_hashtree_footer' command. See https://gitlab.com/cryptsetup/cryptsetup/wikis/DMVerity for more information about dm-verity and these hashes. Arguments: image_filename: File to add the footer to. partition_size: Size of partition or 0 to put it right at the end. partition_name: Name of partition (without A/B suffix). generate_fec: If True, generate FEC codes. fec_num_roots: Number of roots for FEC. hash_algorithm: Hash algorithm to use. block_size: Block size to use. salt: Salt to use as a hexadecimal string or None to use /dev/urandom. chain_partitions: List of partitions to chain. algorithm_name: Name of algorithm to use. key_path: Path to key to use or None. public_key_metadata_path: Path to public key metadata or None. rollback_index: Rollback index. flags: Flags value to use in the image. rollback_index_location: Location of the main vbmeta rollback index. props: Properties to insert (List of strings of the form 'key:value'). props_from_file: Properties to insert (List of strings 'key:<path>'). kernel_cmdlines: Kernel cmdlines to insert (list of strings). setup_rootfs_from_kernel: None or file to generate dm-verity kernel cmdline from. setup_as_rootfs_from_kernel: If True, generate dm-verity kernel cmdline to set up rootfs. include_descriptors_from_image: List of file objects for which to insert descriptors from. calc_max_image_size: Don't store the hashtree or footer - instead calculate the maximum image size leaving enough room for hashtree and metadata with the given |partition_size|. signing_helper: Program which signs a hash and return signature. signing_helper_with_files: Same as signing_helper but uses files instead. release_string: None or avbtool release string. append_to_release_string: None or string to append. output_vbmeta_image: If not None, also write vbmeta struct to this file. do_not_append_vbmeta_image: If True, don't append vbmeta struct. print_required_libavb_version: True to only print required libavb version. use_persistent_root_digest: Use a persistent root digest on device. do_not_use_ab: The partition does not use A/B. no_hashtree: Do not append hashtree. Set size in descriptor as zero. check_at_most_once: Set to verify data blocks only the first time they are read from the data device. Raises: AvbError: If an argument is incorrect or adding the hashtree footer failed. """ required_libavb_version_minor = 0 if use_persistent_root_digest or do_not_use_ab or check_at_most_once: required_libavb_version_minor = 1 if rollback_index_location > 0: required_libavb_version_minor = 2 # If we're asked to calculate minimum required libavb version, we're done. if print_required_libavb_version: print('1.{}'.format(required_libavb_version_minor)) return digest_size = len(create_avb_hashtree_hasher(hash_algorithm, b'') .digest()) digest_padding = round_to_pow2(digest_size) - digest_size # If |partition_size| is given (e.g. not 0), calculate the maximum image # size such that an image this size + the hashtree + metadata (footer + # vbmeta struct) fits in |partition_size|. We use very conservative figures # for metadata. if partition_size > 0: max_tree_size = 0 max_fec_size = 0 if not no_hashtree: (_, max_tree_size) = calc_hash_level_offsets( partition_size, block_size, digest_size + digest_padding) if generate_fec: max_fec_size = calc_fec_data_size(partition_size, fec_num_roots) max_metadata_size = (max_fec_size + max_tree_size + self.MAX_VBMETA_SIZE + self.MAX_FOOTER_SIZE) max_image_size = partition_size - max_metadata_size else: max_image_size = 0 # If we're asked to only calculate the maximum image size, we're done. if calc_max_image_size: print('{}'.format(max_image_size)) return image = ImageHandler(image_filename) if partition_size > 0: if partition_size % image.block_size != 0: raise AvbError('Partition size of {} is not a multiple of the image ' 'block size {}.'.format(partition_size, image.block_size)) elif image.image_size % image.block_size != 0: raise AvbError('File size of {} is not a multiple of the image ' 'block size {}.'.format(image.image_size, image.block_size)) # If there's already a footer, truncate the image to its original # size. This way 'avbtool add_hashtree_footer' is idempotent # (modulo salts). if image.image_size >= AvbFooter.SIZE: image.seek(image.image_size - AvbFooter.SIZE) try: footer = AvbFooter(image.read(AvbFooter.SIZE)) # Existing footer found. Just truncate. original_image_size = footer.original_image_size image.truncate(footer.original_image_size) except (LookupError, struct.error): original_image_size = image.image_size else: # Image size is too small to possibly contain a footer. original_image_size = image.image_size # If anything goes wrong from here-on, restore the image back to # its original size. try: # Ensure image is multiple of block_size. rounded_image_size = round_to_multiple(image.image_size, block_size) if rounded_image_size > image.image_size: # If we need to round up the image size, it means the length of the # data to append is not a multiple of block size. # Setting multiple_block_size to false, so append_raw() will not # require it. image.append_raw(b'\0' * (rounded_image_size - image.image_size), multiple_block_size=False) # If image size exceeds the maximum image size, fail. if partition_size > 0: if image.image_size > max_image_size: raise AvbError('Image size of {} exceeds maximum image ' 'size of {} in order to fit in a partition ' 'size of {}.'.format(image.image_size, max_image_size, partition_size)) if salt: salt = binascii.unhexlify(salt) elif salt is None and not use_persistent_root_digest: # If salt is not explicitly specified, choose a hash that's the same # size as the hash size. Don't populate a random salt if this # descriptor is being created to use a persistent digest on device. hash_size = digest_size with open('/dev/urandom', 'rb') as f: salt = f.read(hash_size) else: salt = b'' # Hashes are stored upside down so we need to calculate hash # offsets in advance. (hash_level_offsets, tree_size) = calc_hash_level_offsets( image.image_size, block_size, digest_size + digest_padding) # If the image isn't sparse, its size might not be a multiple of # the block size. This will screw up padding later so just grow it. if image.image_size % image.block_size != 0: assert not image.is_sparse padding_needed = image.block_size - (image.image_size%image.block_size) image.truncate(image.image_size + padding_needed) # Generate the tree and add padding as needed. tree_offset = image.image_size root_digest, hash_tree = generate_hash_tree(image, image.image_size, block_size, hash_algorithm, salt, digest_padding, hash_level_offsets, tree_size) # Generate HashtreeDescriptor with details about the tree we # just generated. if no_hashtree: tree_size = 0 hash_tree = b'' ht_desc = AvbHashtreeDescriptor() ht_desc.dm_verity_version = 1 ht_desc.image_size = image.image_size ht_desc.tree_offset = tree_offset ht_desc.tree_size = tree_size ht_desc.data_block_size = block_size ht_desc.hash_block_size = block_size ht_desc.hash_algorithm = hash_algorithm ht_desc.partition_name = partition_name ht_desc.salt = salt if do_not_use_ab: ht_desc.flags |= AvbHashtreeDescriptor.FLAGS_DO_NOT_USE_AB if not use_persistent_root_digest: ht_desc.root_digest = root_digest if check_at_most_once: ht_desc.flags |= AvbHashtreeDescriptor.FLAGS_CHECK_AT_MOST_ONCE # Write the hash tree padding_needed = (round_to_multiple(len(hash_tree), image.block_size) - len(hash_tree)) hash_tree_with_padding = hash_tree + b'\0' * padding_needed #ifdef OPLUS_FEATURE_SECURITY_COMMON #Zhiwei.Qu@BSP.Security.Basic, 2023/02/03, skip appending tree chunk if the size is zero. if len(hash_tree_with_padding) > 0: image.append_raw(hash_tree_with_padding) #endif /*OPLUS_FEATURE_SECURITY_COMMON*/ len_hashtree_and_fec = len(hash_tree_with_padding) # Generate FEC codes, if requested. if generate_fec: if no_hashtree: fec_data = b'' else: fec_data = generate_fec_data(image_filename, fec_num_roots) padding_needed = (round_to_multiple(len(fec_data), image.block_size) - len(fec_data)) fec_data_with_padding = fec_data + b'\0' * padding_needed fec_offset = image.image_size image.append_raw(fec_data_with_padding) len_hashtree_and_fec += len(fec_data_with_padding) # Update the hashtree descriptor. ht_desc.fec_num_roots = fec_num_roots ht_desc.fec_offset = fec_offset ht_desc.fec_size = len(fec_data) ht_desc_to_setup = None if setup_as_rootfs_from_kernel: ht_desc_to_setup = ht_desc # Generate the VBMeta footer and add padding as needed. vbmeta_offset = tree_offset + len_hashtree_and_fec vbmeta_blob = self._generate_vbmeta_blob( algorithm_name, key_path, public_key_metadata_path, [ht_desc], chain_partitions, rollback_index, flags, rollback_index_location, props, props_from_file, kernel_cmdlines, setup_rootfs_from_kernel, ht_desc_to_setup, include_descriptors_from_image, signing_helper, signing_helper_with_files, release_string, append_to_release_string, required_libavb_version_minor) padding_needed = (round_to_multiple(len(vbmeta_blob), image.block_size) - len(vbmeta_blob)) vbmeta_blob_with_padding = vbmeta_blob + b'\0' * padding_needed # Write vbmeta blob, if requested. if output_vbmeta_image: output_vbmeta_image.write(vbmeta_blob) # Append vbmeta blob and footer, unless requested not to. if not do_not_append_vbmeta_image: image.append_raw(vbmeta_blob_with_padding) # Now insert a DONT_CARE chunk with enough bytes such that the # final Footer block is at the end of partition_size.. if partition_size > 0: image.append_dont_care(partition_size - image.image_size - 1 * image.block_size) # Generate the Footer that tells where the VBMeta footer # is. Also put enough padding in the front of the footer since # we'll write out an entire block. footer = AvbFooter() footer.original_image_size = original_image_size footer.vbmeta_offset = vbmeta_offset footer.vbmeta_size = len(vbmeta_blob) footer_blob = footer.encode() footer_blob_with_padding = ( b'\0' * (image.block_size - AvbFooter.SIZE) + footer_blob) image.append_raw(footer_blob_with_padding) except Exception as e: # Truncate back to original size, then re-raise. image.truncate(original_image_size) raise AvbError('Adding hashtree_footer failed: {}.'.format(e)) from e详细分析这个脚本
10-17
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值