| 知乎專欄 | 多維度架構 | 微信號 netkiller-ebook | QQ群:128659835 請註明“讀者” |
第一種方法:
Crypt.java
Import netkiller. Security;
Crypt pw = new Crypt();
String passwd = pw.crypt(“passwd”,”salt”);
System.out.println(passwd);
關於JAVA的Crypt包請與我聯繫
第二種方法:
使用PostgreSQL JDBC中提供的org.postgresql.util.UnixCrypt產生crypt。
Class postgresql.util.UnixCrypt
java.lang.Object
|
+----postgresql.util.UnixCrypt
公共類 UnixCrypt 擴展 Object
這個類為我們提供了在通過網絡流傳輸口令時的加密的功能
包含靜態方法用於加密口令和與 Unix 加密的口令比較.
參閲 John Dumas 的 Java Crypt (加密)頁面獲取原始代碼.
http://www.zeh.com/local/jfd/crypt.html
方法
public static final String crypt(String salt, String original)
加密給出了明文口令和一個"種子"("salt")的口令.
參數:
salt - 一個兩字元字串代表的所用的種子, 用以向加密引擎說明加密的不同方式.如果你要生成一個新的密文那麼這個值應該是隨機生成的.
original - 待加密口令.
返回:
一個字串, 先是 2 字元的種子, 然後跟着密文口令.
方法:
1. 安裝PostgreSQL JDBC,請到http://www.postgresql.org 下載
2. 將JDBC的.jar檔案加到JAVA 的CLASSPATH中
3. 新建JAVA檔案。
4. 編譯javac crypt.java
5. 運行JAVA CLASS檔案 java your-package.your-class
java crypt
import org.postgresql.util.UnixCrypt;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;
public class crypt {
public static void main(String[] args) throws IOException {
String password;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the password to encrypt. Your password"+
" will be echoed on the screen,");
System.out.println("please ensure nobody is looking.");
System.out.print("password :>");
password=br.readLine();
System.out.println(UnixCrypt.crypt(password));
};
};
package cn.netkiller.security;
import java.nio.charset.StandardCharsets;
import java.security.SecureRandom;
import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
public class DES {
public DES() {
// TODO Auto-generated constructor stub
}
public static String encrypt(String text, String password) {
try {
SecureRandom random = new SecureRandom();
DESKeySpec desKey = new DESKeySpec(password.getBytes());
// 創建一個密匙工廠,然後用它把DESKeySpec轉換成
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey securekey = keyFactory.generateSecret(desKey);
// Cipher對象實際完成加密操作
Cipher cipher = Cipher.getInstance("DES");
// 用密匙初始化Cipher對象
cipher.init(Cipher.ENCRYPT_MODE, securekey, random);
// 現在,獲取數據並加密
// 正式執行加密操作
return Base64.getEncoder().encodeToString(cipher.doFinal(text.getBytes(StandardCharsets.UTF_8)));
} catch (Throwable e) {
e.printStackTrace();
}
return null;
}
private static String decrypt(String text, String password) throws Exception {
try {
// DES算法要求有一個可信任的隨機數源
SecureRandom random = new SecureRandom();
// 創建一個DESKeySpec對象
DESKeySpec desKey = new DESKeySpec(password.getBytes());
// 創建一個密匙工廠
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
// 將DESKeySpec對象轉換成SecretKey對象
SecretKey securekey = keyFactory.generateSecret(desKey);
// Cipher對象實際完成解密操作
Cipher cipher = Cipher.getInstance("DES");
// 用密匙初始化Cipher對象
cipher.init(Cipher.DECRYPT_MODE, securekey, random);
// 真正開始解密操作
return new String(cipher.doFinal(Base64.getDecoder().decode(text)), StandardCharsets.UTF_8);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
String en = DES.encrypt("Helloworld!!!", "www.netkiller.cn");
String de = DES.decrypt(en, "www.netkiller.cn");
System.out.println(en);
System.out.println(de);
}
}