电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術文章
文章詳情頁

JAVA 實現磁盤文件加解密操作的示例代碼

瀏覽:2日期:2022-08-25 09:32:12

簡單實現了下:

import javax.crypto.BadPaddingException;import javax.crypto.Cipher;import javax.crypto.KeyGenerator;import javax.crypto.SecretKey;import java.io.*;import java.security.GeneralSecurityException;import java.security.SecureRandom;/** * 文件/目錄加解密相關 * @author: Nemo * @date: 2019/3/19. */public class FileCrote { /** * 加密后的文件后綴 */ private static final String SUBFIX = '.enc'; /** * 執行路徑 */ private static String path = ''; /** * 執行模式 1加密 2解密 */ private static String mode = '1'; /** * 執行密碼 */ private static String pass = ''; private static String currentFilePath = null; /** * 根據路徑加密文件 * 1、如果是目錄,則找它下面的文件進行加密 * 2、如果是文件,則直接加密 * @param path * @throws IOException */ private static void encrypt(String path) throws IOException, GeneralSecurityException { System.out.println('開始處理'+path); File file = new File(path); if(!file.exists()){ System.out.println('需加密的路徑不存在:'+path); return; } if(file.isDirectory()){ //目錄則遍歷其下的文件 File[] files = file.listFiles(); if(files == null){return; } for (File subFile : files) {encrypt(subFile.getAbsolutePath()); } }else{ //文件則直接加密 encrypt(file); } } /** * 文件加密 * @param file * @throws IOException */ private static void encrypt(File file) throws IOException, GeneralSecurityException { String path = file.getAbsolutePath(); if(path.endsWith(SUBFIX)){ System.out.println('已加密文件不需再次加密'+path); return; } String encFilePath = path + SUBFIX; File encFile = new File(encFilePath); System.out.println('開始加密文件'+path); encryptFile(file,encFile,Cipher.ENCRYPT_MODE); } /** * 加解密文件操作 * @param srcFile * @param encFile * @throws IOException */ private static void encryptFile(File srcFile, File encFile,int mode) throws IOException, GeneralSecurityException { if(!srcFile.exists()){ System.out.println('source file not exixt'); return; } currentFilePath = srcFile.getAbsolutePath(); //密碼 Cipher cipher = getCipher(mode); FileInputStream fis = null; FileOutputStream fos = null; try { fis = new FileInputStream(srcFile); fos = new FileOutputStream(encFile); //真正處理 crypt(fis, fos, cipher); } catch (IOException e) { e.printStackTrace(); } finally { if (fis != null) {fis.close(); } if (fos != null) {fos.close(); } } //源文件刪除 srcFile.delete(); } /** * 得到加解密Cipher * @param mode * @return * @throws GeneralSecurityException */ private static Cipher getCipher(int mode) throws GeneralSecurityException { String type = 'AES'; Cipher cipher = Cipher.getInstance(type+'/ECB/PKCS5Padding'); KeyGenerator kgen = KeyGenerator.getInstance(type); kgen.init(128, new SecureRandom(pass.getBytes())); SecretKey key = kgen.generateKey(); cipher.init(mode,key); return cipher; } /** * 加密解密流 * @param in 加密解密前的流 * @param out 加密解密后的流 * @param cipher 加密解密 * @throws IOException * @throws GeneralSecurityException */ private static void crypt(InputStream in, OutputStream out, Cipher cipher) throws IOException, GeneralSecurityException { int blockSize = cipher.getBlockSize() * 1000; int outputSize = cipher.getOutputSize(blockSize); byte[] inBytes = new byte[blockSize]; byte[] outBytes = new byte[outputSize]; int inLength = 0; boolean more = true; while (more) { inLength = in.read(inBytes); if (inLength == blockSize) {int outLength = cipher.update(inBytes, 0, blockSize, outBytes);out.write(outBytes, 0, outLength); } else {more = false; } } if (inLength > 0) { outBytes = cipher.doFinal(inBytes, 0, inLength); } else { outBytes = cipher.doFinal(); } out.write(outBytes); } /** * 根據路徑解密 * 1、如果是目錄,則找它下面的加密文件進行解密 * 2、如果是文件,并且它是加密文件,則直接解密 * @param path * @throws IOException */ private static void decrypt(String path) throws IOException, GeneralSecurityException { System.out.println('開始處理'+path); File file = new File(path); if(!file.exists()){ System.out.println('需解密的路徑不存在:'+path); return; } if(file.isDirectory()){ //目錄則遍歷其下的文件 File[] files = file.listFiles(); if(files == null){return; } for (File subFile : files) {decrypt(subFile.getAbsolutePath()); } }else{ decrypt(file); } } /** * 文件解密 * @param file * @throws IOException */ private static void decrypt(File file) throws IOException, GeneralSecurityException { String path = file.getAbsolutePath(); if(!path.endsWith(SUBFIX)){ System.out.println('非加密文件不需解密'+path); return; } System.out.println('開始解密文件'+path); String newPath = path.substring(0,path.length() - SUBFIX.length()); encryptFile(file,new File(newPath),Cipher.DECRYPT_MODE); } /** * 字符串是否非空 * @param s * @return */ private static boolean isNotEmpty(String s){ if (s == null || ''.equals(s)) { return false; } return true; } /** * 開始處理 * @throws IOException * @throws GeneralSecurityException */ private static void deal() throws IOException, GeneralSecurityException { while (true) { print(); BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String s = reader.readLine(); if('path'.equals(s)) {System.out.println('rn請輸入執行路徑rn');while (true) { reader = new BufferedReader(new InputStreamReader(System.in)); s = reader.readLine(); if (!isNotEmpty(s)) { System.out.println('rn請輸入執行路徑rn'); continue; }else { File file = new File(s); if(!file.exists()){ System.out.println('rn該路徑不存在,請重新輸入rn'); continue; } } path = s; break;} } else if('pass'.equals(s)) {System.out.println('rn請輸入加/解密密碼rn');while (true) { reader = new BufferedReader(new InputStreamReader(System.in)); s = reader.readLine(); if (!isNotEmpty(s)) { System.out.println('rn請輸入加/解密密碼rn'); continue; } pass = s; break;} } else if ('1'.equals(s)) {mode = s;System.out.println('rn當前已選模式:加密n'); } else if ('2'.equals(s)) {mode = s;System.out.println('rn當前已選模式:解密rn'); }else if('start'.equals(s)){if(!isNotEmpty(path)) { System.out.println('rn請輸入加/解密密碼再開始rn'); continue;}if(!isNotEmpty(pass)) { System.out.println('rn請輸入執行路徑后再開始rn'); continue;}if ('1'.equals(mode)) { encrypt(path); System.out.println('rnrn操作完成rnrn');} else { try { decrypt(path); System.out.println('rnrn操作完成rnrn'); }catch (BadPaddingException e) { System.out.println('文件:'+currentFilePath+'解密失敗,密碼錯誤'); }} }else if('quit'.equals(s)){System.exit(-1); } else {System.out.println('rn輸入錯誤rn'); } } } /** * 輸出提示語 */ private static void print(){ System.out.println('rn' +'輸入path開始選擇執行路徑rn' +'輸入pass開始選擇加/解密密碼rn' +'輸入如下數字以選擇處理模式:1 加密 2 解密rn' +'輸入start則開始執行已選操作rn' +'輸入quit則退出程序rn' +'當前選擇路徑:'+path+'rn' +'當前選擇模式:'+mode+'rn' +'當前選擇密碼:'+pass+'rn'); } public static void main(String args[]) throws IOException, GeneralSecurityException { deal(); }}

以上就是JAVA 實現磁盤文件加解密操作的示例代碼的詳細內容,更多關于Java 文件加解密的資料請關注好吧啦網其它相關文章!

標簽: Java
相關文章:
主站蜘蛛池模板: 外观设计_设备外观设计_外观设计公司_产品外观设计_机械设备外观设计_东莞工业设计公司-意品深蓝 | 山东信蓝建设有限公司官网 | 山东活动策划|济南活动公司|济南公关活动策划-济南锐嘉广告有限公司 | 雨水收集系统厂家-雨水收集利用-模块雨水收集池-徐州博智环保科技有限公司 | 九爱图纸|机械CAD图纸下载交流中心| 两头忙,井下装载机,伸缩臂装载机,30装载机/铲车,50装载机/铲车厂家_价格-莱州巨浪机械有限公司 | 微学堂-电动能源汽车评测_电动车性能分享网 | 中直网_行业门户-行业人专业的交流平台!| 日本细胞免疫疗法_肿瘤免疫治疗_NK细胞疗法 - 免疫密码 | 中空玻璃生产线,玻璃加工设备,全自动封胶线,铝条折弯机,双组份打胶机,丁基胶/卧式/立式全自动涂布机,玻璃设备-山东昌盛数控设备有限公司 | 水篦子|雨篦子|镀锌格栅雨水篦子|不锈钢排水篦子|地下车库水箅子—安平县云航丝网制品厂 | 光谱仪_积分球_分布光度计_灯具检测生产厂家_杭州松朗光电【官网】 | 聚氨酯保温钢管_聚氨酯直埋保温管道_聚氨酯发泡保温管厂家-沧州万荣防腐保温管道有限公司 | 影视模板素材_原创专业影视实拍视频素材-8k像素素材网 | 污水处理设备维修_污水处理工程改造_机械格栅_过滤设备_气浮设备_刮吸泥机_污泥浓缩罐_污水处理设备_污水处理工程-北京龙泉新禹科技有限公司 | 北京企业宣传片拍摄_公司宣传片制作-广告短视频制作_北京宣传片拍摄公司 | 蓝鹏测控平台 - 智慧车间系统 - 车间生产数据采集与分析系统 | 公交驾校-北京公交驾校欢迎您!| 空冷器|空气冷却器|空水冷却器-无锡赛迪森机械有限公司[官网] | 环氧铁红防锈漆_环氧漆_无溶剂环氧涂料_环氧防腐漆-华川涂料 | 低合金板|安阳低合金板|河南低合金板|高强度板|桥梁板_安阳润兴 北京租车牌|京牌指标租赁|小客车指标出租 | Trimos测长机_测高仪_TESA_mahr,WYLER水平仪,PWB对刀仪-德瑞华测量技术(苏州)有限公司 | 煤矿支护网片_矿用勾花菱形网_缝管式_管缝式锚杆-邯郸市永年区志涛工矿配件有限公司 | OpenI 启智 新一代人工智能开源开放平台| TPE塑胶原料-PPA|杜邦pom工程塑料、PPSU|PCTG材料、PC/PBT价格-悦诚塑胶 | 车牌识别道闸_停车场收费系统_人脸识别考勤机_速通门闸机_充电桩厂家_中全清茂官网 | 淬火设备-钎焊机-熔炼炉-中频炉-锻造炉-感应加热电源-退火机-热处理设备-优造节能 | 四川职高信息网-初高中、大专、职业技术学校招生信息网 | 隔离变压器-伺服变压器--输入输出电抗器-深圳市德而沃电气有限公司 | 净化车间_洁净厂房_净化公司_净化厂房_无尘室工程_洁净工程装修|改造|施工-深圳净化公司 | Magnescale探规,Magnescale磁栅尺,Magnescale传感器,Magnescale测厚仪,Mitutoyo光栅尺,笔式位移传感器-苏州连达精密量仪有限公司 | 手术示教系统-数字化手术室系统-林之硕医疗云智能视频平台 | 体感VRAR全息沉浸式3D投影多媒体展厅展会游戏互动-万展互动 | 亚克力制品定制,上海嘉定有机玻璃加工制作生产厂家—官网 | 康明斯发电机,上柴柴油发电机,玉柴柴油发电机组_海南重康电力官网 | 交通气象站_能见度检测仪_路面状况监测站- 天合环境科技 | 芜湖厨房设备_芜湖商用厨具_芜湖厨具设备-芜湖鑫环厨具有限公司 控显科技 - 工控一体机、工业显示器、工业平板电脑源头厂家 | 深圳宣传片制作-企业宣传视频制作-产品视频拍摄-产品动画制作-短视频拍摄制作公司 | 济南ISO9000认证咨询代理公司,ISO9001认证,CMA实验室认证,ISO/TS16949认证,服务体系认证,资产管理体系认证,SC食品生产许可证- 济南创远企业管理咨询有限公司 郑州电线电缆厂家-防火|低压|低烟无卤电缆-河南明星电缆 | 杭州标识标牌|文化墙|展厅|导视|户内外广告|发光字|灯箱|铭阳制作公司 - 杭州标识标牌|文化墙|展厅|导视|户内外广告|发光字|灯箱|铭阳制作公司 | 炭黑吸油计_测试仪,单颗粒子硬度仪_ASTM标准炭黑自销-上海贺纳斯仪器仪表有限公司(HITEC中国办事处) |