當前位置:首頁 » 密碼管理 » javamd5加密文件

javamd5加密文件

發布時間: 2022-05-27 20:51:21

1. java 關於md5加密的問題

MD5加密是單向的,加密之後,無法反向解密
就是說你只能檢查"aa"的MD5值是不是
而無法通過得到"aa"

2. java 中如何進行md5加密

JDK裡面有一個java.security.MessageDigest類,這個類就是用來加密的。

加密代碼如下:

Stringtoken=System.currentTimeMillis()+newRandom().nextInt()+"";
try{
MessageDigestmd=MessageDigest.getInstance("MD5");
byte[]md5=md.digest(token.getBytes());
}catch(Exceptione){
thrownewRuntimeException(e);
}

這個byte類型的數組就是使用MD5加密後的結果

3. 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);
}

自己好好研究一下,我只用了其中一部分,你需要就調試調試,也許會有點錯誤。

4. java 中怎麼使用md5加密

樓主您好
JDK裡面有一個java.security.MessageDigest類,這個類就是用來加密的。
String token = System.currentTimeMillis()+new Random().nextInt()+"";<img id="selectsearch-icon" src="https://gss0.bdstatic.com/70cFsjip0QIZ8tyhnq/img/iknow/qb/select-search.png" alt="搜索" class="selectsearch-hide">
try {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] md5 = md.digest(token.getBytes());
} catch (Exception e) {
throw new RuntimeException(e);
}

5. java中md5加密

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class md5 {
public String str;

public void md5s(String plainText) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(plainText.getBytes());
byte b[] = md.digest();

int i;

StringBuffer buf = new StringBuffer("");
for (int offset = 0; offset < b.length; offset++) {
i = b[offset];
if (i < 0)
i += 256;
if (i < 16)
buf.append("0");
buf.append(Integer.toHexString(i));
}
str = buf.toString();
System.out.println("result: " + buf.toString());// 32位的加密
System.out.println("result: " + buf.toString().substring(8, 24));// 16位的加密
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();

}
}

public static void main(String agrs[]) {
md5 md51 = new md5();
md51.md5s("4");//加密4
}

}

6. java文件md5值 什麼意思

MD5是常用的一種加密方式,原數據加過加密演算法後的得到的數據就是MD5值

用戶的密碼很多是以MD5值(或類似的其它演算法)的方式保存的,這樣即使資料庫被侵入,也不能直接得到用戶的原始密碼

7. java用md5密碼加密有必要嗎

有必要的,md5就是為了防止人偷窺,而當密碼很短的時候,利用暴力搜索也比較容易搜索到,只有密碼強度足夠的情況下才有意義,你想轉換也就沒那麼容易了。md5類hash演算法的設計初衷就是單向,即不可逆。

8. 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進制字元。

9. Java MD5如何解密

MD5 不能解密, MD5的破解方式就是 把不同的字元串按MD5加密 然後對比加密後的結果是不是一樣. 在線MD5解密 也是這樣的原理.

10. java MD5加密問題

沒有

一般驗證是把用戶登錄輸入的密碼也用md5加密,和資料庫的密碼進行判斷是否相等,相等就是正確的密碼

md5是不可逆的

聽說md5現在可以破解了,
滿意請採納。

熱點內容
如何在別人的伺服器加模組 發布:2024-05-20 21:28:29 瀏覽:60
伺服器的bios晶元電腦店有嗎 發布:2024-05-20 21:28:26 瀏覽:223
剪輯電影什麼配置 發布:2024-05-20 21:25:17 瀏覽:818
解壓神器中的詭異事件 發布:2024-05-20 21:17:59 瀏覽:7
星火草原系統源碼 發布:2024-05-20 21:12:44 瀏覽:767
c編譯器手機版中文版下載 發布:2024-05-20 21:11:56 瀏覽:777
存儲超融合 發布:2024-05-20 21:07:24 瀏覽:455
孩子培訓編程 發布:2024-05-20 21:05:57 瀏覽:455
linux伺服器源碼 發布:2024-05-20 21:05:00 瀏覽:400
javaread 發布:2024-05-20 20:51:52 瀏覽:789