恆知科技

恆知科技 資訊軟體 恆知用心.服務創新

2026.04.27 記述 - 開發 iOS App Icon 配置工具,自動生成 iOS 26 AI Prompt 指令。
27/04/2026

2026.04.27 記述 - 開發 iOS App Icon 配置工具,自動生成 iOS 26 AI Prompt 指令。

01/04/2026

.NET 加解密邏輯 for B4X (B4A/B4J/B4i) 使用 AES-128-CBC 模式

' AES 加密函數
Private Function Encrypt(ByVal plainText As String) As String
Dim plainBytes As Byte() = Encoding.UTF8.GetBytes(plainText)
Using aes As New AesManaged()
aes.Key = Encoding.UTF8.GetBytes(AES_KEY)
aes.IV = Encoding.UTF8.GetBytes(AES_IV)
aes.Mode = CipherMode.CBC
aes.Padding = PaddingMode.PKCS7
Using encryptor As ICryptoTransform = aes.CreateEncryptor()
Dim encryptedBytes As Byte() = encryptor.TransformFinalBlock(plainBytes, 0, plainBytes.Length)
Return Convert.ToBase64String(encryptedBytes)
End Using
End Using
End Function

' AES 解密函數
Private Function Decrypt(ByVal cipherText As String) As String
' 注意:從 URL 傳過來的 Base64 字串,加號(+)有時會變空白,需要處理
Dim cleanedCipherText As String = cipherText.Replace(" ", "+")
Dim cipherBytes As Byte() = Convert.FromBase64String(cleanedCipherText)
Using aes As New AesManaged()
aes.Key = Encoding.UTF8.GetBytes(AES_KEY)
aes.IV = Encoding.UTF8.GetBytes(AES_IV)
aes.Mode = CipherMode.CBC
aes.Padding = PaddingMode.PKCS7
Using decryptor As ICryptoTransform = aes.CreateDecryptor()
Dim plainBytes As Byte() = decryptor.TransformFinalBlock(cipherBytes, 0, cipherBytes.Length)
Return Encoding.UTF8.GetString(plainBytes)
End Using
End Using
End Function

01/04/2026

B4X (B4A/B4J/B4i) 使用 AES-128-CBC 模式

' Module: AESUtils
Sub Process_Globals
Private kg As KeyGenerator
Private c As Cipher
' 金鑰與 IV 必須是 16 個字元 (128-bit)
Private const AES_KEY As String = "1234567890123456"
Private const AES_IV As String = "6543210987654321"
End Sub

' 加密字串並回傳 Base64
Public Sub Encrypt(Data As String) As String
Dim b() As Byte = c.Encrypt(Data.GetBytes("UTF8"), kg.InitializeKey(AES_KEY.GetBytes("UTF8")), AES_IV.GetBytes("UTF8"))
Dim su As StringUtils
Return su.EncodeBase64(b)
End Sub

' 解密 Base64 字串回純文字
Public Sub Decrypt(EncryptedBase64 As String) As String
Dim su As StringUtils
Dim b() As Byte = su.DecodeBase64(EncryptedBase64)
Dim res() As Byte = c.Decrypt(b, kg.InitializeKey(AES_KEY.GetBytes("UTF8")), AES_IV.GetBytes("UTF8"))
Return BytesToString(res, 0, res.Length, "UTF8")
End Sub

11/03/2026

DevExpress控制項防止瀏覽器自動填入 Username 與 Password 的方法

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
' 防止瀏覽器自動填入帳密
txtUsername.Attributes("autocomplete") = "off"
txtPassword.Attributes("autocomplete") = "new-password"

' 進階:隨機化 name 屬性,阻止以欄位名稱比對的自動填入
Dim rnd As String = Guid.NewGuid().ToString("N").Substring(0,8)
txtUsername.Attributes("name") = "u_" & rnd
txtPassword.Attributes("name") = "p_" & rnd
End Sub

恆知用心.服務創新

09/03/2026

在 .NET(包含 VB.NET 與 C #)中 Dictionary 類別中有一個非常方便且強大的特性。
當你使用 字典名稱(Key) = Value 這種寫法時,系統底層執行的邏輯是所謂的 「Upsert (Update or Insert)」。它的運作規則如下:
* 如果 Key還不存在於字典中: 它會自動幫你 「新增」 一筆新的資料進去。
* 如果 Key已經存在於字典中: 它不會報錯,而是會直接把舊的設定值 「覆寫 (更新)」 成新的設定。
為什麼不使用 .Add() 方法?
如果你用傳統的寫法:
_numConfigs.Add(tb, New NumCfg With { ... })

這樣寫會有一個風險:如果同一個 tb 被加入了第二次,程式就會因為發生衝突(Key已存在)而直接拋出錯誤 (Exception) 並崩潰。

使用 _numConfigs(tb) = ... 的寫法,我們就不需要先寫 If _numConfigs.ContainsKey(tb) Then ... 來檢查它到底存不存在,一行程式碼就能完美兼顧「第一次綁定的新增」與「後續修改設定的覆寫」,是最安全且最簡潔的做法!

03/03/2026

BitConverter.GetBytes(counter),在不同 CPU 架構(Little-Endian vs Big-Endian)上會產出不同的結果

int counter = 1;
byte[] bytes = BitConverter.GetBytes(counter);

// Little-Endian (x86/x64, ARM 預設):[01, 00, 00, 00]
// Big-Endian (某些 MIPS, SPARC): [00, 00, 00, 01]

完全不依賴平台解決方案:手動位元移位(跨平台最安全)
// 固定 Big-Endian

static byte[] ToBytesBigEndian(int value)
{
return new byte[]
{
(byte)(value >> 24),
(byte)(value >> 16),
(byte)(value >> 8),
(byte)(value)
};
}

byte[] bytes = ToBytesBigEndian(counter);

extension method:
static byte[] ToBigEndianBytes(this int value)
{
return new byte[]
{
(byte)(value >> 24),
(byte)(value >> 16),
(byte)(value >> 8),
(byte)(value)
};
}

byte[] bytes = counter.ToBigEndianBytes();

19/02/2026

VB.NET 金鑰產生工具

Imports System
Imports System.Security.Cryptography
Imports System.Text

'''
''' 金鑰產生器:用於生成高強度的隨機金鑰 (High-Entropy Keys)。
'''
Public NotInheritable Class KeyGenerator

Private Sub New()
End Sub

'''
''' 產生高強度隨機金鑰 (Base64 格式)。
''' 這是最推薦給 AES 加密使用的格式。
'''
''' 金鑰長度 (Bytes)。預設 32 bytes (256 bits)。
''' Base64 字串
Public Shared Function CreateBase64Key(Optional ByVal keySizeInBytes As Integer = 32) As String
Using rng As RandomNumberGenerator = RandomNumberGenerator.Create()
Dim bytes(keySizeInBytes - 1) As Byte
rng.GetBytes(bytes)
Return Convert.ToBase64String(bytes)
End Using
End Function

'''
''' 產生 URL 安全的隨機金鑰 (無 + / =)。
''' 適合用於網址傳遞或作為檔案名稱的一部分。
'''
''' 金鑰長度 (Bytes)。預設 32 bytes。
''' URL Safe Base64 字串
Public Shared Function CreateUrlSafeKey(Optional ByVal keySizeInBytes As Integer = 32) As String
Dim base64 As String = CreateBase64Key(keySizeInBytes)
Return Base64ToUrl(base64)
End Function

'''
''' 產生十六進位字串金鑰 (Hex String)。
''' 適合用於 API Key 或資料庫索引 (0-9, A-F)。
'''
''' 金鑰長度 (Bytes)。預設 32 bytes (產出的字串長度會是 64)。
''' Hex 字串
Public Shared Function CreateHexKey(Optional ByVal keySizeInBytes As Integer = 32) As String
Using rng As RandomNumberGenerator = RandomNumberGenerator.Create()
Dim bytes(keySizeInBytes - 1) As Byte
rng.GetBytes(bytes)

Dim sb As New StringBuilder(bytes.Length * 2)
For Each b As Byte In bytes
sb.Append(b.ToString("X2"))
Next
Return sb.ToString()
End Using
End Function

'''
''' Base64 -> URL-safe Base64(移除 '=' padding,並把 '+' '/' 替換成 '-' '_')。
'''
Private Shared Function Base64ToUrl(ByVal base64 As String) As String
Return base64.Replace("+"c, "-"c) _
.Replace("/"c, "_"c) _
.TrimEnd("="c)
End Function

End Class

19/02/2026

C # 金鑰產生工具

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

public static class KeyGenerator
{
///
/// 產生高強度隨機金鑰 (Base64 格式)。
/// 這是最推薦給 AES 加密使用的格式。
///
/// 金鑰長度 (Bytes)。預設 32 bytes (256 bits)。
/// Base64 字串
public static string CreateBase64Key(int keySizeInBytes = 32)
{
byte[] bytes = new byte[keySizeInBytes];
using (var rng = RandomNumberGenerator.Create())
{
rng.GetBytes(bytes);
}
return Convert.ToBase64String(bytes);
}

///
/// 產生 URL 安全的隨機金鑰 (無 + / =)。
/// 適合用於網址傳遞或作為檔案名稱的一部分。
///
/// 金鑰長度 (Bytes)。預設 32 bytes。
/// URL Safe Base64 字串
public static string CreateUrlSafeKey(int keySizeInBytes = 32)
{
string base64 = CreateBase64Key(keySizeInBytes);
return Base64ToUrl(base64);
}

///
/// 產生十六進位字串金鑰 (Hex String)。
/// 適合用於 API Key 或資料庫索引 (0-9, A-F)。
///
/// 金鑰長度 (Bytes)。預設 32 bytes (產出的字串長度會是 64)。
/// Hex 字串
public static string CreateHexKey(int keySizeInBytes = 32)
{
byte[] bytes = new byte[keySizeInBytes];
using (var rng = RandomNumberGenerator.Create())
{
rng.GetBytes(bytes);
}

var sb = new StringBuilder(bytes.Length * 2);
foreach (byte b in bytes)
{
sb.Append(b.ToString("X2"));
}
return sb.ToString();
}

///
/// Base64 -> URL-safe Base64(移除 '=' padding,並把 '+' '/' 替換成 '-' '_')。
///
private static string Base64ToUrl(string base64)
{
return base64
.Replace('+', '-')
.Replace('/', '_')
.TrimEnd('=');
}
}

14/02/2026
許多工程師都使用紅色字體部分在寫轉換,通常被網路文章誤導所致。早期工程師對於 COM 應用在何種環境比較了解,犯此種錯誤比較少見。
14/02/2026

許多工程師都使用紅色字體部分在寫轉換,通常被網路文章誤導所致。早期工程師對於 COM 應用在何種環境比較了解,犯此種錯誤比較少見。

Address

1F. , No. 7-3, Minsheng Road , Madou Township
Tainan
72145

Opening Hours

Monday 09:00 - 17:30
Tuesday 09:00 - 17:30
Wednesday 09:00 - 17:30
Thursday 09:00 - 17:30
Friday 09:00 - 17:30

Telephone

+88665710732

Website

Alerts

Be the first to know and let us send you an email when 恆知科技 posts news and promotions. Your email address will not be used for any other purpose, and you can unsubscribe at any time.

Share