UNPKG

gm-sm

Version:

sm2

215 lines (167 loc) 7.72 kB
<!DOCTYPE html> <!-- saved from url=(0081)http://www.jonllen.com/upload/jonllen/case/jsrsasign-master/sample-sm2_crypt.html --> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=GBK"> <meta http-equiv="X-UA-Compatible" content="chrome=1"> <meta name="description" content="JavaScript implementation of SM2 Algorithm Encryption and Decryption sample."> <!--<link rel="stylesheet" type="text/css" media="screen" href="stylesheets/stylesheet.css">--> <title>SM2 Algorithm Encryption and Decryption sample</title> <script language="JavaScript" type="text/javascript" src="../dist/sm2.js"></script> <script language="JavaScript" type="text/javascript"> function test() { for (var i = 0; i < 100; i++) { zgscs_123(i); } } function zgscs_123(index) { doGenerate(); doCrypt(); var f1 = document.form1; var msg = f1.msg1.value; var prvkey = f1.prvkey1.value; var encryptData = f1.sigval1.value; var privateKey = new BigInteger(prvkey, 16); var cipherMode = f1.cipherMode.value; var cipher = new SM2Cipher(cipherMode); var data = cipher.Decrypt(privateKey, encryptData); var prvkey = f1.prvkey1.value; var pubkey = f1.pubkey1.value; console.log(index); if (msg != data) { console.log(index + "公钥:" + pubkey); console.log(index + "私钥:" + prvkey); console.log(index + "密文:" + encryptData); console.log(index + "结果:" + data); } } function doGenerate() { var f1 = document.form1; var curve = f1.curve1.value; var ec = new KJUR.crypto.ECDSA({"curve": curve}); var keypair = ec.generateKeyPairHex(); f1.prvkey1.value = keypair.ecprvhex; f1.pubkey1.value = keypair.ecpubhex; } function doCrypt() { var f1 = document.form1; var curve = f1.curve1.value; var msg = f1.msg1.value; var msgData = CryptoJS.enc.Utf8.parse(msg); var pubkeyHex = f1.pubkey1.value; if (pubkeyHex.length > 130) { pubkeyHex = pubkeyHex.substr(pubkeyHex.length - 130); } var cipherMode = f1.cipherMode.value; var cipher = new SM2Cipher(cipherMode); var userKey = cipher.CreatePoint(pubkeyHex); msgData = cipher.str2Bytes(msgData.toString()); var encryptData = cipher.Encrypt(userKey, msgData); f1.sigval1.value = encryptData; } function doDecrypt() { var f1 = document.form1; var prvkey = f1.prvkey1.value; var encryptData = f1.sigval1.value; var privateKey = new BigInteger(prvkey, 16); var cipherMode = f1.cipherMode.value; var cipher = new SM2Cipher(cipherMode); var data = cipher.Decrypt(privateKey, encryptData); alert(data ? '解密成功,原文:' + data : '解密失败!'); } function certCrypt() { var certData = document.getElementById('txtCertData').value; if (certData != "") { var key = X509.getPublicKeyFromCertPEM(certData); document.getElementById('txtPubKey').value = key.pubKeyHex; } var pubkey = document.getElementById('txtPubKey').value.replace(/\s/g, ''); var pubkeyHex = pubkey; if (pubkeyHex.length > 64 * 2) { pubkeyHex = pubkeyHex.substr(pubkeyHex.length - 64 * 2); } var xHex = pubkeyHex.substr(0, 64); var yHex = pubkeyHex.substr(64); var cipherMode = document.getElementById('cipherMode').value; var cipher = new SM2Cipher(cipherMode); var userKey = cipher.CreatePoint(xHex, yHex); var msg = document.getElementById('txtRawData').value; var msgData = CryptoJS.enc.Utf8.parse(msg); msgData = cipher.GetWords(msgData.toString()); var encryptData = cipher.Encrypt(userKey, msgData); document.getElementById('txtCryptData').value = hex2b64(encryptData); } </script> </head> <body> <!-- HEADER --> <div> <header> <h1 id="project_title">SM2 Algorithm Encryption and Decryption sample</h1> <h2 id="project_tagline">generating SM2 keypair, SM2 Algorithm Encryption and Decryption</h2> </header> </div> <!-- MAIN CONTENT --> <div id="main_content_wrap" class="outer"> <section id="main_content" class="inner"> <script type="text/javascript"> if (/msie/.test(navigator.userAgent.toLowerCase())) { document.write("<p><em>若IE浏览器下提示停止运行此脚本,请选择<b>否(N)</b>继续运行。</em></p>"); } </script> <!-- now editing --> <form name="form1"> <h4>(Step1) choose supported EC curve name and generate key pair</h4> ECC curve name: <select name="curve1"> <option value="sm2">SM2 </option> <option value="secp256r1">secp256r1 (= NIST P-256, P-256, prime256v1) </option> <option value="secp256k1">secp256k1 </option> <option value="secp384r1">secp384r1 (= NIST P-384, P-384) </option> </select><br> <input type="button" value="generate EC key pair" onclick="doGenerate();"><br> <p> EC private key (hex): <input type="text" name="prvkey1" value="" size="100"><br> EC public key (hex): <input type="text" name="pubkey1" value="" size="100"><br> </p> <!-- ============================================================== --> <h4>(Step2) Crypt message</h4> Crypt Options: <select id="cipherMode" name="cipherMode"> <option value="1" selected="selected">C1C3C2 </option> <option value="0">C1C2C3 </option> </select><br> Message string to be Crypted: <input type="text" name="msg1" value="jonllen" size="100"><br> <input type="button" value="Crypt message" onclick="doCrypt();"><br> <p> Crypt value (hex): <input type="text" id="sigval1" name="sigval1" value="" size="100"><br> </p> <h4>(Step3) Decrypt message</h4> <input type="button" value="decrypt it!" onclick="doDecrypt();"> <input type="reset" value="reset"> <input type="button" value="test" onclick="test();"> </form> <!-- now editing --> </section> </div> <script> var ciphers = ''; for(var i=0;i<100;i++){ var cipher = sm2.doEncrypt('asdASD','046DEC23DA498A129DB79C8A6CFF6C5E3B884D3F09E6F5EA20A8C108F6AA5E707FF5553772A27DBE832F308EC9087803A88D383C85E64332EB9B39A33AFD1F767D',0); var plain = sm2.doDecrypt(cipher,'00AD019F316FD0F7676608A6A471ECE1CB1228F6C044B7D09FC9D4CADCABB65769',0); console.log('cipher',cipher); console.log('plain',plain); ciphers = ciphers+cipher+','; } debugger </script> </body> </html>