我使用AES加密字符串,但加密字符串最后包含\n
和\r
。
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 :(得分:11)
添加
Base64.encodeBase64String(hashPassword,Base64.NO_WRAP)
删除\ n。
默认使用Base64.DEFAULT
添加换行符。
点击此处:source
点击此处:Main source
答案 1 :(得分:4)
实际上,我使用apache commons-codec-1.4.0.jar 来编码字符串。将其更改为更高版本可解决此问题。 encodeBase64String方法的行为已从多行分块(commons-codec-1.4)更改为单行非分块(commons-codec-1.5)。
请点击链接了解更多详情。 http://commons.apache.org/proper/commons-codec/apidocs/org/apache/commons/codec/binary/Base64.html
答案 2 :(得分:1)
似乎base64编码标准要求至少每75个字符有一个换行符。我的猜测是base64编码功能是自动添加它,你没有做错什么,并且可以将其保留或删除它。根据下面的链接,base64解码函数应该忽略换行符,所以你是否删除它取决于你...
请点击此处查看遇到此问题的其他人以及base64标准的引用:http://lists.w3.org/Archives/Public/w3c-ietf-xmldsig/2001AprJun/0183.html
答案 3 :(得分:0)
只需对编码的字符串执行encryptedString = encryptedString.replaceAll("(?:\\r\\n|\\n\\r|\\n|\\r)", "")
。
当您尝试将其解码回字节时,它可以正常工作。我用随机生成的字节数组测试了几次。显然,解码过程只是忽略它们存在或不存在的换行符。我使用com.sun.org.apache.xml.internal.security.utils.Base64测试了这个“确认工作”。未经测试的其他编码器。
答案 4 :(得分:0)
这是在编码后的字符串末尾添加\n的代码块
keyBytes = secret_key.substring(0, 32).toByteArray(charset("UTF8"))
val skey = SecretKeySpec(keyBytes, "AES")
val input = strToEncrypt.toByteArray(charset("UTF8"))
synchronized(Cipher::class.java) {
val cipher = Cipher.getInstance("AES/ECB/PKCS5Padding")
cipher.init(Cipher.ENCRYPT_MODE, skey)
val cipherText = ByteArray(cipher.getOutputSize(input.size))
var ctLength = cipher.update(
input, 0, input.size,
cipherText, 0
)
ctLength += cipher.doFinal(cipherText, ctLength)
return String(
android.util.Base64.encode(cipherText, 1)
)
}
这是下面的代码工作正常!!
val algorithm = "AES"
val keyValue = secret_key.substring(0, 32).toByteArray(charset("UTF8"))
val key: Key = SecretKeySpec(keyValue, algorithm)
val c: Cipher = Cipher.getInstance(algorithm, "BC")
c.init(Cipher.ENCRYPT_MODE, key);
val encValue: ByteArray =
c.doFinal(
strToEncrypt.toByteArray()
)
return Base64.getEncoder().encodeToString(encValue)