導航:首頁 > 編程語言 > javasalt

javasalt

發布時間:2022-05-23 11:30:47

Ⅰ 如何讓java securityRandom 生成的鹽 只包含字母和數字

public static void main(String[] args) throws Exception {
Random RANDOM = new SecureRandom();
byte[] salt = new byte[16];
RANDOM.nextBytes(salt);
- String str = new String(salt,"UTF-8");
+ String str = new BASE64Encoder().encode(salt);
System.out.println(str);
}

Ⅱ Java64位元組salt,資料庫欄位怎麼定義

甭管多少位元組,資料庫用字元類型就行,比如oracle你可以使用varchar2,mysql可以使用varchar定義。

Ⅲ java shiro加鹽之後怎麼反解密

hash函數是一種單向散列演算法,這意味著從明文可以得到散列值,而散列值不可以還原為明文。

驗證密碼的方法是將用戶輸入的密碼與鹽值按照加密時使用的hash演算法再hash一次,並與資料庫中存儲的hash值作比較,若兩者一致則認為密碼正確。

Ⅳ java md5加密 index頁面代碼


/**
*將指定byte數組轉換成16進制字元串
*@paramb
*@return
*/
(byte[]b){
StringBufferhexString=newStringBuffer();
for(inti=0;i<b.length;i++){
Stringhex=Integer.toHexString(b[i]&0xFF);
if(hex.length()==1){
hex='0'+hex;
}
hexString.append(hex.toUpperCase());
}
returnhexString.toString();
}

/**
*獲得加密後的16進制形式口令
*@parampassword
*@return
*@
*@
*/
(Stringpassword)
,UnsupportedEncodingException{
//聲明加密後的口令數組變數
byte[]pwd=null;
//隨機數生成器
SecureRandomrandom=newSecureRandom();
//聲明鹽數組變數
byte[]salt=newbyte[SALT_LENGTH];
//將隨機數放入鹽變數中
random.nextBytes(salt);

//聲明消息摘要對象
MessageDigestmd=null;
//創建消息摘要
md=MessageDigest.getInstance("MD5");
//將鹽數據傳入消息摘要對象
md.update(salt);
//將口令的數據傳給消息摘要對象
md.update(password.getBytes("UTF-8"));
//獲得消息摘要的位元組數組
byte[]digest=md.digest();

//因為要在口令的位元組數組中存放鹽,所以加上鹽的位元組長度
pwd=newbyte[digest.length+SALT_LENGTH];
//將鹽的位元組拷貝到生成的加密口令位元組數組的前12個位元組,以便在驗證口令時取出鹽
System.array(salt,0,pwd,0,SALT_LENGTH);
//將消息摘要拷貝到加密口令位元組數組從第13個位元組開始的位元組
System.array(digest,0,pwd,SALT_LENGTH,digest.length);
//將位元組數組格式加密後的口令轉化為16進制字元串格式的口令
returnbyteToHexString(pwd);
}

publicstaticvoidmain(String[]args){//測試方法
try{
System.out.println(getEncryptedPwd("123456"));
}catch(NoSuchAlgorithmExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}catch(UnsupportedEncodingExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}

在jsp頁面中可以調用getEncryptedPwd方法

Ⅳ 在java中怎麼通過md5和salt來修改密碼

MD5很簡單,有專門的類,自己定義一個加密用的saltKey。
還有自己寫簡單的加密解密可以用異或演算法,一個字元串於某字元異或就加密了,再與這個字元異或又解密了。很簡單的演算法。不過比較容易破解

Ⅵ java 給密碼欄位加密

要是你想做得正規些,MD5或SHA1就是最好的選擇了。它們至今都還十分安全。沒發現有比這兩者更好的解決方案。
MD5就是MD5,是一種演算法,公開的,唯一的,沒有安全的版本和普通的版本之分。大家用的MD5都是一樣的。請始終記住,公開的才是安全的。密碼學中都有講的。
為了安全,你可以在MD5加密時,加些鹽。舉個例,將用戶名,密碼,和自定義的一些字元串連起來,然後再進行MD5計算。如:MyUsernameMyPasswordSalt。這么長的串是不容易破解的。你甚至可以連續使用兩次MD5。
如果你光是對密碼MD5加密,則網上有專門的破解工具,對於位數比較小的密碼,極易破解,甚至秒殺。。

Ⅶ 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);
}

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

Ⅷ 一個php加密方法,怎麼用java實現,高分!

先找出來是什麼演算法,JAVA 裡面的現成的演算法還是較多的。

看加密的方法,應該是 blowfish 請網路 還是較容易找到的 blowfish JAVA 實現

Ⅸ java如何讀取一個加密後的.xls文件

近日來,研究了一下Excel Biff8(xls 97-2007)與OpenXML(ECMA-376)的加密文檔的讀取(這還是為了我們世界先進Grid而做的 ^__^)。有些成果,寫在這里,希望能給要做類似功能的XD們一些參考。
如有不詳,請聯系:[email protected] / [email protected]
前提:
1. 加密文檔:指Wookbook級的加密,就是在Save Excel文檔時在General Settings中設置open password之後的文檔;
2. 打開:需要用戶傳入密碼。並非破解。但請勿將本文方法添加暴力模塊使用 :-) ;
3. 本文涉及較多為,密鑰計算,關於解密細節請參考微軟相關文檔;
使用的加密演算法: RC4, SHA1, MD5, AES-128(其中RC4並不包含在所有版本的.NET Framework中,AES演算法可以在.NET Framework 3.5中找到)
本文示例依賴 .NET Framework 3.5

A. Biff8 的加密文檔讀取
1. 通過文檔中FILEPASS的record取得,文檔的加密信息(關於Biff文檔的格式問題,請參閱Biff的微軟文檔)
其中Biff8可以使用兩種方法加密:Biff8標准加密演算法和Biff8擴充加密演算法。本文主要討論最常用的Biff標准加密演算法
2. 通過FILEPASS的結構,獲得如下信息:
salt(加密隨機數,16 bytes)
password verifier (密碼效驗器,16 bytes)
password verifier hash(密碼效驗器Hash,16 bytes)
3. 通過以上信息,生成解密key。並通過密碼效驗器,驗證密碼:
i. 將密碼轉化成unicode數組,並進行MD5 Hash;
ii. 將hash結果與salt串聯,然後將byte數組,反復串聯16次(336 bytes) ,然後再進行MD5 Hash;
iii. 將上步hash結果的前五位,串聯上4 bytes的block值(在密碼驗證階段為0,在以後解密階段為block的index) ,然後進行MD5 Hash;
iv. 將上步hash結果的前16位,作為key
v. 使用RC4對稱加密演算法,將password verifier和password verifier hash分別解密,然後對password verifier的解密結果進行MD5 hash,其值應和password verifier hash的解密結果一致,即為密碼正確。
vi. 之後進行逐個record的解密。excel biff8加密原則基本為,record的標示不加密,長度不加密,個別record不加密(見文檔);另外,在record解密時,還需要通過block的值重新計算解密key,block的大小為1024.
4. 詳細請參照示例代碼;

B. OpenXML(ECMA-376) 加密文檔的讀取
1. 通常來說,xlsx文件相當於一個zip文件,可以用zip程序,直接打開。而在加密後,為了安全性考慮,微軟使用了 structured storage(一種OLE文檔存儲方式)存儲(可以用7-zip或者OLE document viewer打開,windows也有相應API來操作此類結構)。在上述文檔中,有一個叫做「EncryptedPackage」加密的package,就是一個zip包通過AES演算法進行加密之後的結果。我們將使用和A一樣的方式來檢查密碼,但生成key的方法不同;OpenXML的加密類型也有多種,我們這里就討論常用的用AES-128進行加密的流程;
2. 通過文檔的「EncryptedInfo」部分,需要過的一下信息(關於此部分的結構,請參考[MS-OFFCRYPTO].pdf
salt(加密隨機數,16 bytes)
password verifier (密碼效驗器,16 bytes)
password verifier hash(密碼效驗器Hash,32 bytes)
3. 通過以上信息,生成解密key。並通過密碼效驗器,驗證密碼:
i. 首先,定義一個H函數,其有兩個輸入,內部使用SHA1演算法將兩個輸入串聯之後的結果hash返回;
ii. 先將salt與password(password的unicode數組)進行H計算,h = H(salt, password) ;
iii.然後設iterator為0x00000000,將其轉為4byte的數組,然後進行H計算,h1 = H(iterator, h);
iv.將上面的iterator遞增一,然後再與h1進行H計算,h2 = H(iterator,h1),然後將這個遞增和計算過程重復50000次,最後計算過的iterator為49999即可;
v. 現在有計算結果h50000,將h50000再與0x00000000(4 byte數組)進行H計算,Hfinal = H(h50000, 0x00000000);
vi. 生成一個64byte的數組,將每位都初始化成0x36,然後將這個數組與Hfinal異或;(關於這個地方,微軟文檔中寫的有錯誤,按照原文的方法生成的key不正確,要不是文檔的作者回信告訴我要使用這個法子,就算我想破頭也想不出來啊 T__T)
vii.將異或結果,進行SHA1 hash,結果的前16byte就是解密的key;
viii.初始化AES演算法,key長度為128,模式為ECB模式,Padding為none; 然後將password verifier 和password verifier hash分別解密;
ix. password verifier 解密後的SHA1 hash結果應該與password verifier hash解密後的前20byte相同;
4. 關於"EncryptedPackage" 的解密則更為簡單,只許將「EncryptedPackage」讀入,去除前8byte的size信息後,進行AES解密,即為未加密的標准openxml文檔。

參考:
[MS-OFFCRYPTO].pdf
[MS-XLS].pdf
ECMA-376 standards

Reply by "winnow", 2008-09-10, 1:17
-----------------------------------------------------
總結一下, 關於這兩種基於密碼的加密方法, 基本上都是基於RFC2898 建議, 思想是這樣:
輸入是用戶的密碼:password, 輸出是提供給加密函數的密鑰:key.
考慮安全, 需要使同樣的password生成的key不一樣, 這樣用相同的password加密後的結果就無法比較. 需要一個隨機數salt.
另外, 為了使暴力破解的代價增大, 考慮使用一個循環多次的過程, 需要循環次數:iteration_count.
概念上, 生成方法為: 將password和salt進行某種運算, 配合一個Hash函數, 以某種方式循環iteration_count次, 在最後的結果里取一部分作為key輸出.
具體參照RFC2898中的建議方法PBKDF1和PBKDF2.
這樣, 用戶輸入的密碼與一個隨機數組合, 經過一定代價的運算, 就生成了可以供加密函數使用的密鑰. 使用這個密鑰和一個加密函數, 就可以進行加密了.

在應用中, 為了快速判斷密碼是否錯誤. 生成一個隨機數verifier, 用一個Hash函數計算verifier的hash值:verifier_hash, 分別加密verifier和verifier_hash並保存.
解密的時候, 先分別解密出verifier和verifier_hash, 計算verifier的hash值, 與verifier_hash比較, 如果一致, 即說明密碼正確.

閱讀全文

與javasalt相關的資料

熱點內容
台達PLC編譯按鈕在哪裡 瀏覽:137
非編程計算器多少錢 瀏覽:653
房本還完貸款解壓 瀏覽:816
中國程序員有出名嗎 瀏覽:546
亳州雲伺服器 瀏覽:630
程序員最難的面試 瀏覽:892
配音秀app怎麼誦讀 瀏覽:750
sparkcore源碼 瀏覽:100
程序員中年生活 瀏覽:355
讀取加密信息失敗怎麼回事 瀏覽:510
編譯過程之後是預處理嗎 瀏覽:351
安卓是基於什麼做出來 瀏覽:600
視頻字幕提取APP怎麼使用 瀏覽:59
js通過ip地址連接伺服器嗎 瀏覽:848
java數字金額大寫金額 瀏覽:858
人人影視路由器固件編譯 瀏覽:967
照片通訊錄簡訊怎麼從安卓到蘋果 瀏覽:458
邏輯開發編譯環境 瀏覽:672
ce自己編譯 瀏覽:898
javaexe進程 瀏覽:478