好吧,这就是我面临的情况。我正在构建一个带登录屏幕的android应用程序,并将信息加密发送到我的服务器。加密后,我在Android上使用Base64对信息进行编码,将其发送到正在Base64中解码的PC上的服务器,但它没有正确执行。我的服务器报告了一个加密填充错误。多平台的Base64编码和解码问题(Android到PC)
这是Android上的加密代码:
import android.util.Base64;
try {
plainText = user.getBytes("UTF-8");
user = EncryptInfo(plainText, publicKey);
} catch (UnsupportedEncodingException ex) {
ex.getStackTrace();
}
private static String EncryptInfo(byte[] data,PublicKey key){
Cipher cipher;
byte[] cipherText = null;
try {
cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.ENCRYPT_MODE, key);
cipherText = cipher.doFinal(data);
} catch (NoSuchAlgorithmException ex) {
ex.getStackTrace();
System.exit(1);
} catch (NoSuchPaddingException ex) {
ex.getStackTrace();
System.exit(1);
} catch (InvalidKeyException ex) {
ex.getStackTrace();
System.exit(1);
} catch (IllegalBlockSizeException ex) {
ex.getStackTrace();
System.exit(1);
} catch (BadPaddingException ex) {
ex.getStackTrace();
System.exit(1);
}
return Base64.encodeToString(cipherText, Base64.DEFAULT);
}
这是在服务器上的个人电脑上运行的数据进行解码的代码:
import java.util.Base64;
//Decrypt the userpassword
byte[] plainText = Base64.getDecoder().decode(encryptedData);
inputLine = decryptData(plainText);
private String decryptData(byte[] cipherText) throws UnsupportedEncodingException{
// decrypt the ciphertext using the private key
Cipher cipher;
byte[] newPlainText = null;
try {
cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
newPlainText = cipher.doFinal(cipherText);
//System.out.println("Finish decryption: ");
//System.out.println(new String(newPlainText, "UTF8"));
} catch (NoSuchAlgorithmException ex) {
Logger.getLogger(ServerThread.class.getName()).log(Level.SEVERE, null, ex);
} catch (NoSuchPaddingException ex) {
Logger.getLogger(ServerThread.class.getName()).log(Level.SEVERE, null, ex);
} catch (InvalidKeyException ex) {
Logger.getLogger(ServerThread.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalBlockSizeException ex) {
Logger.getLogger(ServerThread.class.getName()).log(Level.SEVERE, null, ex);
} catch (BadPaddingException ex) {
Logger.getLogger(ServerThread.class.getName()).log(Level.SEVERE, null, ex);
}
return new String(newPlainText, "UTF8");
}
我已经包括了我进口正在使用base64。这也不是整个代码,而只是相关的位。我不完全了解base64,我所有的搜索都没有引导我回答,所以任何帮助都将非常感谢!
编辑:我这样做的原因是安全地将登录凭据传输到服务器。这适用于我的桌面应用程序。我从桌面应用程序复制了代码,但编码是唯一需要更改的内容。我认为64位是问题的一部分。无法调试应用程序,因为我运行amd,所以我必须在手机上正确运行它。
但是,你在使用base64呢? 7位时代已经结束。 –
是什么让你认为base64是问题?如果是这样,它应该是直截了当的,以确定两端的一些调试,而不是加密复杂化。 –
我试图轻松地将数据移动到套接字上并进行可能的存储。我再次对base64了解不多,但它在我的桌面应用程序中工作。我基于我的android应用程序关闭相同的代码。如果我能摆脱它,并使用其他好的东西。原来android包含一个老版本的commons库,所以当我尝试使用更新的版本时,它只是说它找不到encodeAsString方法。 –