日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

JavaIO流加解密,AES对字符串加解密

發布時間:2023/12/10 java 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JavaIO流加解密,AES对字符串加解密 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

加解密文件?
哈哈哈哈,當然是為了安全,自己的東西不像讓別人看見。

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的倍數。我懷疑這個不能對視頻加密。字符串,字節數組相互轉換,都可能錯了。

總結

以上是生活随笔為你收集整理的JavaIO流加解密,AES对字符串加解密的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。