顯示具有 加解密 標籤的文章。 顯示所有文章
顯示具有 加解密 標籤的文章。 顯示所有文章

2022年4月7日 星期四

[C++]UTF8轉wide character

 中文轉UTF8 在轉換為wide character 透過此方式轉換即可將文字轉為數字傳送出去

再透過解譯數字長度,將數字組回文字

---------------------------------------------------------------------

宣告以下資訊"

---.h

static const char trailingBytesForUTF8[256] = {
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
    2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 3,3,3,3,3,3,3,3,4,4,4,4,5,5,5,5
};

static const unsigned int offsetsFromUTF8[6] = {
    0x00000000UL, 0x00003080UL, 0x000E2080UL,
    0x03C82080UL, 0xFA082080UL, 0x82082080UL
};

int u8_toucs(unsigned int* dest, int sz, char* src, int srcsz);
int u8_toutf8(char* dest, int sz, unsigned int* src, int srcsz);
int u8_wc_toutf8(char* dest, unsigned int ch);

-----.cpp

int u8_toucs(unsigned int* dest, int sz, char* src, int srcsz){
    unsigned int ch;
    char* src_end = src + srcsz;
    int nb;
    int i = 0;

    while (i < sz - 1) {
        nb = trailingBytesForUTF8[(unsigned char)*src];
        if (srcsz == -1) {
            if (*src == 0)
                goto done_toucs;
        }
        else {
            if (src + nb >= src_end)
                goto done_toucs;
        }
        ch = 0;
        switch (nb) {
            /* these fall through deliberately */
        case 3: ch += (unsigned char)*src++; ch <<= 6;
        case 2: ch += (unsigned char)*src++; ch <<= 6;
        case 1: ch += (unsigned char)*src++; ch <<= 6;
        case 0: ch += (unsigned char)*src++;
        }
        ch -= offsetsFromUTF8[nb];
        dest[i++] = ch;
    }
done_toucs:
    dest[i] = 0;
    return i;
}

int u8_toutf8(char* dest, int sz, unsigned int* src, int srcsz)
{
    unsigned int ch;
    int i = 0;
    char* dest_end = dest + sz;

    while (srcsz < 0 ? src[i] != 0 : i < srcsz) {
        ch = src[i];
        if (ch < 0x80) {
            if (dest >= dest_end)
                return i;
            *dest++ = (char)ch;
        }
        else if (ch < 0x800) {
            if (dest >= dest_end - 1)
                return i;
            *dest++ = (ch >> 6) | 0xC0;
            *dest++ = (ch & 0x3F) | 0x80;
        }
        else if (ch < 0x10000) {
            if (dest >= dest_end - 2)
                return i;
            *dest++ = (ch >> 12) | 0xE0;
            *dest++ = ((ch >> 6) & 0x3F) | 0x80;
            *dest++ = (ch & 0x3F) | 0x80;
        }
        else if (ch < 0x110000) {
            if (dest >= dest_end - 3)
                return i;
            *dest++ = (ch >> 18) | 0xF0;
            *dest++ = ((ch >> 12) & 0x3F) | 0x80;
            *dest++ = ((ch >> 6) & 0x3F) | 0x80;
            *dest++ = (ch & 0x3F) | 0x80;
        }
        i++;
    }
    if (dest < dest_end)
        *dest = '\0';
    return i;
}

-----------------------------------------------

Sample: 範例

char result_str2[100] = {};
uint32_t b_ucs[100] = {}; // plenty of space
int b_chars = 0;

char cbuffdata[100] = u8"哈摟哈!";
uint32_t utf_len2 = strlen(cbuffdata);
b_chars = u8_toucs(b_ucs, (utf_len2 + 1) * 4, cbuffdata, utf_len2);
//此動作已完成轉換
b_chars = u8_toutf8(result_str2, (utf_len2 + 1) * 4, b_ucs, utf_len2);
//此動作為轉換回UTF8


----可再搭配其他轉換方式將UTF8轉成ansi顯示
    CString csbuf;
    ConvertUTF8toANSI(result_str2, &csbuf);
//此函式連結

-----------------------------------------------------------------------

參考:https://www.cprogramming.com/tutorial/unicode.html

2022年2月22日 星期二

[C#]RSA加解密

引用 using System.Security.Cryptography;

直接使用 以下函式即可

         /// <summary>

        /// RSA加密資料
        /// </summary>
        /// <param name="express">要加密資料</param>
        /// <param name="KeyContainerName">密匙容器的名稱</param>
        /// <returns></returns>
        public static string RSAEncryption(string express, string KeyContainerName = null)
        {

            System.Security.Cryptography.CspParameters param = new System.Security.Cryptography.CspParameters();
            param.KeyContainerName = KeyContainerName ?? "zhiqiang"; //密匙容器的名稱,保持加密解密一致才能解密成功
            using (System.Security.Cryptography.RSACryptoServiceProvider rsa = new System.Security.Cryptography.RSACryptoServiceProvider(param))
            {
                byte[] plaindata = System.Text.Encoding.Default.GetBytes(express);//將要加密的字串轉換為位元組陣列
                byte[] encryptdata = rsa.Encrypt(plaindata, false);//將加密後的位元組資料轉換為新的加密位元組陣列
                return Convert.ToBase64String(encryptdata);//將加密後的位元組陣列轉換為字串
            }
        }
        /// RSA解密資料
        /// </summary>
        /// <param name="express">要解密資料</param>
        /// <param name="KeyContainerName">密匙容器的名稱</param>
        /// <returns></returns>
        public static string RSADecrypt(string ciphertext, string KeyContainerName = null)
        {
            System.Security.Cryptography.CspParameters param = new System.Security.Cryptography.CspParameters();
            param.KeyContainerName = KeyContainerName ?? "zhiqiang"; //密匙容器的名稱,保持加密解密一致才能解密成功
            using (System.Security.Cryptography.RSACryptoServiceProvider rsa = new System.Security.Cryptography.RSACryptoServiceProvider(param))
            {
                byte[] encryptdata = Convert.FromBase64String(ciphertext);
                byte[] decryptdata = rsa.Decrypt(encryptdata, false);
                return System.Text.Encoding.Default.GetString(decryptdata);
            }
        }


參考來源: https://iter01.com/432462.html

2022年1月11日 星期二

[SQL]資料加解密

DECLARE @Pass NVARCHAR(32)  --加密Key

DECLARE @OrgVal nvarchar(128) --原始資料

DECLARE @EnVal VARBINARY(8000) --加密用

DECLARE @DeVal NVARCHAR(128) --解密用


--設定密碼

SET @Pass = N'rmpwd';

--原資料

SET @OrgVal = N'可以看見我';

--透過 ENCRYPTBYPASSPHRASE 加密成 VARBINARY(8000)

SET @EnVal = ENCRYPTBYPASSPHRASE(@Pass, @OrgVal);

--透過 DECRYPTBYPASSPHRASE 將資料解密回來

SET @DeVal = CONVERT(NVARCHAR, DECRYPTBYPASSPHRASE(@Pass, @EnVal));

SELECT @OrgVal AS [原資料], @DeVal AS  [解密的資料], @EnVal AS [加密過的資料];

[SQL]顯示千分位與小數顯示

  CONVERT ( data_type [ ( length ) ] , expression [ , style ] ) CONVERT style參數說明 1  (expression為 money 或 smallmoney型別): 0 : 預設,保留小數位後兩位,並四捨...