我试图通过网络加密通过AES算法和base64发送受保护的文本。对于我尝试过的几个文本,它可以正常工作,但是有一些字符串未正确解码。我在字符串上找到了无法解码的图案。他们似乎对他们有一个破裂。例子:加密步骤:
1(明文
2(AES加密文本:
l1 µ?|6îô.jcæ÷ï_ðnso〜a’q@ó [
3(base64 aes文本:
zwunbdegnz jfdbo1c5qy b3z1/wtlnpmadbknfa01s =
解密步骤:
1(base64 AES文本:
zwunbdegnz jfdbo1c5qy b3z1/wtlnpmadbknfa01s =
2(AES加密文本:
l1 µ?|6îô.jcæ÷ï_ðnso〜a’q@ó [
3(纯文本:
fi
我加密的任何其他文本不包含Breakline的加密版本都被正确解密了。
ex:
加密步骤:
1(纯文本:P@$$ W0RD
2(AES加密文本:
÷÷odã29ôðñìe£™Ø
3(base64 aes文本:
9xH3T2TJMJN00NHMZAOZ a ==
解密步骤:
3(base64 aes文本:
9xH3T2TJMJN00NHMZAOZ a ==
2(AES加密文本:
÷÷odã29ôðñìe£™Ø
3(纯文本:
p@$$ W0RD
以下是我为此目的创建的对象。
base64codec对象:
package security;
import java.util.Base64;
import java.util.Base64.Decoder;
import java.util.Base64.Encoder;
public class Base64Codec
{
private static Base64Codec objInstance;
private Encoder objEncoder;
private Decoder objDecoder;
private Base64Codec()
{
this.setEncoder();
this.setDecoder();
}
public static Base64Codec getInstance()
{
if (objInstance == null)
{
objInstance = new Base64Codec();
}
return objInstance;
}
private void setEncoder()
{
this.objEncoder = Base64.getEncoder();
}
private Encoder getEncoder()
{
return this.objEncoder;
}
private void setDecoder()
{
this.objDecoder = Base64.getDecoder();
}
private Decoder getDecoder()
{
return this.objDecoder;
}
protected String encode(String strValue)
{
return this.getEncoder().encodeToString(strValue.getBytes());
}
protected String decode(String strValue)
{
return new String(this.getDecoder().decode(strValue));
}
}
vathsentencryptionstandard对象
package security;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
public class AdvancedEncryptionStandard
{
private static final String strKey = "02E68E02BE7400FE11E8A45B60017F98";
private static final byte[] bytKey = strKey.getBytes();
private static final String strAlgorithm = "AES";
private static AdvancedEncryptionStandard objInstance;
private Cipher objCipher;
private SecretKeySpec objSecretKey;
private AdvancedEncryptionStandard() throws NoSuchAlgorithmException, NoSuchPaddingException
{
this.setSecretKey();
this.setCipher();
}
public static AdvancedEncryptionStandard getInstance() throws NoSuchAlgorithmException, NoSuchPaddingException
{
if (objInstance == null)
{
objInstance = new AdvancedEncryptionStandard();
}
return objInstance;
}
private byte[] getKey()
{
return bytKey;
}
private String getAlgorithm()
{
return strAlgorithm;
}
private void setSecretKey()
{
this.objSecretKey = new SecretKeySpec(this.getKey(), this.getAlgorithm());
}
private SecretKeySpec getSecretKey()
{
return this.objSecretKey;
}
private void setCipher() throws NoSuchAlgorithmException, NoSuchPaddingException
{
this.objCipher = Cipher.getInstance(this.getAlgorithm());
}
private Cipher getCipher()
{
return this.objCipher;
}
protected String encrypt(String strValue) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException
{
this.getCipher().init(Cipher.ENCRYPT_MODE, this.getSecretKey());
return new String(this.getCipher().doFinal(strValue.getBytes()));
}
protected String decrypt(String strValue) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException
{
this.getCipher().init(Cipher.DECRYPT_MODE, this.getSecretKey());
return new String(this.getCipher().doFinal(strValue.getBytes()));
}
}
Securetext对象
package security;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
public class SecureText
{
private static SecureText objInstance;
private SecureText()
{
}
public static SecureText getInstance()
{
if (objInstance == null)
{
objInstance = new SecureText();
}
return objInstance;
}
public String encode(String strValue) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException
{
return Base64Codec.getInstance().encode(AdvancedEncryptionStandard.getInstance().encrypt(strValue));
}
public String decode(String strValue) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException
{
return AdvancedEncryptionStandard.getInstance().decrypt(Base64Codec.getInstance().decode(strValue));
}
}
测试:
public static void main(String[] args)
{
//Test SecureText Object. AES and Base64
String strSecureText = <plain text>
System.out.println(strSecureText);
System.out.println(SecureText.getInstance().decode(strSecureText));
//Test AES and Base64 separated
String strCoded;
String strEncrypted;
String strDecrypted;
String strDecoded;
strEncrypted = AdvancedEncryptionStandard.getInstance().encrypt(<plain text>);
System.out.println(strEncrypted);
strCoded = Base64Codec.getInstance().encode(strEncrypted);
System.out.println(strCoded);
strDecoded = Base64Codec.getInstance().decode(strCoded);
System.out.println(strDecoded);
strDecrypted = AdvancedEncryptionStandard.getInstance().decrypt(strDecoded);
System.out.println(strDecrypted);
String strSecureText = SecureText.getInstance().encode(<plain text>);
}
我看到了您的代码两个问题(有关加密,对于您已经有足够评论的结构(
(对于我尝试过的几个文本,它可以正常工作,但是有一些字符串未正确解码。我在字符串上找到了无法解码的图案。
切勿尝试以String
表示Java中的随机字节数组。JVM将尝试暗示字符集转换,您可能会丢失/更改数据。当您将String
转换为字节时,也适用同样的习惯,最好明确说明编码。
加密数据只是一个字节数组,一旦您打印它们后,将字节数组编码为可读的内容,例如Hex或base64。请勿使用new String(encryptedArray)
下一期是我看不到您使用iv(似乎要求进行身份验证的加密太多(,请阅读我的博客有关如何至少正确地进行加密的博客。