所以..这是课程:(我使用并制作了AES256加密)
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;
}
}
我正在使用我编写的这个类来使用 CBC/PKCS5PADDING 在 java 中加密和解密信息,并且我还使用哈希算法来对我的密码进行哈希处理。
注意:我知道这个程序存在一些效率问题,比如为什么我要一直计算从文件中获得的密钥块。我稍后会修复它。它只是为了测试一些东西。
无论如何,我正在使用 encrypt(File) 方法,当我给它一个文件时,它一次获取 1024 个字节的信息并加密该信息,然后转换为 BASE64 以避免编码问题..然后将其写回另一个文件名称相同,但前面有 ENCRYPTED 或 DECRYPTED 字样……并且与父文件位于同一目录中。
现在我的问题是,当它加密信息时,我向它发送 1024 字节的信息进行处理,然后使用 BASE64 来避免编码问题,例如 UTF 等。但最终我正在加密的 1024 字节是如何加密的变成 1388 字节的数据,这就是我得到的……现在为什么会这样?
第二个问题:除了上面的问题之外,它有些工作可能不是问题,但我很想知道为什么..无论如何,第二个问题也是使用加密(文件)方法以及解密(文件)..当我加密文件它以某种方式增加了一些额外的长度(可能与上面的问题直接相关......),所以当我解密文件时,文件的文本完全解密,但是我在底部得到一些额外的重复文本原因......就像文件将按顺序排列,直到最后有一些重复信息......所以我不知道这些随机字节来自哪里,但我很想知道......
无论如何,如果您发现我的加密方法有任何其他问题,请在这里告诉我,可能是弱加密选择?也许它很容易被暴力破解?使用低效的方法?不明白我想要完成的事情?可能喜欢用相同的名称和相同的目录保存文件。有没有更简单的方法?