日日操夜夜添-日日操影院-日日草夜夜操-日日干干-精品一区二区三区波多野结衣-精品一区二区三区高清免费不卡

公告:魔扣目錄網為廣大站長提供免費收錄網站服務,提交前請做好本站友鏈:【 網站目錄:http://www.ylptlb.cn 】, 免友鏈快審服務(50元/站),

點擊這里在線咨詢客服
新站提交
  • 網站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

本文介紹了蘋果eciesEncryptionCofactorVariableIVX963SHA256AESGCM VS BouncyCastle ECCDHwith SHA256KDF的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在嘗試使用Apple算法eciesEncryptionCofactorVariableIVX963SHA256AESGCM加密Java(BouncyCastle)和iOS之間的通信。

Apple的算法沒有很好的文檔記錄,但我找到了this article,這很有幫助。

我還在BouncyCastle documentation中找到了以下算法,似乎與我正在尋找的算法很接近:

ECCDHwithSHA256KDF表示使用帶有SHA256的X9.63 KDF作為PRF的EC輔因子DH

package com.example.ios.encryption;

import org.bouncycastle.jce.ECNamedCurveTable;
import org.bouncycastle.jce.ECPointUtil;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.jce.spec.ECNamedCurveSpec;
import org.bouncycastle.jce.spec.ECParameterSpec;
import org.bouncycastle.util.encoders.Base64;

import java.math.BigInteger;
import java.security.*;
import java.security.spec.ECPoint;
import java.security.spec.ECPrivateKeySpec;
import java.security.spec.ECPublicKeySpec;
import java.util.Arrays;

import javax.crypto.Cipher;
import javax.crypto.KeyAgreement;
import javax.crypto.SecretKey;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class IOSEncryptionECwithAES {

    public void testDecrypt() {

        // Receiver EC Public Key
        String pubKeyBase64 = "BBPT50Rn0PeeV0LxUbhDV7U1FUgVw9YLVctQx5HA+TiA3lp3k/cud8Xsjh6lytgaI5S7IUW1YouUiPNR/7LPArk=";
        PublicKey pubKey = getPublicKey(Base64.decode(pubKeyBase64));
        
        // Receiver EC Private Key
        String privateKeyBase64 = "BBPT50Rn0PeeV0LxUbhDV7U1FUgVw9YLVctQx5HA+TiA3lp3k/cud8Xsjh6lytgaI5S7IUW1YouUiPNR/7LPArkWcIYOQWtdkbTqmy++lz0cQ8ukWvUyhD9yzqZHPLQgQg==";
        PrivateKey privateKey = getPrivateKey(Base64.decode(privateKeyBase64));
    
        // Encrypted data
        String iosOutputBase64 = "BNNzHjSJQxP8jNuj5W9XSW0XNgpOlEHY/S4KzZQJFxwjzoujuwz5kJeOLj6cASBaYKePGLhkbE0qN20y8aHpU+PmeuDJWY7LZ25LjvutafOJGugdRZdURRwFSke7hzhXlSneaTFegT3xOoq9ffjCynwD7iRD";
        byte[] iosOutput = Base64.decode(iosOutputBase64);

        // Plaintext is a random UUID
        String plainText = "514227F0-51E9-41AC-9A39-42752E2ABADF";

        byte[] decryptedData = decryptEciesEncryptionCofactorVariableIVX963SHA256AESGCM(privateKey, iosOutput);
        System.out.println(new String(decryptedData));
    }

    public byte[] decryptEciesEncryptionCofactorVariableIVX963SHA256AESGCM(PrivateKey privateKey, byte[] iosOutput) throws Exception {

        // 1. Take ephemeral public key
        byte[] ephemeralKeyBytes = Arrays.copyOfRange(iosOutput, 0, 65);
        PublicKey ephemeralPublicKey = getPublicKey(ephemeralKeyBytes);
        byte[] encryptedData = Arrays.copyOfRange(iosOutput, 65, iosOutput.length);

        // 2. Key agreement using ECDH with Cofactor and integrated X9.63 
        byte[] kdfOut = getSharedSecret(ephemeralPublicKey, privateKey);

        byte[] secretKeyBytes = Arrays.copyOfRange(kdfOut, 0, 16);
        SecretKey secretKey = new SecretKeySpec(secretKeyBytes, "AES");

        // 4. Decrypt with AES key
        int tagLength = 128;
        byte[] iv = Arrays.copyOfRange(kdfOut, 16, kdfOut.length);
        GCMParameterSpec aesGcmParams = new GCMParameterSpec(tagLength, iv);
        Cipher c = Cipher.getInstance("AES/GCM/NoPadding");
        c.init(Cipher.DECRYPT_MODE, secretKey, aesGcmParams);
        byte[] decryptedData = c.doFinal(encryptedData);

        return decryptedData;
    }

    /**
     * Convert uncompressed public key into PublicKey using BouncyCastle
     * For an elliptic curve public key, the format follows the ANSI X9.63 standard
     * using a byte string of 04 || X || Y
     *
     * @param encodedBytes raw bytes received
     * @return the Elliptic-Curve Public Key based on curve SECP256R1
     */
    private PublicKey getPublicKey(byte[] encodedBytes) throws Exception {
        KeyFactory keyFactory = KeyFactory.getInstance("EC");
        ECParameterSpec ecParameterSpec = ECNamedCurveTable.getParameterSpec("secp256r1");
        ECNamedCurveSpec params = new ECNamedCurveSpec("secp256r1", ecParameterSpec.getCurve(), ecParameterSpec.getG(), ecParameterSpec.getN());
        ECPoint publicPoint =  ECPointUtil.decodePoint(params.getCurve(), encodedBytes);
        ECPublicKeySpec pubKeySpec = new ECPublicKeySpec(publicPoint, params);
        return keyFactory.generatePublic(pubKeySpec);
    }

  /**
   * Convert private key for external output from iOS
   * For an elliptic curve private key, the output is formatted as the public key
   * concatenated with the big endian encoding of the secret scalar, or 04 || X || Y || K.
   *
   * @param encodedBytes raw bytes received
   * @return the Elliptic-Curve Private Key based on curve SECP256R1
   */
  private PrivateKey getPrivateKey(byte[] encodedBytes) throws Exception {
        BigInteger s = new BigInteger(Arrays.copyOfRange(encodedBytes, 65, encodedBytes.length));
        ECParameterSpec ecParameterSpec = ECNamedCurveTable.getParameterSpec("secp256r1");
        ECNamedCurveSpec params = new ECNamedCurveSpec("secp256r1", ecParameterSpec.getCurve(), ecParameterSpec.getG(), ecParameterSpec.getN());
        ECPrivateKeySpec privateKeySpec = new ECPrivateKeySpec(s, params);
        KeyFactory keyFactory = KeyFactory.getInstance("EC");
        return keyFactory.generatePrivate(privateKeySpec);
  }

  /**
   * Key agreement using ECDH with Cofactor and integrated X9.63 KDF SHA-256
   * 
   * @param ephemeralPublicKey created by the sender
   * @param privateKey from the receiver
   * @return shared secret of 32-bytes containing the 128-bit AES key and 16-byte IV
   */
   private byte[] getSharedSecret(PublicKey ephemeralPublicKey, PrivateKey privateKey) throws Exception {
      String keyAgreementAlgorithm = "ECCDHwithSHA256KDF";
      KeyAgreement keyAgreement = KeyAgreement.getInstance(keyAgreementAlgorithm, new BouncyCastleProvider());
      keyAgreement.init(privateKey);
      keyAgreement.doPhase(ephemeralPublicKey, true);
      return keyAgreement.generateSecret();
  }
}

遺憾的是,這不起作用并導致異常。

javax.crypto.AEADBadTagException: Tag mismatch!

    at java.base/com.sun.crypto.provider.NativeGaloisCounterMode.decryptFinal(NativeGaloisCounterMode.java:454)

我錯過了什么?是否可以對此代碼稍作更改即可修復?

推薦答案

我確實通過使用O2 Czech Republic中的代碼成功地實現了這一點,這確實可以工作,但該代碼是2017年的代碼,我希望現在使用當前版本的BouncyCastle可以用更少的代碼行來完成這項工作。

請參閱下面的工作代碼。它將KeyAgreement和KeyDerivationFunction分成兩個單獨的函數。

   /**
     * Key agreement using ECDH with Cofactor and integrated X9.63 KDF SHA-256
     *
     * @param ephemeralPublicKey created by the sender
     * @param privateKey from the receiver
     * @return shared secret of 32-bytes containing the 128-bit AES key and 16-byte IV
     */
    private byte[] getInitialSecret(PublicKey ephemeralPublicKey, PrivateKey privateKey) throws Exception {
        String keyAgreementAlgorithm = "ECCDH"; // Seems to be equivalent to "ECDHC"
        KeyAgreement keyAgreement = KeyAgreement.getInstance(keyAgreementAlgorithm, new BouncyCastleProvider());
        keyAgreement.init(privateKey);
        keyAgreement.doPhase(ephemeralPublicKey, true);
        return keyAgreement.generateSecret();
    }

    /**
     * Derive actual SecretKey with X9.63 KDF SHA-256
     *
     * @param initialSecret output from the ECDH agreement with Cofactor
     * @param ephemeralKeyBytes emphemeral public key from sender
     * @return shared secret of 32-bytes containing the 128-bit AES key and 16-byte IV
     */
    private byte[] getDerivation(byte[] initialSecret, byte[] ephemeralKeyBytes) {
        KDF2BytesGenerator kdfGenerator = new KDF2BytesGenerator(new SHA256Digest());
        kdfGenerator.init(new KDFParameters(initialSecret, ephemeralKeyBytes));
        byte[] kdfOut = new byte[32]; 
        kdfGenerator.generateBytes(kdfOut, 0, 32);
        return kdfOut;
    }
  }

和更改

byte[] kdfOut = getSharedSecret(ephemeralPublicKey, privateKey);

進入

byte[] initialSecret = getInitialSecret(ephemeralPublicKey, privateKey);
byte[] kdfOut = getDerivation(initialSecret, ephemeralKeyBytes);

這篇關于蘋果eciesEncryptionCofactorVariableIVX963SHA256AESGCM VS BouncyCastle ECCDHwith SHA256KDF的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,

分享到:
標簽:BouncyCastle ECCDHwith eciesEncryptionCofactorVariableIVX963SHA256AESGCM SHA256K
用戶無頭像

網友整理

注冊時間:

網站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網站吧!
最新入駐小程序

數獨大挑戰2018-06-03

數獨一種數學游戲,玩家需要根據9

答題星2018-06-03

您可以通過答題星輕松地創建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學四六

運動步數有氧達人2018-06-03

記錄運動步數,積累氧氣值。還可偷

每日養生app2018-06-03

每日養生,天天健康

體育訓練成績評定2018-06-03

通用課目體育訓練成績評定