使用DiffieHellman生成的SecretKey的BadPaddingException

时间:2015-11-11 15:38:03

标签: java android encryption

我看过很多问题/答案,但没有人适合我。问题是,当我想要解密文本时,我会抛出BadPaddingException并且不知道原因。

以下是我加密文字的代码:

            KeyAgreement keyAgreement = this.getSecretKeyAgreement(publicOtherUserKey, privateOwnKey);
            SecretKey secretKey = new SecretKeySpec(keyAgreement.generateSecret(), "AES");
            Cipher aesCipher = null;
            aesCipher = Cipher.getInstance("AES");
            aesCipher.init(Cipher.ENCRYPT_MODE, secretKey);
            byte[] byteDataToEncrypt = text.getBytes();
            byte[] byteCipherText = aesCipher.doFinal(byteDataToEncrypt);
            byte[] encodedBytes = Base64.encodeBase64(byteCipherText);
            textEncrypted = new String(encodedBytes);

使用Diffie-Hellman协议生成的“publicOtherUserKey”和“privateOwnKey”。

这是解密文本的代码,抛出BadPaddingException

        KeyAgreement keyAgreement = this.getSecretKeyAgreement(publicOtherUserKey, privateOwnKey);
        byte[] encodedBytes = text.getBytes();
        SecretKey secretKey = new SecretKeySpec(keyAgreement.generateSecret(), "AES");
        byte[] decodedBytes = Base64.decodeBase64(encodedBytes);
        Cipher decrypt = Cipher.getInstance("AES");
        decrypt.init(Cipher.DECRYPT_MODE, secretKey);
        textDecrypted = new String(decrypt.doFinal(decodedBytes));

其中“publicOtherUserKey”和“privateOwnKey”使用Diffie-Hellman协议生成,“text”是加密文本。

你能帮助我吗?

修改

重要的是要提到所有的密钥和文本都是用Base64编码的

编辑2

与Diffie-Hellman开始密钥交换的代码

    int bitLength = 256; // 256 bits
    SecureRandom rnd = new SecureRandom();
    BigInteger p = BigInteger.probablePrime(bitLength, rnd);
    BigInteger g = BigInteger.probablePrime(bitLength, rnd);

    DHParameterSpec dhParams = new DHParameterSpec(p, g);
    KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DH", "BC");
    keyGen.initialize(dhParams, new SecureRandom());

    KeyAgreement aKeyAgree = KeyAgreement.getInstance("DH", "BC");
    KeyPair aPair = keyGen.generateKeyPair();
    aKeyAgree.init(aPair.getPrivate());

    byte[] aPairPrivateKey = aPair.getPrivate().getEncoded();
    byte[] encodedBytesPrivateKey = Base64.encodeBase64(aPairPrivateKey);
    String privateKey = new String(encodedBytesPrivateKey);

    byte[] aPairPublicKey = aPair.getPublic().getEncoded();
    byte[] encodedBytesPublicKey = Base64.encodeBase64(aPairPublicKey);
    String publicKey = new String(encodedBytesPublicKey);

其中“publicKey”和“privateKey”是稍后生成密钥的密钥。 “publicKey”是发给其他用户的密钥。 “p”和“g”也是生成密钥的数字。

完成交换的代码:

    DHParameterSpec dhParams = new DHParameterSpec(p, g);
    KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DH", "BC");
    keyGen.initialize(dhParams, new SecureRandom());

    KeyAgreement bKeyAgree = KeyAgreement.getInstance("DH", "BC");
    KeyPair bPair = keyGen.generateKeyPair();
    bKeyAgree.init(bPair.getPrivate());

    byte[] userPublicBytesBase64 = base64EncodedPublicKey.getBytes();
    byte[] userPublicKey = Base64.decodeBase64(userPublicBytesBase64);

    X509EncodedKeySpec keySpec = new X509EncodedKeySpec(userPublicKey);
    KeyFactory keyFactory = KeyFactory.getInstance("DH");
    PublicKey aPublicKey = keyFactory.generatePublic(keySpec);
    bKeyAgree.doPhase(aPublicKey, true);

    final byte[] bPairPrivateKey = bPair.getPrivate().getEncoded();
    byte[] encodedBytesPrivateKey = Base64.encodeBase64(bPairPrivateKey);
    String privateKey = new String(encodedBytesPrivateKey);

    final byte[] bPairPublicKey = bPair.getPublic().getEncoded();
    byte[] encodedBytesPublicKey = Base64.encodeBase64(bPairPublicKey);
    String publicKey = new String(encodedBytesPublicKey);

“base64EncodedPublicKey”是在Code的第一个块(“publicKey”)中生成的Key,而“p”和“g”也是在Code的第一个块中生成的素数。

0 个答案:

没有答案