加解密文件?
哈哈哈哈,当然是为了安全,自己的东西不像让别人看见。
1,学了JavaIO流的字节流的读取写入,便可实现。
加密原理: 把文件读取,然后,按某个特定的规则改变其字节写入一个新文件。
解密原理:读取新文件,把文件按照写入的格式还原。
这样是说不清的,上代码。
以加解密图片为例子。
package funJoy;import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;public class AddPwd {public static void main(String[] args) {String path = "E:\\s2\\myEclipseWork\\CopyImage\\";// 加密//addPwd(path + "1.jpg", path + "2.jpg");// 解密solutionPwd(path + "2.jpg", path + "deCode.jpg");}public static void addPwd(String read, String write) {// 加解密操作 改变文件字节码组成FileInputStream fis = null;FileOutputStream fos = null;try {fis = new FileInputStream(read);fos = new FileOutputStream(write);int len;while ((len = fis.read()) != -1) {// 这里可以对len做出改变 +-*/都行,这里+,后面减fos.write(len + 9);}} catch (FileNotFoundException e) {System.out.println(e);} catch (IOException e) {System.out.println(e);} finally {try {fos.close();fis.close();} catch (IOException e) {System.out.println(e);}}}public static void solutionPwd (String read, String write) {// 解密操作 还原到原来的字节码FileInputStream fis = null;FileOutputStream fos = null;try {fis = new FileInputStream(read);fos = new FileOutputStream(write);int len;while ((len = fis.read()) != -1) {fos.write(len - 9);}} catch (FileNotFoundException e) {System.out.println(e);} catch (IOException e) {System.out.println(e);} finally {try {fos.close();fis.close();} catch(IOException e) {System.out.println(e);}}}}
2,Cipher类 和 base64encode对字符串加密
package pwd.joy;//import java.io.*;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;import Decoder.BASE64Decoder;
import Decoder.BASE64Encoder;/*** @author echo lovely* @createDay 2020/1/1* * */public class TestCipher {public static void main(String[] args) {String pwd = setCipher("HELLO WORLD, 2020, EVERYTHING IS WELL!");System.out.println(pwd);String str = decrypt(pwd);System.out.println(str); }/*** 返回一个密文。* */public static String setCipher(String content) {String encoded = "";try {// 密钥Key secretKey = getKey("abc");// AES 高级加密标准 ADVANCED ENCRYPTION STANDARDCipher ch = Cipher.getInstance("AES");ch.init(Cipher.ENCRYPT_MODE, secretKey);BASE64Encoder encoder = new BASE64Encoder();byte[] b = ch.doFinal(content.getBytes("UTF-8"));encoded = encoder.encode(b); } catch (IllegalBlockSizeException | BadPaddingException e) {e.printStackTrace();} catch (NoSuchAlgorithmException e) {e.printStackTrace();} catch (NoSuchPaddingException e) {e.printStackTrace();} catch (InvalidKeyException e) {e.printStackTrace();} catch (UnsupportedEncodingException e) {e.printStackTrace();}return encoded;}public static Key getKey(String keySeed) { if (keySeed == null) { // 获取指定的环境变量值。keySeed = System.getenv("AES_SYS_KEY"); } if (keySeed == null) { // 获取指定键指示的系统属性。keySeed = System.getProperty("AES_SYS_KEY"); } if (keySeed == null || keySeed.trim().length() == 0) { keySeed = "echoLovely!@#$";// 默认种子 } try { // 这是个强加密随机器 返回实现指定随机数生成器 (RNG) 算法的 SecureRandom 对象。SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG"); // 重新设置此随机对象的种子。secureRandom.setSeed(keySeed.getBytes()); // 返回AES对称密钥生成器 KeyGenerator generator = KeyGenerator.getInstance("AES"); // 初始化密钥生成器generator.init(secureRandom); // 返回密钥return generator.generateKey(); } catch (Exception e) { throw new RuntimeException(e); } }/*** 根据密钥对指定的密文cipherText进行解密.** @param pwd 密文* @return 解密后的字符串.*/public static String decrypt(String pwd) {Key secretKey = getKey("abc");byte[] result = null;try {// 密钥AES算法Cipher cipher = Cipher.getInstance("AES");cipher.init(Cipher.DECRYPT_MODE, secretKey);BASE64Decoder decoder = new BASE64Decoder();byte[] c = decoder.decodeBuffer(pwd);result = cipher.doFinal(c); String re = new String (result, "UTF-8");return re;} catch (Exception e) {throw new RuntimeException(e);} }}
密钥是 abc,可控的。
Zo+zIB3CEIFwshPItHKL7sKqjhub9hEZO+pyy3alhUnw2O8Z8mY/lBxY1rL5oar5
HELLO WORLD, 2020, EVERYTHING IS WELL!
emmm,其实我想对视频加密的如果用第一种方法,太太慢了,也是不可行的。
第二种aes算法,我我从昨天晚上搞到今天下午都只报 字节要是16的倍数。我怀疑这个不能对视频加密。字符串,字节数组相互转换,都可能错了。