『壹』 求一個用java編寫的可逆的加密演算法程序,自己寫的小程序也行。
public class mySecurity {
private static KeyGenerator keygen ;
private static SecretKey secretKey;
private static Cipher cipher;
private static mySecurity security = null;
private mySecurity(){
}
public static mySecurity getInstance() throws Exception{
if(security == null){
security = new mySecurity();
keygen = KeyGenerator.getInstance("AES");
secretKey = keygen.generateKey();
cipher =Cipher.getInstance("AES");
}
return security;
}
//加密
public String encrypt(String str) throws Exception{
cipher.init(Cipher.ENCRYPT_MODE,secretKey);
byte [] src = str.getBytes(); byte [] enc = cipher.doFinal(src);
return parseByte2HexStr(enc); }
//解密
public String decrypt(String str) throws Exception{
cipher.init(Cipher.DECRYPT_MODE,secretKey);
byte[] enc = parseHexStr2Byte(str); byte [] dec = cipher.doFinal(enc);
return new String(dec); }
/**將16進制轉換為二進制
* @param hexStr
* @return
*/
public static byte[] parseHexStr2Byte(String hexStr) {
if (hexStr.length() < 1)
return null;
byte[] result = new byte[hexStr.length()/2];
for (int i = 0;i< hexStr.length()/2; i++) {
int high = Integer.parseInt(hexStr.substring(i*2, i*2+1), 16);
int low = Integer.parseInt(hexStr.substring(i*2+1, i*2+2), 16);
result[i] = (byte) (high * 16 + low);
}
return result;
}
/**將二進制轉換成16進制
* @param buf
* @return
*/
public static String parseByte2HexStr(byte buf[]) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < buf.length; i++) {
String hex = Integer.toHexString(buf[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
sb.append(hex.toUpperCase());
}
return sb.toString();
}
public static void main(String[] args) throws Exception{
String str = "abc haha 我";
String ss = mySecurity.getInstance().encrypt(str) ;
System.out.println(ss);
System.out.println(mySecurity.getInstance().decrypt(ss));
}
}
『貳』 java十大演算法
演算法一:快速排序演算法
快速排序是由東尼·霍爾所發展的一種排序演算法。在平均狀況下,排序 n 個項目要Ο(n log n)次比較。在最壞狀況下則需要Ο(n2)次比較,但這種狀況並不常見。事實上,快速排序通常明顯比其他Ο(n log n) 演算法更快,因為它的內部循環(inner loop)可以在大部分的架構上很有效率地被實現出來。
快速排序使用分治法(Divide and conquer)策略來把一個串列(list)分為兩個子串列(sub-lists)。
演算法步驟:
1 從數列中挑出一個元素,稱為 "基準"(pivot),
2 重新排序數列,所有元素比基準值小的擺放在基準前面,所有元素比基準值大的擺在基準的後面(相同的數可以到任一邊)。在這個分區退出之後,該基準就處於數列的中間位置。這個稱為分區(partition)操作。
3 遞歸地(recursive)把小於基準值元素的子數列和大於基準值元素的子數列排序。
遞歸的最底部情形,是數列的大小是零或一,也就是永遠都已經被排序好了。雖然一直遞歸下去,但是這個演算法總會退出,因為在每次的迭代(iteration)中,它至少會把一個元素擺到它最後的位置去。
演算法二:堆排序演算法
堆排序(Heapsort)是指利用堆這種數據結構所設計的一種排序演算法。堆積是一個近似完全二叉樹的結構,並同時滿足堆積的性質:即子結點的鍵值或索引總是小於(或者大於)它的父節點。
堆排序的平均時間復雜度為Ο(nlogn) 。
演算法步驟:
創建一個堆H[0..n-1]
把堆首(最大值)和堆尾互換
3. 把堆的尺寸縮小1,並調用shift_down(0),目的是把新的數組頂端數據調整到相應位置
4. 重復步驟2,直到堆的尺寸為1
演算法三:歸並排序
歸並排序(Merge sort,台灣譯作:合並排序)是建立在歸並操作上的一種有效的排序演算法。該演算法是採用分治法(Divide and Conquer)的一個非常典型的應用。
演算法步驟:
1. 申請空間,使其大小為兩個已經排序序列之和,該空間用來存放合並後的序列
2. 設定兩個指針,最初位置分別為兩個已經排序序列的起始位置
3. 比較兩個指針所指向的元素,選擇相對小的元素放入到合並空間,並移動指針到下一位置
4. 重復步驟3直到某一指針達到序列尾
5. 將另一序列剩下的所有元素
『叄』 用JAVA設計一個簡單的加密、解密演算法,用該演算法來實現對數據的加密、解密
給你個MD5的加密演算法
package test;
import java.security.MessageDigest;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author unascribed
* @version 1.0
*/
public class StringUtil {
private final static String[] hexDigits = {
"0", "1", "2", "3", "4", "5", "6", "7",
"8", "9", "a", "b", "c", "d", "e", "f"};
/**
* 轉換位元組數組為16進制字串
* @param b 位元組數組
* @return 16進制字串
*/
public static String byteArrayToHexString(byte[] b) {
StringBuffer resultSb = new StringBuffer();
for (int i = 0; i < b.length; i++) {
resultSb.append(byteToHexString(b[i]));
}
return resultSb.toString();
}
private static String byteToHexString(byte b) {
int n = b;
if (n < 0)
n = 256 + n;
int d1 = n / 16;
int d2 = n % 16;
return hexDigits[d1] + hexDigits[d2];
}
public static String MD5Encode(String origin) {
String resultString = null;
try {
resultString=new String(origin);
MessageDigest md = MessageDigest.getInstance("MD5");
resultString=byteArrayToHexString(md.digest(resultString.getBytes()));
}
catch (Exception ex) {
}
return resultString;
}
public static void main(String[] args){
System.err.println(MD5Encode("a"));
}
}
在RFC 1321中,給出了Test suite用來檢驗你的實現是否正確:
MD5 ("") =
MD5 ("a") =
MD5 ("abc") =
MD5 ("message digest") =
MD5 ("abcdefghijklmnopqrstuvwxyz") =
『肆』 android,java 通用的加密解密方式有幾種
移動端越來越火了,我們在開發過程中,總會碰到要和移動端打交道的場景,比如.NET和android或者iOS的打交道。為了讓數據交互更安全,我們需要對數據進行加密傳輸。今天研究了一下,把幾種語言的加密都實踐了一遍,實現了.NET,java(android),iOS都同一套的加密演算法,下面就分享給大家。
AES加密有多種演算法模式,下面提供兩套模式的可用源碼。
加密方式:
先將文本AES加密
返回Base64轉碼
解密方式:
將數據進行Base64解碼
進行AES解密
一、CBC(Cipher Block Chaining,加密塊鏈)模式
是一種循環模式,前一個分組的密文和當前分組的明文異或操作後再加密,這樣做的目的是增強破解難度.
密鑰
密鑰偏移量
java/adroid加密AESOperator類:
package com.bci.wx.base.util;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
/**
* AES 是一種可逆加密演算法,對用戶的敏感信息加密處理 對原始數據進行AES加密後,在進行Base64編碼轉化;
*/
public class AESOperator {
/*
* 加密用的Key 可以用26個字母和數字組成 此處使用AES-128-CBC加密模式,key需要為16位。
*/
private String sKey = "smkldospdosldaaa";//key,可自行修改
private String ivParameter = "0392039203920300";//偏移量,可自行修改
private static AESOperator instance = null;
private AESOperator() {
}
public static AESOperator getInstance() {
if (instance == null)
instance = new AESOperator();
return instance;
}
public static String Encrypt(String encData ,String secretKey,String vector) throws Exception {
if(secretKey == null) {
return null;
}
if(secretKey.length() != 16) {
return null;
}
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
byte[] raw = secretKey.getBytes();
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
IvParameterSpec iv = new IvParameterSpec(vector.getBytes());// 使用CBC模式,需要一個向量iv,可增加加密演算法的強度
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
byte[] encrypted = cipher.doFinal(encData.getBytes("utf-8"));
return new BASE64Encoder().encode(encrypted);// 此處使用BASE64做轉碼。
}
// 加密
public String encrypt(String sSrc) throws Exception {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
byte[] raw = sKey.getBytes();
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());// 使用CBC模式,需要一個向量iv,可增加加密演算法的強度
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));
return new BASE64Encoder().encode(encrypted);// 此處使用BASE64做轉碼。
}
// 解密
public String decrypt(String sSrc) throws Exception {
try {
byte[] raw = sKey.getBytes("ASCII");
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());
cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
byte[] encrypted1 = new BASE64Decoder().decodeBuffer(sSrc);// 先用base64解密
byte[] original = cipher.doFinal(encrypted1);
String originalString = new String(original, "utf-8");
return originalString;
} catch (Exception ex) {
return null;
}
}
public String decrypt(String sSrc,String key,String ivs) throws Exception {
try {
byte[] raw = key.getBytes("ASCII");
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
IvParameterSpec iv = new IvParameterSpec(ivs.getBytes());
cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
byte[] encrypted1 = new BASE64Decoder().decodeBuffer(sSrc);// 先用base64解密
byte[] original = cipher.doFinal(encrypted1);
String originalString = new String(original, "utf-8");
return originalString;
} catch (Exception ex) {
return null;
}
}
public static String encodeBytes(byte[] bytes) {
StringBuffer strBuf = new StringBuffer();
for (int i = 0; i < bytes.length; i++) {
strBuf.append((char) (((bytes[i] >> 4) & 0xF) + ((int) 'a')));
strBuf.append((char) (((bytes[i]) & 0xF) + ((int) 'a')));
}
return strBuf.toString();
}
『伍』 JAVA如何對URL進行加密和解密啊
一般我們都是通過在地址里,再加個驗證密鑰參數,來做安全驗證,如果參數被改,把加密後的值跟驗證密鑰做對比就不一樣,程序做下判斷,拒絕訪問就行。
第二種方法,真要加密,就找個可逆的加密演算法(自己搜),把地址參數字元串加密後,到服務端獲取到這串加密字元,解密後,再分解參數。
『陸』 使用java做一個加密和隱藏文件的軟體,具體需要怎麼做求指導
不知道你打算怎麼加密呢?隱藏又是什麼意思?是將多個文件合成一個嗎?
因為從操作系統層面來說理應能看到所有合法的文件,因此想要讓操作系統都看不到基本上是不可能的(何況Java也是用的操作系統API來實現對文件的操作)。
就加密我說說我的想法吧,首先需要一個加密的演算法。這個演算法需要滿足:演算法可逆,雙向計算復雜度(時間/空間)低,安全程度高,可靠性高。另外可以考慮並行化來增加性能,因為現在的文件系統大多比較大,管理的東西都不小。
如果能找到這樣一個演算法,可以對於文件使用二進制的讀寫(Binary I/O),然後每讀到一定大小的數據就進行加密運算,並寫入目標加密文件中。如果是解密則是讀取數據進行解密運算。
題外話:我覺得實際上做這樣一個軟體也沒有必要,每次存取文件都需要進行大量的計算操作,也很容易破壞cache的局部性原理。如果真的需要對一部分文件進行加密,也有很多現成的工具可用,甚至於是說現在的壓縮文件都可以帶上密碼加密。所以我認為這個軟體的前景不大,當然如果只是用來玩一玩也是可以的,只不過演算法比較難找而已。(如果用RSA這種級別的演算法估計也行的吧……)
『柒』 java中,用不定長字元串生成定長字元串,要求可逆
你說的這個事情是不可能完成的,謝謝
簡單的抽屜原理就能說明呀
如果 被轉換的 字元串的長度為a,轉換後的長度為b
那麼由字元的范圍c可知,a能表示的各種組合為 c的a次方,b能表示的各種組合為c的b次方
當 a>b 的時候,將所有可能出現的字元串都進行轉化,無論轉換的方式為什麼
那麼必然有超過2個源字元串指向了同一個目標,那麼目標還原的時候就不知道是哪一個了
所以說 MD5本身不是加密演算法,是簽名演算法!!這個很重要!
希望你看懂了……
『捌』 JAVA:MD5把a加密成b,還能把b還原成a么
MD5加密演算法是不可逆演算法,所以是不可以還原成a的。
『玖』 java 密碼加密後為什麼不能解密
首先確認你的加密演算法是可逆的,比如DES/RSA,如果是MD5/SHA這類的哈希演算法是沒有對應演算法解密的。
其次確認你的解密演算法與加密時是匹配的,演算法和參數都要匹配。
『拾』 Java 加密解密的方法都有哪些
加密解密並非java才有的,所有編程語言都有加密和解密。
目前的加密解密主要可分為以下2大類:
對稱秘鑰加密:如DES演算法,3DES演算法,TDEA演算法,Blowfish演算法,RC5演算法,IDEA演算法等。其主要特點是加密方和解密方都有同一個密碼,加密方和解密方可以使用秘鑰任意加密解密。
非對稱密碼加密:這種加密方式加密方僅有加密秘鑰,對加密後的密文無法反向解密,解密方僅有解密秘鑰,無法對明文進行加密。
另外還有一些摘要演算法,比如MD5和HASH此類演算法不可逆,但經常用來作為確認欄位或者對一些重要匹配信息簽名防止明文內容被修改。