2014-02-18 137 views
3

我使用AES加密字符串,但加密字符串最後包含\n\r從AES加密字符串中刪除 r和 n

public class AESImpl { 

    private static String decryptedString; 

    private static String encryptedString; 

    public static void main(String[] args) throws NoSuchAlgorithmException, IOException, ClassNotFoundException { 

    String strToEncrypt = "This text has to be encrypted"; 
    SecretKey secretKey = generateSecretKey(); 
    String encryptStr = encrypt(strToEncrypt, secretKey); 
    System.out.println("Encrypted String : " + encryptStr + "It should not come in new line"); 
    String decryptStr = decrypt(encryptStr, secretKey); 
    System.out.println("Decrypted String : " + decryptStr); 
    } 

    private static SecretKey generateSecretKey() throws NoSuchAlgorithmException, IOException { 
    KeyGenerator kg = KeyGenerator.getInstance("AES"); 
    kg.init(128); 
    SecretKey sk = kg.generateKey(); 
    String secretKey = String.valueOf(Hex.encodeHex(sk.getEncoded())); 
    System.out.println("Secret key is " + secretKey); 
    return sk; 
    } 

    public static String encrypt(String strToEncrypt, SecretKey secretKey) { 
    try { 
     Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING"); 
     cipher.init(Cipher.ENCRYPT_MODE, secretKey); 
     encryptedString = new String(Base64.encodeBase64String(cipher.doFinal(strToEncrypt.getBytes()))); 
    } catch (Exception e) { 
     System.out.println("Error while encrypting: " + e.toString()); 
    } 

    return encryptedString; 
    } 

    public static String decrypt(String strToDecrypt, SecretKey secretKey) { 
    try { 
     Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING"); 
     cipher.init(Cipher.DECRYPT_MODE, secretKey); 
     decryptedString = new String(cipher.doFinal(Base64.decodeBase64(strToDecrypt))); 
    } catch (Exception e) { 
     System.out.println("Error while decrypting: " + e.toString()); 
    } 

    return decryptedString; 
    } 
} 

輸出

Secret key is 2df36561b09370637d35b4a310617e60 
Encrypted String : TUDUORnWtsZFJAhBw1fYMF9CFExb/tSsLeDx++cpupI= 
It should not come in new line 
Decrypted String : This text has to be encrypted 

實際上,加密的字符串是TUDUORnWtsZFJAhBw1fYMF9CFExb/tSsLeDx++cpupI=/r/n。 我是否需要從加密字符串中明確地替換\r\n或者我在上面的代碼中做了一些錯誤?

+0

Base64字符串中的空白被忽略。沒有任何錯誤。雖然可能有一個Base64編碼器選項來防止添加字符。 –

回答

0

看起來base64編碼標準要求至少每75個字符有一個換行符。我的猜測是base64編碼函數會自動添加這個函數,你沒有做錯任何事情,並且可以放入或移除它。根據以下鏈接,base64解碼功能應該忽略換行符,所以不管是否刪除它都取決於您...

請參閱此處查看遇到此問題的其他人,以及base64標準的引用:http://lists.w3.org/Archives/Public/w3c-ietf-xmldsig/2001AprJun/0183.html

+2

***但我會強調,在加密字符串中更改值必然會在某個點或另一個點造成麻煩。 – christopher

+0

同意!亂搞加密的字符串可能不是一個好主意! – user1578653

+0

除非另一端的Base64解碼器堅持存在(這不太可能),否則更改空白不應該導致問題。 –

6

添加 Base64.encodeBase64String(hashPassword,Base64.NO_WRAP)刪除\ n。

默認情況下,它使用Base64.DEFAULT,它添加換行符。

請點擊這裏:source

請點擊這裏:Main source

-1

的編碼字符串簡單地執行encryptedString = encryptedString.replaceAll("(?:\\r\\n|\\n\\r|\\n|\\r)", "")

當你嘗試將它解碼回字節時,它工作正常。我用隨機生成的字節數組測試過幾次。顯然,解碼過程忽略了新行,無論它們是否存在。我使用com.sun.org.apache.xml.internal.security.utils.Base64測試了這個「確認工作」。其他編碼器未經測試。