Skip to content

Instantly share code, notes, and snippets.

@CandyMi
Last active December 7, 2020 14:57
Show Gist options
  • Save CandyMi/2f76035b490443c8b837c467e5031211 to your computer and use it in GitHub Desktop.
Save CandyMi/2f76035b490443c8b837c467e5031211 to your computer and use it in GitHub Desktop.
框架对crypt库的测试用例.
-- AES 测试用例
local crypt = require "crypt"
local hexencode = crypt.hexencode
local b64enc = crypt.base64encode
-- ECB
local aes_128_ecb_encrypt = crypt.aes_128_ecb_encrypt
local aes_192_ecb_encrypt = crypt.aes_192_ecb_encrypt
local aes_256_ecb_encrypt = crypt.aes_256_ecb_encrypt
local aes_128_ecb_decrypt = crypt.aes_128_ecb_decrypt
local aes_192_ecb_decrypt = crypt.aes_192_ecb_decrypt
local aes_256_ecb_decrypt = crypt.aes_256_ecb_decrypt
-- CBC
local aes_128_cbc_encrypt = crypt.aes_128_cbc_encrypt
local aes_192_cbc_encrypt = crypt.aes_192_cbc_encrypt
local aes_256_cbc_encrypt = crypt.aes_256_cbc_encrypt
local aes_128_cbc_decrypt = crypt.aes_128_cbc_decrypt
local aes_192_cbc_decrypt = crypt.aes_192_cbc_decrypt
local aes_256_cbc_decrypt = crypt.aes_256_cbc_decrypt
-- OFB
local aes_128_ofb_encrypt = crypt.aes_128_ofb_encrypt
local aes_192_ofb_encrypt = crypt.aes_192_ofb_encrypt
local aes_256_ofb_encrypt = crypt.aes_256_ofb_encrypt
local aes_128_ofb_decrypt = crypt.aes_128_ofb_decrypt
local aes_192_ofb_decrypt = crypt.aes_192_ofb_decrypt
local aes_256_ofb_decrypt = crypt.aes_256_ofb_decrypt
-- CFB
local aes_128_cfb_encrypt = crypt.aes_128_cfb_encrypt
local aes_192_cfb_encrypt = crypt.aes_192_cfb_encrypt
local aes_256_cfb_encrypt = crypt.aes_256_cfb_encrypt
local aes_128_cfb_decrypt = crypt.aes_128_cfb_decrypt
local aes_192_cfb_decrypt = crypt.aes_192_cfb_decrypt
local aes_256_cfb_decrypt = crypt.aes_256_cfb_decrypt
-- CTR
local aes_128_ctr_encrypt = crypt.aes_128_ctr_encrypt
local aes_192_ctr_encrypt = crypt.aes_192_ctr_encrypt
local aes_256_ctr_encrypt = crypt.aes_256_ctr_encrypt
local aes_128_ctr_decrypt = crypt.aes_128_ctr_decrypt
local aes_192_ctr_decrypt = crypt.aes_192_ctr_decrypt
local aes_256_ctr_decrypt = crypt.aes_256_ctr_decrypt
-- GCM
local aes_128_gcm_encrypt = crypt.aes_128_gcm_encrypt
local aes_192_gcm_encrypt = crypt.aes_192_gcm_encrypt
local aes_256_gcm_encrypt = crypt.aes_256_gcm_encrypt
local aes_128_gcm_decrypt = crypt.aes_128_gcm_decrypt
local aes_192_gcm_decrypt = crypt.aes_192_gcm_decrypt
local aes_256_gcm_decrypt = crypt.aes_256_gcm_decrypt
local key, iv = "1234567891234560", "0123456789123456"
local key24, iv24 = "012345678901234567891234", "123456789012345678901234"
local key32, iv32 = "01234567890123456789012345678912", "12345678901234567890123456789012"
-- print("key16 = ", hexencode(key))
-- print("key24 = ", hexencode(key24))
-- print("key32 = ", hexencode(key32))
-- print("iv16 = ", hexencode(iv))
-- print("iv24 = ", hexencode(iv24))
-- print("iv32 = ", hexencode(iv32))
local text = "admin"
print("aes_128_ecb_encrypt: ", b64enc(aes_128_ecb_encrypt(key, text, iv)), "解密:", aes_128_ecb_decrypt(key, aes_128_ecb_encrypt(key, text, iv), iv) == text and "成功" or "失败")
print("aes_192_ecb_encrypt: ", b64enc(aes_192_ecb_encrypt(key24, text, iv24)), "解密:", aes_192_ecb_decrypt(key, aes_192_ecb_encrypt(key, text, iv), iv) == text and "成功" or "失败")
print("aes_256_ecb_encrypt: ", b64enc(aes_256_ecb_encrypt(key32, text, iv32)), "解密:", aes_256_ecb_decrypt(key, aes_256_ecb_encrypt(key, text, iv), iv) == text and "成功" or "失败")
print(string.format("echo -n '%s' | ./openssl %s -e -nosalt -K '%s' -iv '%s' -p -a", text, 'aes-128-ecb', hexencode(key), hexencode(iv)))
print(string.format("echo -n '%s' | ./openssl %s -e -nosalt -K '%s' -iv '%s' -p -a", text, 'aes-192-ecb', hexencode(key24), hexencode(iv24)))
print(string.format("echo -n '%s' | ./openssl %s -e -nosalt -K '%s' -iv '%s' -p -a", text, 'aes-256-ecb', hexencode(key32), hexencode(iv32)))
print()
print("aes_128_cbc_encrypt: ", b64enc(aes_128_cbc_encrypt(key, text, iv)), "解密:", aes_128_cbc_decrypt(key, aes_128_cbc_encrypt(key, text, iv), iv) == text and "成功" or "失败")
print("aes_192_cbc_encrypt: ", b64enc(aes_192_cbc_encrypt(key24, text, iv24)), "解密:", aes_192_cbc_decrypt(key, aes_192_cbc_encrypt(key, text, iv), iv) == text and "成功" or "失败")
print("aes_256_cbc_encrypt: ", b64enc(aes_256_cbc_encrypt(key32, text, iv32)), "解密:", aes_256_cbc_decrypt(key, aes_256_cbc_encrypt(key, text, iv), iv) == text and "成功" or "失败")
print(string.format("echo -n '%s' | ./openssl %s -e -nosalt -K '%s' -iv '%s' -p -a", text, 'aes-128-cbc', hexencode(key), hexencode(iv)))
print(string.format("echo -n '%s' | ./openssl %s -e -nosalt -K '%s' -iv '%s' -p -a", text, 'aes-192-cbc', hexencode(key24), hexencode(iv32)))
print(string.format("echo -n '%s' | ./openssl %s -e -nosalt -K '%s' -iv '%s' -p -a", text, 'aes-256-cbc', hexencode(key24), hexencode(iv32)))
print()
print("aes_128_ofb_encrypt: ", b64enc(aes_128_ofb_encrypt(key, text, iv)), "解密:", aes_128_ofb_decrypt(key, aes_128_ofb_encrypt(key, text, iv), iv) == text and "成功" or "失败")
print("aes_192_ofb_encrypt: ", b64enc(aes_192_ofb_encrypt(key24, text, iv24)), "解密:", aes_192_ofb_decrypt(key, aes_192_ofb_encrypt(key, text, iv), iv) == text and "成功" or "失败")
print("aes_256_ofb_encrypt: ", b64enc(aes_256_ofb_encrypt(key32, text, iv32)), "解密:", aes_256_ofb_decrypt(key, aes_256_ofb_encrypt(key, text, iv), iv) == text and "成功" or "失败")
print(string.format("echo -n '%s' | ./openssl %s -e -nosalt -K '%s' -iv '%s' -p -a", text, 'aes-128-ofb', hexencode(key), hexencode(iv)))
print(string.format("echo -n '%s' | ./openssl %s -e -nosalt -K '%s' -iv '%s' -p -a", text, 'aes-192-ofb', hexencode(key24), hexencode(iv24)))
print(string.format("echo -n '%s' | ./openssl %s -e -nosalt -K '%s' -iv '%s' -p -a", text, 'aes-256-ofb', hexencode(key32), hexencode(iv32)))
print()
print("aes_128_cfb_encrypt: ", b64enc(aes_128_cfb_encrypt(key, text, iv)), "解密:", aes_128_cfb_decrypt(key, aes_128_cfb_encrypt(key, text, iv), iv) == text and "成功" or "失败")
print("aes_192_cfb_encrypt: ", b64enc(aes_192_cfb_encrypt(key24, text, iv24)), "解密:", aes_192_cfb_decrypt(key, aes_192_cfb_encrypt(key, text, iv), iv) == text and "成功" or "失败")
print("aes_256_cfb_encrypt: ", b64enc(aes_256_cfb_encrypt(key32, text, iv32)), "解密:", aes_256_cfb_decrypt(key, aes_256_cfb_encrypt(key, text, iv), iv) == text and "成功" or "失败")
print(string.format("echo -n '%s' | ./openssl %s -e -nosalt -K '%s' -iv '%s' -p -a", text, 'aes-128-cfb', hexencode(key), hexencode(iv)))
print(string.format("echo -n '%s' | ./openssl %s -e -nosalt -K '%s' -iv '%s' -p -a", text, 'aes-192-cfb', hexencode(key24), hexencode(iv24)))
print(string.format("echo -n '%s' | ./openssl %s -e -nosalt -K '%s' -iv '%s' -p -a", text, 'aes-256-cfb', hexencode(key32), hexencode(iv32)))
print()
print("aes_128_ctr_encrypt: ", b64enc(aes_128_ctr_encrypt(key, text, iv)), "解密:", aes_128_ctr_decrypt(key, aes_128_ctr_encrypt(key, text, iv), iv) == text and "成功" or "失败")
print("aes_192_ctr_encrypt: ", b64enc(aes_192_ctr_encrypt(key24, text, iv24)), "解密:", aes_192_ctr_decrypt(key, aes_192_ctr_encrypt(key, text, iv), iv) == text and "成功" or "失败")
print("aes_256_ctr_encrypt: ", b64enc(aes_256_ctr_encrypt(key32, text, iv32)), "解密:", aes_256_ctr_decrypt(key, aes_256_ctr_encrypt(key, text, iv), iv) == text and "成功" or "失败")
print(string.format("echo -n '%s' | ./openssl %s -e -nosalt -K '%s' -iv '%s' -p -a", text, 'aes-128-ctr', hexencode(key), hexencode(iv)))
print(string.format("echo -n '%s' | ./openssl %s -e -nosalt -K '%s' -iv '%s' -p -a", text, 'aes-192-ctr', hexencode(key24), hexencode(iv24)))
print(string.format("echo -n '%s' | ./openssl %s -e -nosalt -K '%s' -iv '%s' -p -a", text, 'aes-256-ctr', hexencode(key32), hexencode(iv32)))
print()
print("aes_128_gcm_encrypt: ", b64enc(aes_128_gcm_encrypt(key, text, iv)), "解密:", aes_128_gcm_decrypt(key, aes_128_gcm_encrypt(key, text, iv), iv) == text and "成功" or "失败")
print("aes_192_gcm_encrypt: ", b64enc(aes_192_gcm_encrypt(key24, text, iv24)), "解密:", aes_192_gcm_decrypt(key, aes_128_gcm_encrypt(key, text, iv), iv) == text and "成功" or "失败")
print("aes_256_gcm_encrypt: ", b64enc(aes_256_gcm_encrypt(key32, text, iv32)), "解密:", aes_256_gcm_decrypt(key, aes_128_gcm_encrypt(key, text, iv), iv) == text and "成功" or "失败")
print(string.format("echo -n '%s' | ./openssl %s -e -nosalt -K '%s' -iv '%s' -p -a", text, 'aes-128-gcm', hexencode(key), hexencode(iv)))
print(string.format("echo -n '%s' | ./openssl %s -e -nosalt -K '%s' -iv '%s' -p -a", text, 'aes-192-gcm', hexencode(key24), hexencode(iv24)))
print(string.format("echo -n '%s' | ./openssl %s -e -nosalt -K '%s' -iv '%s' -p -a", text, 'aes-256-gcm', hexencode(key32), hexencode(iv32)))
local LOG = require"logging"
local crypt = require"crypt"
local hexencode = crypt.hexencode
local base64enc = crypt.base64encode
local iv = "0123456789123456"
local key = "1234567890123456"
local text = "admin"
LOG:DEBUG(hexencode(key))
LOG:DEBUG(hexencode(iv))
local cipher = crypt.desx_encrypt(key, text, iv)
local rawtext = crypt.desx_decrypt(key, cipher, iv)
LOG:DEBUG(base64enc(cipher), rawtext)
local cipher = crypt.des_cbc_encrypt(key, text, iv)
local rawtext = crypt.des_cbc_decrypt(key, cipher, iv)
LOG:DEBUG(base64enc(cipher), rawtext)
local cipher = crypt.des_ecb_encrypt(key, text, "")
local rawtext = crypt.des_ecb_decrypt(key, cipher, iv)
LOG:DEBUG(base64enc(cipher), rawtext)
local cipher = crypt.des_cfb_encrypt(key, text, iv)
local rawtext = crypt.des_cfb_decrypt(key, cipher, iv)
LOG:DEBUG(base64enc(cipher), rawtext)
local cipher = crypt.des_ofb_encrypt(key, text, iv)
local rawtext = crypt.des_ofb_decrypt(key, cipher, iv)
LOG:DEBUG(base64enc(cipher), rawtext)
local cipher = crypt.des_ede_encrypt(key, text, iv)
local rawtext = crypt.des_ede_decrypt(key, cipher, iv)
LOG:DEBUG(base64enc(cipher), rawtext)
local cipher = crypt.des_ede3_encrypt(key, text, iv)
local rawtext = crypt.des_ede3_decrypt(key, cipher, iv)
LOG:DEBUG(base64enc(cipher), rawtext)
local cipher = crypt.des_ede_ecb_encrypt(key, text, iv)
local rawtext = crypt.des_ede_ecb_decrypt(key, cipher, iv)
LOG:DEBUG(base64enc(cipher), rawtext)
local cipher = crypt.des_ede3_ecb_encrypt(key, text, iv)
local rawtext = crypt.des_ede3_ecb_decrypt(key, cipher, iv)
LOG:DEBUG(base64enc(cipher), rawtext)
-- SM2/SM3/SM4 测试用例
local crypt = require "crypt"
local hexencode = crypt.hexencode
local sm3 = crypt.sm3
local hmac_sm3 = crypt.hmac_sm3
local sm2keygen = crypt.sm2keygen
local sm2sign = crypt.sm2sign
local sm2verify = crypt.sm2verify
local sm4_cbc_encrypt = crypt.sm4_cbc_encrypt
local sm4_cbc_decrypt = crypt.sm4_cbc_decrypt
local sm4_ecb_encrypt = crypt.sm4_ecb_encrypt
local sm4_ecb_decrypt = crypt.sm4_ecb_decrypt
local sm4_ofb_encrypt = crypt.sm4_ofb_encrypt
local sm4_ofb_decrypt = crypt.sm4_ofb_decrypt
local sm4_ctr_encrypt = crypt.sm4_ctr_encrypt
local sm4_ctr_decrypt = crypt.sm4_ctr_decrypt
local key = "administrator"
local iv = "0123456789123456"
local text = "水果糖的`SM API`实现."
print(string.format('text = "%s", key = "%s", iv = "%s"', text, key, iv))
print(string.format("使用openssl 1.1.1测试[%s]算法: echo -n '%s' | openssl %s -nosalt -iv '%s' -K '%s' -a -p", "sm4-cbc", text, "sm4-cbc", hexencode(iv), hexencode(key)))
print(string.format("使用openssl 1.1.1测试[%s]算法: echo -n '%s' | openssl %s -nosalt -iv '%s' -K '%s' -a -p", "sm4-ecb", text, "sm4-ecb", hexencode(iv), hexencode(key)))
print(string.format("使用openssl 1.1.1测试[%s]算法: echo -n '%s' | openssl %s -nosalt -iv '%s' -K '%s' -a -p", "sm4-ofb", text, "sm4-ofb", hexencode(iv), hexencode(key)))
print(string.format("使用openssl 1.1.1测试[%s]算法: echo -n '%s' | openssl %s -nosalt -iv '%s' -K '%s' -a -p", "sm4-ctr", text, "sm4-ctr", hexencode(iv), hexencode(key)))
print()
-- sm3 摘要内容
print('SM3摘要结果: ', sm3(text, true))
-- hmac_sm3 摘要内容
print('HMAC-SM3哈希结果:', hmac_sm3(key, text, true))
local cipher = sm4_cbc_encrypt(key, text, iv, true)
print("SM4-CBC-加密结果", cipher)
local cipher = sm4_cbc_decrypt(key, cipher, iv, true)
print("SM4-CBC-解密结果", cipher)
local cipher = sm4_ecb_encrypt(key, text, iv, true)
print("SM4-ECB-加密结果", cipher)
local cipher = sm4_ecb_decrypt(key, cipher, iv, true)
print("SM4-ECB-解密结果", cipher)
local cipher = sm4_ofb_encrypt(key, text, iv, true)
print("SM4-OFB-加密结果", cipher)
local cipher = sm4_ofb_decrypt(key, cipher, iv, true)
print("SM4-OFB-解密结果", cipher)
local cipher = sm4_ctr_encrypt(key, text, iv, true)
print("SM4-CTR-加密结果", cipher)
local cipher = sm4_ctr_decrypt(key, cipher, iv, true)
print("SM4-CTR-解密结果", cipher)
-- 测试随机生成EC-SM2的私钥与公钥
sm2keygen("pri.key", "pub.key")
-- 测试签名与验签
local sign = sm2sign("pri.key", text, true)
print("SM3 With SM2签名为:", sign)
print("SM3 With SM2验签结果:", sm2verify("pub.key", text, sign, true) and "成功" or "失败")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment