小程式資料簽名校正-Java端

來源:互聯網
上載者:User

標籤:exce   throws   deb   構造   com   encrypted   codebase   decrypt   mpi   

JDK內建的簽名演算法不包含小程式需要的(對稱解密使用的演算法為 AES-128-CBC,資料採用PKCS#7填充),所以需要引用第三方jar。

    compile group: ‘org.bouncycastle‘, name: ‘bcprov-jdk15on‘, version: ‘1.54‘

以下是具體的代碼:

  //自行在建構函式中賦值   String sessionKey; /**     * AES-128-CBC解密     * 使用PKCS填充     * @param encryptedData     * @param iv     * @return     * @throws Exception     */    public String decryptAesPkcData(String encryptedData, String iv) throws Exception {        //檢查sessionKey的長度        if (this.sessionKey.length() != 24) {            throw new AesException(AesException.IllegalAesKey);        }        //檢查iv的長度        if (iv.length() != 24) {            throw new AesException(AesException.IllegalAesKey);        }        byte[] aesKey = Base64.decodeBase64(this.sessionKey);        byte[] aesIV = Base64.decodeBase64(iv);        byte[] aesCipher = Base64.decodeBase64(encryptedData);        Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");        Key sKeySpec = new SecretKeySpec(aesKey, "AES");        AlgorithmParameters params = AlgorithmParameters.getInstance("AES");        params.init(new IvParameterSpec(aesIV));        cipher.init(Cipher.DECRYPT_MODE, sKeySpec, params);        return new String(cipher.doFinal(aesCipher), "utf-8");    }

 

小程式資料簽名校正-Java端

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.