政务浏浏览器Demo

国密SM4相关的API

sm4加密解密

通过“ zwexplorer.__gmUtils.sm4.encypt(gmRequest,callback(resp));”加密; 通过“ zwexplorer.__gmUtils.sm4.decrypt(gmRequest,callback(resp));”解密

加密

原始数据(不超过2k):




解密


随机生成key和iv
                                            function generate_randomstr(lengthArg, elemId) {
                                                const length = lengthArg
                                                const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
                                                let result = ''
                                                for (let i = 0; i < length; i++) {
                                                    result += characters.charAt(Math.floor(Math.random() * characters.length))
                                                }
                                                $(elemId).val(result);
                                            }
                                        
sm4加密
                                            function encrypt() {
                                                const content = $("#sm4_content").val();
                                                const key = $("#sm4_key").val();
                                                const iv = $("#sm4_iv").val();
                                                const gmRequest = {
                                                    content: content,
                                                    key: key,
                                                    options: {
                                                        iv: iv
                                                    }
                                                }
        
                                                zwexplorer.__gmUtils.sm4.encrypt(gmRequest, (resp) => {
        
                                                    if (resp.resultCode === 0) {
                                                        $("#sm4_encrypt_result").val(resp.data);
        
                                                    } else {
                                                        $("#sm4_encrypt_result").val(resp.resultMessage);
                                                    }
                                                })
                                            }
                                        
sm4解密
                                            function decrypt() {
                                                const content = $("#sm4_encrypt_result").val();
                                                const key = $("#sm4_key").val();
                                                const iv = $("#sm4_iv").val();
                                                const gmRequest = {
                                                    content: content,
                                                    key: key,
                                                    options: {
                                                        iv: iv
                                                    }
                                                }
        
                                                zwexplorer.__gmUtils.sm4.decrypt(gmRequest, (resp) => {
        
                                                    if (resp.resultCode === 0) {
                                                        $("#sm4_decrypt_result").val(resp.data);
        
                                                    } else {
                                                        $("#sm4_decrypt_result").val(resp.resultMessage);
                                                    }
                                                })
                                            }