vue 核心加解密工具類 方法

HiEagle發表於2021-12-28
  1  /* base64 加解密
  2   */
  3  export let Base64 = require('js-base64').Base64
  4 
  5  /* md5 加解密
  6  */
  7  export let crypto = require('crypto');
  8  export let md5 = require('js-md5');
  9  export let CryptoJS = require('crypto-js');
 10  export let MD5 = CryptoJS.MD5;
 11  /*
 12   *引入jsencrypt實現資料RSA加密
 13   */
 14  import JSEncrypt from 'jsencrypt';
 15  // jsencrypt.js處理長文字資料時報錯  Message too long for RSA
 16  // encryptlong是基於jsencrypt擴充套件的長文字分段加解密功能。
 17  import Encrypt from "encryptlong";
 18  // rsa sign
 19  import jsrsasign from 'jsrsasign'
 20 
 21  // Message Digest algorithm 5,資訊摘要演算法
 22  // alglorithm:md5、sha1、sha256
 23  export function Md5(plainText, alglorithm, encoding){
 24    const hash =  crypto.createHash(alglorithm)
 25    hash.update(plainText);//加密內容
 26    return  hash.digest(encoding);//密文
 27  }
 28 
 29  //Hash Message Authentication Code,雜湊訊息鑑別碼
 30  //Secure Hash Algorithm,安全雜湊演算法
 31  // alglorithm:md5、sha256、sha1
 32  export function HMac(plainText, secretKey,alglorithm, encoding){
 33    const hmac= crypto.createHmac(alglorithm, secretKey);
 34    const cipherText= hmac.update(plainText);//加密內容
 35    return  cipherText.digest(encoding);//密文
 36  }
 37 
 38  //Data Encryption Standard,資料加密演算法
 39  // DES/DES3/AES 加密, key與iv長度必須是8的倍數
 40  //mode:CryptoJS.mode.CBC、CryptoJS.mode.ECB、CryptoJS.mode.CFB
 41  //padding:CryptoJS.pad.ZeroPadding、CryptoJS.pad.Pkcs7、CryptoJS.pad.NoPadding
 42  export function encrypt ( algorithm, plainText,key, iv, mode, padding, isBase64) {
 43      key = key ? key : "abcdefghijklmnop";
 44      iv = iv ? iv : "0102030405060708";
 45 
 46      const keyHex = CryptoJS.enc.Utf8.parse(key);
 47      const ivHex = CryptoJS.enc.Utf8.parse(iv);
 48      const option = { iv:keyHex,mode: mode, padding: padding }
 49      let encrypted = null  ;
 50      if(algorithm === "TripleDES"){
 51        encrypted = CryptoJS.TripleDES.encrypt(plainText, keyHex, option)
 52      }else if(algorithm === "DES"){
 53        encrypted = CryptoJS.DES.encrypt(plainText, keyHex, option)
 54      }
 55      else if(algorithm === "AES"){
 56        encrypted =  CryptoJS.AES.encrypt(plainText, keyHex, option)
 57      }
 58      return isBase64?CryptoJS.enc.Base64.stringify(encrypted.ciphertext):encrypted.ciphertext.toString();
 59  }
 60 
 61  // DES/DES3/AES解密,key與iv長度必須是8的倍數
 62  export function decrypt (algorithm,cipherText,key, iv, mode, padding, isBase64) {
 63      key = key ? key : "abcdefghijklmnop";
 64      iv = iv ? iv : "0102030405060708";
 65 
 66      const keyHex = CryptoJS.enc.Utf8.parse(key);
 67      const ivHex = CryptoJS.enc.Utf8.parse(iv);
 68      const decryptText = isBase64?  CryptoJS.enc.Base64.parse(cipherText):cipherText;
 69      const textHex = { ciphertext:  isBase64?decryptText:CryptoJS.enc.Hex.parse(decryptText) }
 70      const option = { iv:ivHex,mode: mode, padding: padding }
 71      let decrypted = null;
 72      if(algorithm === "TripleDES"){
 73        decrypted = CryptoJS.TripleDES.decrypt(textHex, keyHex, option);
 74      }else if(algorithm === "DES"){
 75        decrypted = CryptoJS.DES.decrypt(textHex, keyHex, option);
 76      }
 77      else if(algorithm === "AES"){
 78        decrypted =  CryptoJS.AES.decrypt(textHex, keyHex, option);
 79      }
 80      return decrypted.toString(CryptoJS.enc.Utf8);
 81  }
 82 
 83  /** RSA 加密過程
 84   * (1)A生成一對金鑰(公鑰和私鑰),私鑰不公開,A自己保留。公鑰為公開的,任何人可以獲取。
 85   * (2)A傳遞自己的公鑰給B,B用A的公鑰對訊息進行加密。
 86   * (3)A接收到B加密的訊息,利用A自己的私鑰對訊息進行解密。
 87   *  在這個過程中,只有2次傳遞過程,第一次是A傳遞公鑰給B,第二次是B傳遞加密訊息給A,即使都被敵方截獲,也沒有危險性。
 88   *  因為只有A的私鑰才能對訊息進行解密,防止了訊息內容的洩露。
 89   *  使用方法
 90   *  客戶端初始化訪問伺服器端時,伺服器端會生成一對RSA對,及公鑰和金鑰。
 91   *  如果前端只需要將要傳給後端的資料進行加密後傳輸,那麼前端可以只要公鑰,通過公鑰對要傳輸的引數進行加密後把加密的字串發給後端,後端取出儲存的密碼種子或者直接儲存的私鑰,採用私鑰對加密字串進行解密,得到明文。
 92   *  如果前端要獲取後端傳過來的已經加密後的字串,並且解密使用,那麼前端就需要拿到RSA對立面的私鑰進行解密後使用了。
 93   * */
 94  /* JSEncrypt 公鑰加密  padding:pkcs1pad2 */
 95  export function RsaJSEncrypt(plainText,publicKey) {
 96    const jsencrypt = new JSEncrypt({
 97      default_key_size: 1024
 98    });
 99    jsencrypt.setPublicKey(publicKey);
100    // 如果是物件/陣列的話,需要先JSON.stringify轉換成字串
101    // 處理中文亂碼,伺服器端:String result = java.net.URLDecoder.decode(cipherText ,"UTF-8");
102    const cipherText = encodeURIComponent(jsencrypt.encrypt(plainText));
103    return  cipherText;
104  }
105 
106  /* JSEncrypt 私鑰解密 padding:pkcs1pad2 */
107  export function RsaJSDecrypt(cipherText,privateKey) {
108    const jsencrypt = new JSEncrypt({
109      default_key_size: 1024
110    });
111    jsencrypt.setPrivateKey(privateKey);
112    return jsencrypt.decrypt(decodeURIComponent(cipherText));
113  }
114 
115  /* 長文字分段加密 */
116  export function RsaEncrypt(plainText,publicKey) {
117    const encryptor = new Encrypt();
118    encryptor.setPublicKey(publicKey);
119    // 處理中文亂碼,伺服器端:String result = java.net.URLDecoder.decode(cipherText ,"UTF-8");
120    const cipherText = encodeURIComponent(encryptor.encryptLong(plainText));
121    return  cipherText;
122  }
123 
124  /* 長文字分段解密 */
125  export function RsaDecrypt(cipherText,privateKey) {
126    var encryptor = new Encrypt();
127    encryptor.setPrivateKey(privateKey);
128    return encryptor.decryptLong(decodeURIComponent(cipherText));
129  }
130 
131  // 獲取簽名 privateKey_s 伺服器端的私鑰
132  export function RsaSign(plainText,privateKey_s,format_key, alglorithm,isBase64)
133  {
134    // 生成簽名物件
135    let sign = genSign(privateKey_s,format_key, alglorithm);
136 
137     plainText = genDigest(plainText,alglorithm);
138     sign.updateString(plainText);
139 
140     // +號伺服器端不識別,url編碼
141     return isBase64? encodeURIComponent(jsrsasign.hextob64(sign.sign())):encodeURIComponent(sign.sign());
142  }
143 
144  // 驗證簽名 publicKey_s 伺服器端的公鑰
145  // alglorithm: SHA1withRSA、MD5withRSA、SHA256withRSA、 SHA384withRSA、SHA512withRSA、RIPEMD160withRSA
146  // format_key: PKCS#1、PKCS#5、PKCS#8
147  /*
148   * PKCS#1:定義RSA公開金鑰演算法加密和簽名機制,主要用於組織PKCS#7中所描述的數字簽名和數字信封。
149   * PKCS#3:定義Diffie-Hellman金鑰交換協議。
150   * PKCS#5:描述一種利用從口令派生出來的安全金鑰加密字串的方法。使用MD2或MD5 從口令中派生金鑰,並採用DES-CBC模式加密。主要用於加密從一個計算機傳送到另一個計算機的私人金鑰,不能用於加密訊息[24]。
151   * PKCS#6:描述了公鑰證照的標準語法,主要描述X.509證照的擴充套件格式。
152   * PKCS#7:定義一種通用的訊息語法,包括數字簽名和加密等用於增強的加密機制,PKCS#7與PEM相容,所以不需其他密碼操作,就可以將加密的訊息轉換成PEM訊息[26]。
153   * PKCS#8:描述私有金鑰資訊格式,該資訊包括公開金鑰演算法的私有金鑰以及可選的屬性集等。
154   * PKCS#9:定義一些用於PKCS#6證照擴充套件、PKCS#7數字簽名和PKCS#8私鑰加密資訊的屬性型別。
155   * PKCS#10:描述證照請求語法。
156   * PKCS#11:稱為Cyptoki,定義了一套獨立於技術的程式設計介面,用於智慧卡和PCMCIA卡之類的加密裝置。
157   * PKCS#12:描述個人資訊交換語法標準。描述了將使用者公鑰、私鑰、證照和其他相關資訊打包的語法。
158   * PKCS#13:橢圓曲線密碼體制標準。
159   * PKCS#14:偽隨機數生成標準。
160   * PKCS#15:密碼令牌資訊格式標準。
161   */
162  export function RsaVerifySign(plainText,signedText,publicKey_s,format_key, alglorithm,isBase64)
163  {
164    // 生成簽名
165    let verifySign = genSign(publicKey_s,format_key, alglorithm);
166    // 根據明文生成摘要
167    let digestText = genDigest(plainText,alglorithm);
168 
169    verifySign.updateString(digestText);
170 
171    return  verifySign.verify(isBase64?jsrsasign.b64tohex(decodeURIComponent(signedText)):decodeURIComponent(signedText));
172  }
173 
174  // 根據明文生成摘要
175  //SHA1withRSA、MD5withRSA、SHA256withRSA、 SHA384withRSA、SHA512withRSA、RIPEMD160withRSA
176  export function genDigest(plainText,alglorithm, ){
177    let option = { "alg": alglorithm.split('w')[0], "prov":"cryptojs/jsrsa", }
178    let text = new jsrsasign.KJUR.crypto.MessageDigest(option);   // 摘要
179    text.updateString(plainText);
180    return text.digest();
181  }
182 
183  /* 生成rsa簽名物件
184   * */
185  export function genSign(RsaKey_s,format_key, alglorithm)
186  {
187    //私鑰要寫開頭和結束
188    // var private_key = '-----BEGIN PRIVATE KEY-----' + privateKey_s + '-----END PRIVATE KEY-----'
189    // 讀取解析pem格式的祕鑰, 生成祕鑰例項 (RSAKey)
190    let rsaKey = new jsrsasign.RSAKey();
191    if (format_key === "PKCS#1" || format_key === "PKCS#5"|| format_key === "PKCS#7"|| format_key === "PKCS#8") {
192      rsaKey = jsrsasign.KEYUTIL.getKey(RsaKey_s);
193      // rsaSign.readPrivateKeyFromPEMString(privateKey_s);
194    }
195 
196    let option= {
197      "alg":alglorithm,
198      "prov":"cryptojs/jsrsa",
199      "prvkeypem": rsaKey
200    };
201 
202    let sign = new jsrsasign.KJUR.crypto.Signature(option);
203    sign.init(rsaKey);
204 
205    return sign;
206  }

測試程式碼:

  1    // 測試工具:http://www.1818288.com/
  2       console.log("12345678漢字abcdef,md5="+this.$md5("12345678漢字abcdef"));
  3       console.log("12345678,MD5="+this.$MD5("12345678"));
  4       console.log("12345678,Md5="+this.$Md5("12345678","md5","hex"));
  5       console.log("12345678,HMac-MD5="+this.$HMac("12345678漢字abcdef","abcdef","md5","hex"));
  6       console.log("12345678,enDES="+this.$Encrypt("DES","12345678","abcdef","abcdef",this.$CryptoJS.mode.CBC,this.$CryptoJS.pad.ZeroPadding,true));
  7       console.log("12345678,deDES="+this.$Decrypt("DES","CQlZsExZRQM=","abcdef","abcdef",this.$CryptoJS.mode.CBC,this.$CryptoJS.pad.ZeroPadding,true));
  8 
  9       console.log("12345678,enDES3="+this.$Encrypt("TripleDES","12345678","abcdef","abcdef",this.$CryptoJS.mode.ECB,this.$CryptoJS.pad.Pkcs7,true));
 10       console.log("12345678,deDES3="+this.$Decrypt("TripleDES","Wb6LDNctz2HpGd1QX22oNQ==","abcdef","abcdef",this.$CryptoJS.mode.ECB,this.$CryptoJS.pad.Pkcs7,true));
 11 
 12       console.log("12345678,enAES="+this.$Encrypt("AES","12345678","","",this.$CryptoJS.mode.ECB,this.$CryptoJS.pad.Pkcs7,false));
 13       console.log("12345678,deAES="+this.$Decrypt("AES","5a9906baab69bdcd17a0f925cbb3dbb3","","",this.$CryptoJS.mode.ECB,this.$CryptoJS.pad.Pkcs7,false));
 14 
 15       console.log("12345678,enAES="+this.$Encrypt("AES","12345678","","",this.$CryptoJS.mode.ECB,this.$CryptoJS.pad.Pkcs7,true));
 16       console.log("12345678漢字abcdef,deAES="+this.$Decrypt("AES","WpkGuqtpvc0XoPkly7Pbsw==","","",this.$CryptoJS.mode.ECB,this.$CryptoJS.pad.Pkcs7,true));
 17 
 18 
 19 
 20 
 21       console.log("12345678漢字abcdef,RSAEn_pubK="+this.$RsaEncrypt("12345678漢字abcdef","MIGeMA0GCSqGSIb3DQEBAQUAA4GMADCBiAKBgFrtUneGIrf/hc7l9JUR+OCCDWqq\n" +
 22         "ttX2pItsbg0fpZ6Zl0rCoZwuaRc5ltoVhIPvT2HEUYJrUmNwamtfXqZaxkvOtxpc\n" +
 23         "GWB8nR3r9XTSE3/iuI7jMvXqP5HiWWoiT8IKwxaa3P5pu5UF5q/2t+/8XGeiGAoc\n" +
 24         "WLiZ/HPpeZyQVUfXAgMBAAE="));
 25 
 26       console.log("12345678漢字abcdef,RSADe_priK="+this.$RsaDecrypt(
 27         "LbBdgV0HR0hmA1QTLTQdVugBlc%2FmmuAXJ51aTZnMukv5jlCBftVRWmyjk1UUntLzSRZV0J6arcbwv0i7qoLLgpYWQQfn700QcS0hp5GNyFxshZf%2FOHdJxzRcpSoCd7%2B%2F9NpAChLYcWYRBynHRZXXHb9fK6rDrXLK%2FIkBDaQItTM%3D","MIICWgIBAAKBgFrtUneGIrf/hc7l9JUR+OCCDWqqttX2pItsbg0fpZ6Zl0rCoZwu\n" +
 28         "aRc5ltoVhIPvT2HEUYJrUmNwamtfXqZaxkvOtxpcGWB8nR3r9XTSE3/iuI7jMvXq\n" +
 29         "P5HiWWoiT8IKwxaa3P5pu5UF5q/2t+/8XGeiGAocWLiZ/HPpeZyQVUfXAgMBAAEC\n" +
 30         "gYAbawi9BfOeLCz7MJhKAzta55yIF6Y+ars/V5jv2CUm6AWZA4ub/tqtqC324Rou\n" +
 31         "dpEnV/2EKDyVw6YzbnSqESFOCVNYOslqSLhSStkuvBYo3Tr1/8ITt1JKxcqS6Lix\n" +
 32         "VVp4zl+MgBvYNOv1pbubsc76lMB1TirFN6U0iFsPKuBrgQJBALTqGJjbgI/Idvsi\n" +
 33         "Z5ycE3kyD+ZLOYCDY3/GKh8xcD93iXRIj5AY4vA6Tqnb56eDY4GvXGEQaJpNAJ9z\n" +
 34         "Ukq4G6ECQQCAqjEfELqDA2dLdV9fojYxq1j/PGlBUWlt0+8cEsOBTbMfD9Kt5diM\n" +
 35         "b1Ut213I2z07ZFNHDouBOK8x4iDYanB3AkASchPT8f1nq47GCDn8M8Wgg8QvPNg+\n" +
 36         "DCdm7bN5vratIej33DYXARKSX7+Jfgxeffz1SgXnN58SrGs11rI7QA5BAkB0BwZd\n" +
 37         "GVzYoM0ZwzLKryFoETkkknnoqV4F6MUL7+/WPxd0v3Qh1dA+dHe/7r7YTr0dPg/L\n" +
 38         "WFZ9pXx344/zGalpAkB8QBNZej44DU+nAi6KQVEbLxOps1BbyzzWfXI2uQRR+w52\n" +
 39         "ABLacpwutsHC8jLjUDaCLfFUaEnpkBWW8u0pI54v"));
 40 
 41 
 42       console.log("12345678漢字abcdef,RSAJSEn_pubK="+this.$RsaJSEncrypt("12345678漢字abcdef","MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDNpEYv5DxAXWOg0HA1y0DROLiKfHTZJx/4TTWhrVNDSgNeR4iVT7fekbhRDYy0oHVDAS8Sy6yJDqMAPjRNmjrICgVuMwYzwXg2LRNy6SrB79DOz5X5vBPP6Lc+8eNt4mm0S+I1sba3tyueTXPaA9aMCyJjebKrsxwa8rG9K/OlqwIDAQAB"));
 43 
 44       console.log("12345678漢字abcdef,RSAJSDe_priK="+this.$RsaJSDecrypt("DryU8wEn2yEG5HlkaJ2WeR5QYh%2FLhvMU3IwUsrN1OufDK5jlkSjMufgR%2BUruyTutj5N7r7MxXLXOH11VK%2BzBdr%2BIbK9Du54vQGhj2aERIUYqCKp0tk02qhCXWwl7XDYeusiFfvQ4J%2Fr%2BrEZolk%2FdA4WxOtq7QJf59fhPrxpf4YI%3D","MIICXQIBAAKBgQDNpEYv5DxAXWOg0HA1y0DROLiKfHTZJx/4TTWhrVNDSgNeR4iV\n" +
 45         "T7fekbhRDYy0oHVDAS8Sy6yJDqMAPjRNmjrICgVuMwYzwXg2LRNy6SrB79DOz5X5\n" +
 46         "vBPP6Lc+8eNt4mm0S+I1sba3tyueTXPaA9aMCyJjebKrsxwa8rG9K/OlqwIDAQAB\n" +
 47         "AoGAPQoe30ZS8BQIZ4qXVBBhy/iXJlws3uwzHCPm4HevOFD20PxQuWkxPbppSH3K\n" +
 48         "Hy9lvx7R1zVnoEMc9QyBv+sguQkNir722Hk8+BhMXxgb0eZPsSBUgXQmciHscRix\n" +
 49         "52myI/tRRSbpuW/Qx4TYDeP/xUvDOgx4XnJ8v5Dy5bblFNkCQQD83ldtZpJHBsZ3\n" +
 50         "rzzU+4xkUsA5W7rMmNK94N37Cu2hoFofk/9gfyG2BCDCUwBSfeQWJk0U2RaEsItm\n" +
 51         "CBqn0cYFAkEA0DA2IBp5ECUDJhmdmgWfqi/NZGwLvBedWFY9hdQJ0auF3wOTeVSI\n" +
 52         "fs1oBM+DKmHxb1oavZ8MaYFx6tQ2LFhb7wJAJV1lZdEGRGCH6x+FyaLx99ESfmdD\n" +
 53         "jv/2BAC6TGItAvkC2C+ySLwAazjo7k39ejMIBQ3bzfzWCv1bW9ROvAWPUQJBAIIf\n" +
 54         "QBU8tqJSn+82X30fOrZiflvf9E2x7NSDwpxc9W4paNkFIS3amh23QOaQ1Qiugvr4\n" +
 55         "NHKEcHuZPpxp2xpId90CQQDeT8TK9ClvWutbj8fpSYGDFyyaf1qLPwBNpozXUMGh\n" +
 56         "hNuOF+69LlAeH5p26omFjQUMfk7r0LjDdBrcoRhpbmkG"));
 57 
 58 
 59       console.log("12345678漢字abcdef,RSASign_priK="+this.$RsaSign("12345678漢字abcdef","-----BEGIN PRIVATE KEY-----\n" +
 60         "MIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBAOihfrud6pbH6y9m\n" +
 61         "MGpG870wcZyKqwmedUmQH1cwsyOiJZZib0J4IlxRq6yTAuLRnXHM1JBnvvJl6oBH\n" +
 62         "tvM6vSj6kSDY0SCuoARdp1oJH7BXLTcEVXIbGxD+/6ZcapydzcnBMtkHpG+pgf9c\n" +
 63         "lf57z/aqewEAq00Z+rH+yWuyggITAgMBAAECgYEAnacEeiWVG8Y0jpTUbtHS0P4J\n" +
 64         "oKshKo2mssnnU3F/lcCHdJkVCZjcwG/gxj2DETA5YSwNSifk8eU7vN/dDo1aLyZn\n" +
 65         "+qc9rot+jHW+z8hXHWm+RbC8CkD7KMjd2RlRBcnSXieoUqqKyrwYL2JPecG22CFp\n" +
 66         "QcvbAPT0ITor0lKNt4ECQQD7OGHxzM4DfG+tXj3RvWgk7I6lcHL4s7ad3hdkjkVe\n" +
 67         "x+fHKybiCzl4o9dXS9iObw4xg54k7sKYs+S+6AsNKlazAkEA7Q6Rscpocw+dYQMv\n" +
 68         "SdLIwB3eqUkcVxbvj9C/hFm5LqzvAMhcGQBp4Hp9OHILuqEYqFxuyIbCOyhbBJMo\n" +
 69         "bBJXIQJBAM74F7FnYv76QOObNeEbY7av8aGZMA93t+b991XP5JT+qxSql4E5cCLV\n" +
 70         "EO+JvCM0mYPvhJW24zl9U+J4N8qj7AMCQA9VROk6DV174hMXUKS2xJNgmzlZ6EJd\n" +
 71         "0mr79pdJ8mdpeiAT1ZksZKzHX3gaMFYG+4BQUj4YMVIAPzrhhdjPquECQQDBFWVi\n" +
 72         "ByzngHyXCuVpDN3FKZ26Cnqt8mY2kW8N+4/okQ+rPzxt86TywPerIGR1qAl5PgDS\n" +
 73         "gMZzSWvMDuT8369m\n" +
 74         "-----END PRIVATE KEY-----","PKCS#8","MD5withRSA",true));
 75 
 76 
 77       console.log("12345678漢字abcdef,RSAVerifySign_pubK="+this.$RsaVerifySign("12345678漢字abcdef","VLFvc958umN%2FORw4Bb9kzsePgaar5gyW8VWpoAqETZJYKsulZFdBHT%2BQihD%2BEXufg7Ml%2BYp1gxtK5BEVWdfBS39v%2FRDGM8kYTz9aSi8HiSPAylm6d8KkqY61aXy%2F2GX%2FuRgycjRFklo5w9W8irYXELSXeBJFeEQmgsE3hMQIui4%3D","-----BEGIN PUBLIC KEY-----\n" +
 78         "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDooX67neqWx+svZjBqRvO9MHGc\n" +
 79         "iqsJnnVJkB9XMLMjoiWWYm9CeCJcUauskwLi0Z1xzNSQZ77yZeqAR7bzOr0o+pEg\n" +
 80         "2NEgrqAEXadaCR+wVy03BFVyGxsQ/v+mXGqcnc3JwTLZB6RvqYH/XJX+e8/2qnsB\n" +
 81         "AKtNGfqx/slrsoICEwIDAQAB\n" +
 82         "-----END PUBLIC KEY-----","PKCS#8","MD5withRSA",true));
 83 
 84 
 85       console.log("12345678漢字abcdef,RSASign_priK="+this.$RsaSign("12345678漢字abcdef","-----BEGIN PRIVATE KEY-----\n" +
 86         "MIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBAOihfrud6pbH6y9m\n" +
 87         "MGpG870wcZyKqwmedUmQH1cwsyOiJZZib0J4IlxRq6yTAuLRnXHM1JBnvvJl6oBH\n" +
 88         "tvM6vSj6kSDY0SCuoARdp1oJH7BXLTcEVXIbGxD+/6ZcapydzcnBMtkHpG+pgf9c\n" +
 89         "lf57z/aqewEAq00Z+rH+yWuyggITAgMBAAECgYEAnacEeiWVG8Y0jpTUbtHS0P4J\n" +
 90         "oKshKo2mssnnU3F/lcCHdJkVCZjcwG/gxj2DETA5YSwNSifk8eU7vN/dDo1aLyZn\n" +
 91         "+qc9rot+jHW+z8hXHWm+RbC8CkD7KMjd2RlRBcnSXieoUqqKyrwYL2JPecG22CFp\n" +
 92         "QcvbAPT0ITor0lKNt4ECQQD7OGHxzM4DfG+tXj3RvWgk7I6lcHL4s7ad3hdkjkVe\n" +
 93         "x+fHKybiCzl4o9dXS9iObw4xg54k7sKYs+S+6AsNKlazAkEA7Q6Rscpocw+dYQMv\n" +
 94         "SdLIwB3eqUkcVxbvj9C/hFm5LqzvAMhcGQBp4Hp9OHILuqEYqFxuyIbCOyhbBJMo\n" +
 95         "bBJXIQJBAM74F7FnYv76QOObNeEbY7av8aGZMA93t+b991XP5JT+qxSql4E5cCLV\n" +
 96         "EO+JvCM0mYPvhJW24zl9U+J4N8qj7AMCQA9VROk6DV174hMXUKS2xJNgmzlZ6EJd\n" +
 97         "0mr79pdJ8mdpeiAT1ZksZKzHX3gaMFYG+4BQUj4YMVIAPzrhhdjPquECQQDBFWVi\n" +
 98         "ByzngHyXCuVpDN3FKZ26Cnqt8mY2kW8N+4/okQ+rPzxt86TywPerIGR1qAl5PgDS\n" +
 99         "gMZzSWvMDuT8369m\n" +
100         "-----END PRIVATE KEY-----","PKCS#1","SHA1withRSA",true));
101 
102 
103       console.log("12345678漢字abcdef,RSAVerifySign_pubK="+this.$RsaVerifySign("12345678漢字abcdef","jpSg6D%2BOE0sSRRKZi7EHngMMwF73hHH4FGQ9YPKdjJzX2pNRYeFgraFYGB4s80tDKv4wkD2P8W06tpWfR4uRfF0Au0nmC1%2FHogi%2FEYTpZNHQlJLMYrLtv6NmQYOidOUWly1iu2NCUuUCFLNNqyARHz%2Bca0e2JZtQmqwBKL8DRIk%3D","-----BEGIN PUBLIC KEY-----\n" +
104         "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDooX67neqWx+svZjBqRvO9MHGc\n" +
105         "iqsJnnVJkB9XMLMjoiWWYm9CeCJcUauskwLi0Z1xzNSQZ77yZeqAR7bzOr0o+pEg\n" +
106         "2NEgrqAEXadaCR+wVy03BFVyGxsQ/v+mXGqcnc3JwTLZB6RvqYH/XJX+e8/2qnsB\n" +
107         "AKtNGfqx/slrsoICEwIDAQAB\n" +
108         "-----END PUBLIC KEY-----","PKCS#1","SHA1withRSA",true));

測試結果:

 

相關文章