java–BadPaddingException:无效的密文

java–BadPaddingException:无效的密文,第1张

概述我想要一些帮助,因为这是我第一次编码加密代码.加密代码似乎工作正常,但解密会引发错误.我得到的错误是:de.flexiprovider.api.exceptions.BadPaddingException:无效的密文在解密函数中朝向代码的末尾,标记为注释//错误在这里!…………………………我已经包含了所有的进口产品,

我想要一些帮助,因为这是我第一次编码加密代码.

加密代码似乎工作正常,但解密会引发错误.

我得到的错误是:

de.flexiprovIDer.API.exceptions.BadpaddingException:无效的密文

在解密函数中朝向代码的末尾,标记为注释

//错误在这里! …………………………

我已经包含了所有的进口产品,请原谅这一点,因为它可能与此问题有关.

非常感谢非常感谢任何关于我做错的帮助.

码:

import java.io.UnsupportedEnCodingException;import java.security.KeyFactory;import java.security.KeyPair;import java.security.KeyPairGenerator;import java.security.PrivateKey;import java.security.PublicKey;import java.security.SecureRandom;import java.security.Security;import javax.crypto.BadpaddingException;import javax.crypto.Cipher;import javax.crypto.IllegalBlockSizeException;import androID.app.Activity;import androID.os.Bundle;import androID.util.Base64;import androID.util.Log;import de.flexiprovIDer.common.IEs.IESParameterSpec;import de.flexiprovIDer.core.FlexiCoreProvIDer;import de.flexiprovIDer.ec.FlexIECProvIDer;import de.flexiprovIDer.ec.parameters.CurveParams;import de.flexiprovIDer.ec.parameters.CurveRegistry.BrainpoolP384r1;import de.flexiprovIDer.pki.PKCS8EncodedKeySpec;import de.flexiprovIDer.pki.X509EncodedKeySpec;public class MainActivity extends Activity {private static PublicKey PublicKey;private static PrivateKey PrivateKey;private static String PubKey;private static String PrvKey;private static String message = "Hello World";private static String encryptedMessage;private static String decryptedMessage;private final static String TAG = "ERROR: ";@OverrIDeprotected voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentVIEw(R.layout.activity_main);    try {        Security.addProvIDer(new FlexiCoreProvIDer());        Security.addProvIDer(new FlexIECProvIDer());        // instantiate the elliptic curve key pair generator        KeyPairGenerator kpg = KeyPairGenerator.getInstance("ECIES", "FlexIEC");        // choose the curve        CurveParams ecParams = new BrainpoolP384r1();        // Initialize the key pair generator        kpg.initialize(ecParams, new SecureRandom());        KeyPair keyPair = kpg.generateKeyPair();        // generate the public key        PublicKey = keyPair.getPublic();        // generate private key        PrivateKey = keyPair.getPrivate();    }    catch (Exception e) {        Log.e(TAG, e.toString());    }    // I'm converting keys to strings here as the public keys will be stored on a server    // database and the private keys will be stored in the application preferences file    // this private key storage is maybe not optimum, but at this point I just want to    // simulate a messaging encryption/decryption process for testing purposes    // convert public key to a string    PubKey = Base64.encodetoString(PublicKey.getEncoded(), Base64.DEFAulT);    Log.d("PubKey: ", PubKey);    // convert private key to a string    PrvKey = Base64.encodetoString(PrivateKey.getEncoded(), Base64.DEFAulT);    Log.d("PrvKey: ", PrvKey);    // encrypt the message with the public key    encryptedMessage = encryptMessage(PubKey, message);    // report if the public key has not been regenerated correctly    if (encryptedMessage == null) {        Log.d("PUBliC_KEY_REGENERATE_ERROR: ", encryptedMessage);    }    // decrypt the message with the private key    decryptedMessage = decryptMessage(PrvKey, encryptedMessage);    // report if the private key has not been regenerated correctly    if (encryptedMessage == null) {        Log.d("PRIVATE_KEY_REGENERATE_ERROR: ", decryptedMessage);    }}// encrypt functionpublic static String encryptMessage(String publicKey, String message) {    KeyFactory keyFactory = null;    PublicKey pubkey = null;    Cipher cipher = null;    byte[] PLAINTEXT_MESSAGE = message.getBytes();    Log.d("PLAINTEXT_MESSAGE: ", message);    Security.addProvIDer(new FlexiCoreProvIDer());    Security.addProvIDer(new FlexIECProvIDer());    // Base64 decode the publicKey string into a byte array    byte[] decodedPublicKey = Base64.decode(publicKey, Base64.DEFAulT);    try {        // instantiate a X509EncodedKeySpec        X509EncodedKeySpec X509spec = new X509EncodedKeySpec(decodedPublicKey);        keyFactory = KeyFactory.getInstance("ECIES", "FlexIEC");        // re-generate the public key        pubkey = keyFactory.generatePublic(X509spec);        // sanity check, return null on inequality        if (!pubkey.equals(PublicKey)) {            return null;        }        cipher = Cipher.getInstance("ECIES", "FlexIEC");        IESParameterSpec IEsspec = new IESParameterSpec("AES256_CBC", "HmacSHA512", null, null);        cipher.init(Cipher.ENCRYPT_MODE, pubkey, IEsspec);    }    catch (Exception e) {        Log.e(TAG, e.toString());    }    // encrypt the message    byte[] encryptedData = null;    try {        encryptedData = cipher.doFinal(PLAINTEXT_MESSAGE);    }    catch (IllegalBlockSizeException e) {        Log.e(TAG, e.toString());    }    catch (BadpaddingException e) {        Log.e(TAG, e.toString());    }    String encryptedMessage = null;    try {        encryptedMessage = new String(encryptedData, "UTF-8");    }    catch (UnsupportedEnCodingException e) {        Log.e(TAG, e.toString());    }    Log.d("encryptedMessage: ", encryptedMessage);    return encryptedMessage;}// decrypt functionpublic static String decryptMessage(String privateKey, String message) {    KeyFactory keyFactory = null;    PrivateKey prvkey = null;    Cipher cipher = null;    byte[] ENCRYPTED_MESSAGE = message.getBytes();    Log.d("ENCRYPTED_MESSAGE: ", message);    Security.addProvIDer(new FlexiCoreProvIDer());    Security.addProvIDer(new FlexIECProvIDer());    try {        // Base64 decode the privateKey string into a byte array        byte[] decodedPrivateKey = Base64.decode(privateKey, Base64.DEFAulT);        // instantiate a PKCS8EncodedKeySpec        PKCS8EncodedKeySpec PKCS8spec = new PKCS8EncodedKeySpec(decodedPrivateKey);        keyFactory = KeyFactory.getInstance("ECIES", "FlexIEC");        // re-generate the private key        prvkey = keyFactory.generatePrivate(PKCS8spec);        // sanity check, return null on inequality        if (!prvkey.equals(PrivateKey)) {            return null;        }        cipher = Cipher.getInstance("ECIES", "FlexIEC");        IESParameterSpec IEsspec = new IESParameterSpec("AES256_CBC", "HmacSHA512", null, null);        cipher.init(Cipher.DECRYPT_MODE, prvkey, IEsspec);    }    catch (Exception e) {        Log.e(TAG, e.toString());    }    // decrypt the message    byte[] decryptedData = null;    try {        decryptedData = cipher.doFinal(ENCRYPTED_MESSAGE);        // ERROR THROWN HERE! ..............................        // de.flexiprovIDer.API.exceptions.BadpaddingException: invalID ciphertext    }    catch (IllegalBlockSizeException e) {        Log.e(TAG, e.toString());    }    catch (BadpaddingException e) {        Log.e(TAG, e.toString());    }    String decryptedMessage = null;    try {        decryptedMessage = new String(decryptedData, "UTF-8");    }    catch (UnsupportedEnCodingException e) {        Log.e(TAG, e.toString());    }    Log.d("decryptedMessage: ", decryptedMessage);    return decryptedMessage;}

}

解决方法:

您不能只使用密文作为String构造函数的输入,就像您在此行中所做的那样:

encryptedMessage = new String(encryptedData, "UTF-8");

如果要使用字符串而不是字节来传递密文,则必须像使用密钥一样使用Base 64等编码.

加密将导致数据看起来像随机字节.并非所有字节都具有等效字符.转换的结果取决于字符编码. UTF-8可能使用许多字节,并且许多组合不会产生正确的字符. Java默默地转换它们,检查Charset和相关类以获取更多信息.

总结

以上是内存溢出为你收集整理的java – BadPaddingException:无效的密文全部内容,希望文章能够帮你解决java – BadPaddingException:无效的密文所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1098118.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存