~~~lua|woo print('===json编码解码:===') local r = { kl = 123 } _out('输出数组:', r, '\n') _out('输出json编码:', _json_encode(r), '\n') print("===crc32:===" .. _crc32(_time())) print("===md5:===" .. _md5(_time())) print("===sha1:===" .. _sha1(_time())) print('===base64编码解码:===',_base64_decode(_base64_encode(_time()))) ~~~ # openssl加密解密 ~~~lua|woo print('===openssl加密解密:===') local cipher = require(':cipher'):new() -- 私钥生成 -- openssl genrsa -out rsa_private_key.pem 1024 local privateKey = [[ -----BEGIN RSA PRIVATE KEY----- MIICWwIBAAKBgQDcGsUIIAINHfRTdMmgGwLrjzfMNSrtgIf4EGsNaYwmC1GjF/bM h0Mcm10oLhNrKNYCTTQVGGIxuc5heKd1gOzb7bdTnCDPPZ7oV7p1B9Pud+6zPaco qDz2M24vHFWYY2FbIIJh8fHhKcfXNXOLovdVBE7Zy682X1+R1lRK8D+vmQIDAQAB AoGAeWAZvz1HZExca5k/hpbeqV+0+VtobMgwMs96+U53BpO/VRzl8Cu3CpNyb7HY 64L9YQ+J5QgpPhqkgIO0dMu/0RIXsmhvr2gcxmKObcqT3JQ6S4rjHTln49I2sYTz 7JEH4TcplKjSjHyq5MhHfA+CV2/AB2BO6G8limu7SheXuvECQQDwOpZrZDeTOOBk z1vercawd+J9ll/FZYttnrWYTI1sSF1sNfZ7dUXPyYPQFZ0LQ1bhZGmWBZ6a6wd9 R+PKlmJvAkEA6o32c/WEXxW2zeh18sOO4wqUiBYq3L3hFObhcsUAY8jfykQefW8q yPuuL02jLIajFWd0itjvIrzWnVmoUuXydwJAXGLrvllIVkIlah+lATprkypH3Gyc YFnxCTNkOzIVoXMjGp6WMFylgIfLPZdSUiaPnxby1FNM7987fh7Lp/m12QJAK9iL 2JNtwkSR3p305oOuAz0oFORn8MnB+KFMRaMT9pNHWk0vke0lB1sc7ZTKyvkEJW0o eQgic9DvIYzwDUcU8wJAIkKROzuzLi9AvLnLUrSdI6998lmeYO9x7pwZPukz3era zncjRK3pbVkv0KrKfczuJiRlZ7dUzVO0b6QJr8TRAA== -----END RSA PRIVATE KEY----- ]] -- 公钥: 根据私钥生成 -- openssl rsa -in rsa_private_key.pem -pubout -out rsa_public_key.pem local publicKey = [[ -----BEGIN PUBLIC KEY----- MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDcGsUIIAINHfRTdMmgGwLrjzfM NSrtgIf4EGsNaYwmC1GjF/bMh0Mcm10oLhNrKNYCTTQVGGIxuc5heKd1gOzb7bdT nCDPPZ7oV7p1B9Pud+6zPacoqDz2M24vHFWYY2FbIIJh8fHhKcfXNXOLovdVBE7Z y682X1+R1lRK8D+vmQIDAQAB -----END PUBLIC KEY----- ]] local isToBase64 = true -- true:把openssl加密结果转换为base64,false:返回字节集合 local rsaEnStr = cipher:rsaEncode("hello world!", publicKey, isToBase64) print('openssl加密结果:', rsaEnStr) local isFromBase64=true -- 解密时,待解密数据是否是base64明文,否则就是字节集数据 local isToString=true -- 表示是否把返回的数据转换为字符串,否则返回字节集 local rsaDeStr = cipher:rsaDecode(rsaEnStr, privateKey, isFromBase64,isToString) print('openssl解密结果:', rsaDeStr) ~~~ >openssl加密结果: ZBHGTkdhkwSCUZAz3cPUb2tEoKNXjbYf9Tgij2X8Q5/7Mcs/X/RGTE7ElCtDyMAZczESdahmlDkk1RmDiAns/ShltZ4Woe7lnW5UiTy94Tk8jSS1ihDpKXjgU8VZVoysQlBW0SqbHZPBvsQrxNDzRpaRq/sJlL38ZX7xuuIz8yg= openssl解密结果: hello world! # AES加密解密 ```lua|woo print('===aes加密解密:===') local encrypt_string = "hello world!" local encrypt_psw = "这是密码" local iv = nil -- 这个向量可忽略掉 local enStr = cipher:aesEncode(encrypt_string, encrypt_psw, iv) print("aes加密结果", enStr) local deStr = cipher:aesDecode(enStr, encrypt_psw, iv) print("aes解密结果", deStr) ``` >aes加密结果 X/1AlrMTv2yVH98XbJ7UCw== aes解密结果 hello world!