文件管理 · 2022年8月2日

javamd5文件加密|java如何实现md5加密的压缩为zip

① java web md5加密的使用

1、Java中你可抄以用MD5 util工具类,网袭上有md5工具类的,你下载一个,在数据入库时候,进行密文md5一下在存入数据库就行2、非重要性数据使用md5是没有意义的3、以上个人观点,如果还有什么不懂的可以在继续追问

② 谁告诉我在java程序中md5怎么加密密码要后面注释。有的运算符号我不知道。比如">>>"和"0xf"

MessageDigest instance = MessageDigest.getInstance("md5"); byte[] bb = instance.digest("1".getBytes()); StringBuilder ss = new StringBuilder(); for (byte b : bb) { if (Integer.toHexString(0xFF & b).length() == 1) { ss.append("0"); } ss.append(Integer.toHexString(0xFF & b)); } System.out.println(ss.toString().toUpperCase());

③ java MD5加密问题

jdk本身有md5的加密方法,也可以用appache下面的jar包。加密后保存在数据库,平时在调用出对比,是否正确!

④ java用md5密码加密有必要吗

md5加密是为抄了原信息的准确性,因为md5是不可逆加密。

有两个例子,比如

存在数据库中的密码,加密后就算被人看到也不知道原密码是什么,但是可以对输入的原密码加密,然后两者比较用于验证。

发布软件的时候同时发布md5码,防止恶意篡改原程序

⑤ java 关于md5加密的问题

MD5加密是单向的,加密之后,无法反向解密就是说你只能检查"aa"的MD5值是不是而无法通过得到"aa"

⑥ java如何实现md5加密的压缩为zip

public class ZipUtil { private static final String ALGORITHM = "PBEWithMD5AndDES"; private static Logger logger = Logger.getLogger(ZipUtil.class); public static void zip(String zipFileName, String inputFile,String pwd) throws Exception { zip(zipFileName, new File(inputFile), pwd); } /** * 功能描述:压缩指定路径下的所有文件 * @param zipFileName 压缩文件名(带有路径) * @param inputFile 指定压缩文件夹 * @return * @throws Exception */ public static void zip(String zipFileName, String inputFile) throws Exception { zip(zipFileName, new File(inputFile), null); } /** * 功能描述:压缩文件对象 * @param zipFileName 压缩文件名(带有路径) * @param inputFile 文件对象 * @return * @throws Exception */ public static void zip(String zipFileName, File inputFile,String pwd) throws Exception { ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName)); zip(out, inputFile, "",pwd); out.close(); } /** * * @param out 压缩输出流对象 * @param file * @param base * @throws Exception */ public static void zip(ZipOutputStream outputStream, File file, String base,String pwd) throws Exception { if (file.isDirectory()) { File[] fl = file.listFiles(); outputStream.putNextEntry(new ZipEntry(base + "/")); base = base.length() == 0 ? "" : base + "/"; for (int i = 0; i < fl.length; i++) { zip(outputStream, fl[i], base + fl[i].getName(), pwd); } } else { outputStream.putNextEntry(new ZipEntry(base)); FileInputStream inputStream = new FileInputStream(file); //普通压缩文件 if(pwd == null || pwd.trim().equals("")){ int b; while ((b = inputStream.read()) != -1){ outputStream.write(b); } inputStream.close(); } //给压缩文件加密 else{ PBEKeySpec keySpec = new PBEKeySpec(pwd.toCharArray()); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM); SecretKey passwordKey = keyFactory.generateSecret(keySpec); byte[] salt = new byte[8]; Random rnd = new Random(); rnd.nextBytes(salt); int iterations = 100; PBEParameterSpec parameterSpec = new PBEParameterSpec(salt, iterations); Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, passwordKey, parameterSpec); outputStream.write(salt); byte[] input = new byte[64]; int bytesRead; while ((bytesRead = inputStream.read(input)) != -1) { byte[] output = cipher.update(input, 0, bytesRead); if (output != null){ outputStream.write(output); } } byte[] output = cipher.doFinal(); if (output != null){ outputStream.write(output); } inputStream.close(); outputStream.flush(); outputStream.close(); } } file.delete(); } public static void unzip(String zipFileName, String outputDirectory)throws Exception { ZipInputStream inputStream = new ZipInputStream(new FileInputStream(zipFileName)); unzip(inputStream, outputDirectory, null); }/** * 功能描述:将压缩文件解压到指定的文件目录下 * @param zipFileName 压缩文件名称(带路径) * @param outputDirectory 指定解压目录 * @return * @throws Exception */ public static void unzip(String zipFileName, String outputDirectory, String pwd)throws Exception { ZipInputStream inputStream = new ZipInputStream(new FileInputStream(zipFileName)); unzip(inputStream, outputDirectory, pwd); }public static void unzip(File zipFile, String outputDirectory, String pwd)throws Exception { ZipInputStream inputStream = new ZipInputStream(new FileInputStream(zipFile)); unzip(inputStream, outputDirectory,pwd); }public static void unzip(ZipInputStream inputStream, String outputDirectory, String pwd) throws Exception{ ZipEntry zipEntry = null; FileOutputStream outputStream = null; try{ while ((zipEntry = inputStream.getNextEntry()) != null) { if (zipEntry.isDirectory()) { String name = zipEntry.getName(); name = name.substring(0, name.length() – 1); File file = new File(outputDirectory + File.separator + name); if(! file.exists()){ file.mkdirs(); } }else { File file = new File(outputDirectory + File.separator + zipEntry.getName()); File parentFile = file.getParentFile(); if(! parentFile.exists()){ parentFile.mkdirs(); } file.createNewFile(); outputStream = new FileOutputStream(file); if(pwd == null || pwd.trim().equals("")){ int b; byte[] buffer = new byte[1024]; while ((b = inputStream.read(buffer)) != -1){ outputStream.write(buffer); } outputStream.flush(); }else{ PBEKeySpec keySpec = new PBEKeySpec(pwd.toCharArray()); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM); SecretKey passwordKey = keyFactory.generateSecret(keySpec); byte[] salt = new byte[8]; inputStream.read(salt); int iterations = 100; PBEParameterSpec parameterSpec = new PBEParameterSpec(salt, iterations); Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, passwordKey, parameterSpec); byte[] input = new byte[64]; int bytesRead; while ((bytesRead = inputStream.read(input)) != -1) { byte[] output = cipher.update(input, 0, bytesRead); if (output != null){ outputStream.write(output); } } byte[] output = cipher.doFinal(); if (output != null){ outputStream.write(output); } outputStream.flush(); } } } } catch(IOException ex){ logger.error(ex); throw ex; }catch(Exception ex){ logger.error(ex); throw ex; }finally{ if(inputStream != null){ inputStream.close(); } if(outputStream != null){ outputStream.close(); } } } public static byte[] getByteArrayFromFile(String fileName) throws Exception{ FileInputStream fi = null; try{ File file = new File(fileName); long size = file.length(); if (size > Integer.MAX_VALUE) { throw new Exception("文件太大"); } fi = new FileInputStream(file); byte[] buffer = new byte[1024]; byte[] all = new byte[(int) size]; int offset = 0; int len = 0; while ((len = fi.read(buffer)) > -1) { System.array(buffer, 0, all, offset, len); offset += len; } return all; }catch(Exception ex){ logger.error(ex); throw ex; }finally{ if(fi != null){ fi.close(); } } } public static void createFileFromByteArray(byte[] b,String destName) throws Exception{ FileOutputStream os = null; try{ File destFile = new File(destName); File parentFile = destFile.getParentFile(); if(! parentFile.exists()){ parentFile.mkdirs(); } os = new FileOutputStream(destName); os.write(b); os.flush(); }catch(Exception e){ logger.error(e); throw e; }finally{ if(os != null){ os.close(); } } } public static void main(String[] args) throws Exception{ String fileName = "E:\\sunjinfu\\test\\test.zip"; String outputDir = "E:\\sunjinfu\\test\\csv\\123"; unzip(fileName, outputDir); }自己好好研究一下,我只用了其中一部分,你需要就调试调试,也许会有点错误。

⑦ java MD5加密,解释解释!

给你解释一下for里面这段代码byte byte0 = md[i];//取得md数组中第i个元素str[k++] = hexDigits[byte0 >>> 4 & 0xf ];取得byte0的前四位,然后找到转化成16进制字符,如果byte0为10001000(二进制)那么前四位就是1000,十进制就是8,而 hexDigits[8]就=‘8’str[k++] = hexDigits[byte0 & 0xf ]; //同理取得byte0的后四位,转化成16进制字符。

⑧ java 中如何进行md5加密

JDK里面有一个java.security.MessageDigest类,这个类就是用来加密的。

Stringtoken=System.currentTimeMillis()+newRandom().nextInt()+"";<img id="selectsearch-icon"src="https://gss0.bdstatic.com/70cFsjip0QIZ8tyhnq/img/iknow/qb/select-search.png"alt="搜索"class="selectsearch-hide">

try{

MessageDigestmd=MessageDigest.getInstance("MD5");

byte[]md5=md.digest(token.getBytes());

}catch(Exceptione){

thrownewRuntimeException(e);

}

⑨ java文件md5值 什么意思

MD5是常用的一种加密方式,原数据加过加密算法后的得到的数据就是MD5值用户的密码很多是以MD5值(或类似的其它算法)的方式保存的,这样即使数据库被侵入,也不能直接得到用户的原始密码