암호화 코드를 코딩하는 데 처음으로 도움이되기를 바랍니다.BadPaddingException : 유효하지 않은 암호문
암호화 코드가 올바르게 작동하는 것으로 나타나지만 암호 해독시 오류가 발생합니다.
내가 오류는 다음과 같습니다
de.flexiprovider.api.exceptions.BadPaddingException : 유효하지 않은 암호문 A와 표시되는 코드의 끝을 향해 해독 기능에서
덧글
// 오류가 발생했습니다! ..............................
모든 수입품을 포함 시켰습니다. 생각해 보면 어떨까요? 문제에.
내가 뭘 잘못하고 있는지에 대한 도움은 크게 감사 드리며 대단히 감사합니다.
코드 :이 라인에서 일을 같이
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: ";
@Override
protected 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 function
public 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 function
public 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;
}
}
최소한 [Java 코드 규칙] (http://www.oracle.com/technetwork/java/codeconv-138413.html)을 계속 사용하면 큰 도움이됩니다. 이클립스와 같은 좋은 IDE는이 작업에 매우 유용하며 {clean code} 및 CheckStyle을 사용하여 완벽하게 완성 할 수 있습니다. –
저는 Java에 익숙하지 않습니다. 저는 C/C++ 프로그래머입니다. 조언을 주셔서 감사합니다. 귀하의 링크를 확인하겠습니다. – Mike