java中AES256加密的问题

java中AES256加密的问题,java,encryption,stream,aes,encryption-symmetric,Java,Encryption,Stream,Aes,Encryption Symmetric,所以。。这是一个类:(我使用AES256加密并制作) 公共类AES256 { 私有字符串charSet=“UTF-8”; 私有字符串algo=“AES/CBC/pkcs5ppadding”; 专用字符串baseAlgo=“AES”; 私有字符串hashAlgo=“PBKDF2WithHmacSHA1”; 私有字符串密钥=null; 私有字符串salt=“defaultsaltsalt”; 私有字符串iv=”a1bC@6jZ!#sL1z0y”; 专用密码; 私有缓冲数据流bIs; 私有缓冲输出流;

所以。。这是一个类:(我使用AES256加密并制作)

公共类AES256
{
私有字符串charSet=“UTF-8”;
私有字符串algo=“AES/CBC/pkcs5ppadding”;
专用字符串baseAlgo=“AES”;
私有字符串hashAlgo=“PBKDF2WithHmacSHA1”;
私有字符串密钥=null;
私有字符串salt=“defaultsaltsalt”;
私有字符串iv=”a1bC@6jZ!#sL1z0y”;
专用密码;
私有缓冲数据流bIs;
私有缓冲输出流;
公共服务
{
}
公共AES256(字符串传递)
{
this.key=pass;
}
公共AES256(串通、串咸)
{
this.key=pass;
盐=咸的;
}
公共AES256(串通、串咸、串ivs)
{
this.key=pass;
盐=咸的;
这1.iv=ivs;
}
公共无效设置键(字符串键)
{
this.key=key;
}
公共无效设置盐(字符串盐)
{
盐=盐;
}
公共无效集合IV(字符串IV)
{
这1.iv=ivs;
}
/**
*@Method填充并构造SecretKey(填充@32)
*@return返回加垫的键。
*@throws异常如果密钥为null或其他错误,则会引发异常。。
*/
public SecretKeySpec getKey()引发异常
{
字节[]saltBytes=salt.getBytes(字符集);
SecretKeyFactory=SecretKeyFactory.getInstance(hashAlgo);
PBEKeySpec spec=新PBEKeySpec(
this.key.toCharArray(),
saltBytes,
300000,//生成变量
263//默认值32字节
);
SecretKey SecretKey=factory.generateSecret(规范);
SecretKeySpec secret=新的SecretKeySpec(secretKey.getEncoded(),baseAlgo);
归还秘密;
}
/**
*@Method填充并返回IV(填充@16)
*@返回
*@抛出异常
*/
公共字节[]getIV()引发异常
{
byte[]byteKey=iv.getBytes(字符集);
MessageDigest sha=MessageDigest.getInstance(“sha-512”);
byteKey=sha.digest(byteKey);
byteKey=Arrays.copyOf(byteKey,16);
返回byteKey;
}
公共字节[]加密(字节[]明文)引发异常
{
cipher=cipher.getInstance(algo);
cipher.init(cipher.ENCRYPT_模式,getKey(),新的IvParameterSpec(getIV());
System.out.println(“纯文本长度:“+纯文本.length”);
字节[]enc=Base64.encodeBase64(cipher.doFinal(明文));
System.out.println(“加密文本长度”+enc.length);
返回enc;
}
公共字节[]解密(字节[]encryptedText)引发异常
{
cipher=cipher.getInstance(algo);
cipher.init(cipher.DECRYPT_模式,getKey(),新的IvParameterSpec(getIV());
System.out.println(“加密解密文本长度:“+encryptedText.length”);
字节[]de=cipher.doFinal(Base64.decodeBase64(encryptedText));
System.out.println(“解密文本长度:“+de.length”);
返回de;
}
public void encrypt(File fileToEncrypt)抛出FileNotFoundException、IOException、Exception
{
如果(fileToEncrypt==null)
抛出新的FileNotFoundException(“未找到指定用于加密的文件!”);
文件加密=新文件(剪切路径(fileToEncrypt.getPath()),“加密”+fileToEncrypt.getName());
如果(!encrypted.exists())
encrypted.createNewFile();
bIs=新的BufferedInputStream(新的FileInputStream(fileToEncrypt));
bOs=新的BufferedOutputStream(新的FileOutputStream(加密));
@抑制警告(“未使用”)
int read=0;
字节[]buff=新字节[1024];
而((读=bIs.read(buff))!=-1)
{
字节[]enc=加密(buff);
bOs.write(enc,0,enc.length);
}
二、关闭();
bOs.close();
}
public void decrypt(File fileToDecrypt)抛出FileNotFoundException、IOException、Exception
{
if(fileToDecrypt==null)
抛出新的FileNotFoundException(“未找到指定用于解密的文件!”);
File decrypted=新文件(剪切路径(fileToDecrypt.getPath()),“decrypted”+fileToDecrypt.getName().replace(“加密的”),“”);
如果(!decrypted.exists())
已解密的.createNewFile();
bIs=新的BufferedInputStream(新的FileInputStream(fileToDecrypt));
bOs=新的BufferedOutputStream(新的FileOutputStream(已解密));
@抑制警告(“未使用”)
int read=0;
字节[]buff=新字节[1388];
而((读=bIs.read(buff))!=-1)
{
字节[]de=解密(buff);
bOs.write(de,0,de.length);
}
二、关闭();
bOs.close();
}
专用字符串剪切路径(字符串路径)
{
字符串temp=“”;
String[]parts=path.split(Pattern.quote(File.separator));
对于(int i=0;i
}

我正在使用我编写的这个类来使用CBC/PKCS5PADDING对java中的信息进行加密和解密,我还使用哈希算法来哈希我的密码

注意:我知道这个程序中存在一些效率问题,比如为什么我要一直计算从文件中得到的密钥块。。我以后再修。。它只是在那里测试一些东西。

无论如何,我使用的是encrypt(File)方法,当我给它一个文件时,它一次获取1024字节的信息,并对该信息进行加密,然后转换到BASE64以避免编码问题。。然后将它写回另一个同名的文件,但前面的字已加密或解密。。。并且与父文件位于同一目录中

现在我的问题是,当它对信息进行加密时,我会将1024字节的信息发送给它进行处理,然后使用BASE64来避免诸如UTF之类的编码问题。。但最后,我加密的1024字节如何变成1388字节的数据,这就是我得到的。。。为什么呢

第二个问题:除了上面的问题之外,它还有些作用。也许这不是一个问题,但我很想知道原因。。无论如何,第二个问题也是使用加密(文件)方法以及解密(文件)当我加密文件时,它会以某种方式增加一些额外的长度(可能与上述问题直接相关…),因此
public class AES256
{
private String charSet = "UTF-8";
private String algo = "AES/CBC/PKCS5Padding";
private String baseAlgo = "AES";
private String hashAlgo = "PBKDF2WithHmacSHA1";

private String key = null;
private String salt = "defaultsaltsalt";
private String iv = "a1bC@6jZ!#sL1z0y";

private Cipher cipher;
private BufferedInputStream bIs;
private BufferedOutputStream bOs;

public AES256()
{

}

public AES256(String pass)
{
    this.key = pass;
}

public AES256(String pass, String salty)
{
    this.key = pass;
    this.salt = salty;
}

public AES256(String pass, String salty, String ivs)
{
    this.key = pass;
    this.salt = salty;
    this.iv = ivs;
}

public void setKey(String key)
{
    this.key = key;
}

public void setSalt(String salt)
{
    this.salt = salt;
}

public void setIV(String ivs)
{
    this.iv = ivs;
}

/**
 * @Method Pads and constructs the SecretKey (Padding @ 32)
 * @return Returns the padded key.
 * @throws Exception Exception is thrown if the key is null or something else wrong..
 */
public SecretKeySpec getKey() throws Exception
{
     byte[] saltBytes = salt.getBytes(charSet);

     SecretKeyFactory factory = SecretKeyFactory.getInstance(hashAlgo);
     PBEKeySpec spec = new PBEKeySpec(
             this.key.toCharArray(), 
             saltBytes, 
             300000, //make variable
             263 //default 32 bytes
     );

     SecretKey secretKey = factory.generateSecret(spec);
     SecretKeySpec secret = new SecretKeySpec(secretKey.getEncoded(), baseAlgo);

     return secret;
}

/**
 * @Method Pads and returns the IV (Padding @ 16)
 * @return
 * @throws Exception
 */
public byte[] getIV() throws Exception
{
    byte[] byteKey = iv.getBytes(charSet);
    MessageDigest sha = MessageDigest.getInstance("SHA-512");
    byteKey = sha.digest(byteKey);
    byteKey = Arrays.copyOf(byteKey, 16);
    return byteKey;
}

public byte[] encrypt(byte[] plainText) throws Exception
{
    cipher = Cipher.getInstance(algo);
    cipher.init(Cipher.ENCRYPT_MODE, getKey(), new IvParameterSpec(getIV()));

    System.out.println("Plain text length: "+plainText.length);
    byte[] enc = Base64.encodeBase64(cipher.doFinal(plainText));
    System.out.println("Encrypted text length "+enc.length);

    return  enc;
}

public byte[] decrypt(byte[] encryptedText) throws Exception
{
    cipher = Cipher.getInstance(algo);
    cipher.init(Cipher.DECRYPT_MODE, getKey(), new IvParameterSpec(getIV()));

    System.out.println("Encrypted Decrypted Text length: "+encryptedText.length);
    byte[] de = cipher.doFinal(Base64.decodeBase64(encryptedText));
    System.out.println("Decrypted Text length: "+de.length);

    return de;
}

public void encrypt(File fileToEncrypt) throws FileNotFoundException, IOException, Exception
{
    if(fileToEncrypt == null)
        throw new FileNotFoundException("File given to encrypt was not found!");
    File encrypted = new File(cutPath(fileToEncrypt.getPath()), "ENCRYPTED "+fileToEncrypt.getName());
    if(!encrypted.exists())
        encrypted.createNewFile();
    bIs = new BufferedInputStream(new FileInputStream(fileToEncrypt));
    bOs = new BufferedOutputStream(new FileOutputStream(encrypted));

    @SuppressWarnings("unused")
    int read = 0;
    byte[] buff = new byte[1024];
    while((read = bIs.read(buff)) != -1)
    {
        byte[] enc = encrypt(buff);
        bOs.write(enc, 0, enc.length);
    }
    bIs.close();
    bOs.close();
}

public void decrypt(File fileToDecrypt) throws FileNotFoundException, IOException, Exception
{
    if(fileToDecrypt == null)
        throw new FileNotFoundException("File given to decrypt was not found!");
    File decrypted = new File(cutPath(fileToDecrypt.getPath()), "DECRYPTED "+fileToDecrypt.getName().replace("ENCRYPTED ", ""));
    if(!decrypted.exists())
        decrypted.createNewFile();
    bIs = new BufferedInputStream(new FileInputStream(fileToDecrypt));
    bOs = new BufferedOutputStream(new FileOutputStream(decrypted));

    @SuppressWarnings("unused")
    int read = 0;
    byte[] buff = new byte[1388];
    while((read = bIs.read(buff)) != -1)
    {
        byte[] de = decrypt(buff);
        bOs.write(de, 0, de.length);
    }
    bIs.close();
    bOs.close();
}

private String cutPath(String path)
{
    String temp = "";
    String[] parts = path.split(Pattern.quote(File.separator));
    for(int i = 0; i < parts.length-1; i++)
        temp+=parts[i]+"/";
    return temp;
}
1024 bytes + 16 padding = 1040 bytes
1040 bytes is not divisible by 3 (as required by base-64) so add 1 byte
1041 bytes * 8 = 8328 bits / 6 = 1388
1388 base-64 characters
byte[] buff = new byte[1024];
while((read = bIs.read(buff)) != -1)
{
    byte[] enc = encrypt(buff);
    bOs.write(enc, 0, enc.length);
}
    byte[] enc = encrypt(buff, read);
public byte[] encrypt(byte[] plainText, int len) throws Exception
byte[] enc = Base64.encodeBase64(cipher.doFinal(plainText, 0, len));