2016-04-25 95 views
0

好吧,這就是我面臨的情況。我正在構建一個帶登錄屏幕的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,所以我必須在手機上正確運行它。

+0

但是,你在使用base64呢? 7位時代已經結束。 –

+0

是什麼讓你認爲base64是問題?如果是這樣,它應該是直截了當的,以確定兩端的一些調試,而不是加密複雜化。 –

+0

我試圖輕鬆地將數據移動到套接字上並進行可能的存儲。我再次對base64瞭解不多,但它在我的桌面應用程序中工作。我基於我的android應用程序關閉相同的代碼。如果我能擺脫它,並使用其他好的東西。原來android包含一個老版本的commons庫,所以當我嘗試使用更新的版本時,它只是說它找不到encodeAsString方法。 –

回答

1

答:

return Base64.encodeToString(cipherText, Base64.NO_WRAP); 

調試我發現我的加密數據是在中途被切斷。使用Base64.No_Wrap解決了我的問題。只需發佈它,所有人都會知道。 Android開發對我來說是新的,所以我想我甚至沒有想到它。

+0

它看起來不像base64的問題,也與Android編程無關。問題似乎是您希望將您的數據放在一行中(您的發佈代碼未顯示)。通常,bass64數據的行長也有限,因此您的編碼數據由多行組成。另請參閱[關於base64的維基百科文章](https://en.wikipedia.org/wiki/Base64)。 –