md5加密小例子:
import java.security.MessageDigest;
public class StringMD5 {
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) {
StringMD5 test = new StringMD5();
String var = test.MD5Encode("123456");
System.out.println(var);
}
}
2. vb 文本的加密解密問題
Private Sub Command1_Click() Open App.Path & "\\aaa.txt" For Output As #1 Print #1, Enc(Text1.Text, 1) Close #1 Debug.Print "加密內容:" & Enc(Text1.Text, 1) MsgBox "加密文件已輸出!" End Sub Private Sub Command2_Click() Open App.Path & "\\aaa.txt" For Input As #1 Do While Not EOF(1) Line Input #1, tmp Text1.Text = deEnc(tmp, 1) Debug.Print "解密:" & tmp & "為:" & deEnc(tmp, 1) Loop Close #1 End Sub Function Enc(ByVal Str1 As String, ByVal EncCode As Long) As String ' EncCode為加密密碼,你可以自己修改 '日曜星君原創作品,QQ:575837484 Dim strEnc As String For i = 1 To Len(Str1) strEnc = strEnc & Chr(Asc(Mid(Str1, i, 1)) + EncCode) Next i Enc = strEnc End Function Function deEnc(ByVal Str1 As String, ByVal deEncCode As Long) As String ' EncCode為解密密碼,必須和加密密碼一樣,否則是亂碼! '日曜星君原創作品,QQ:575837484 Dim strDeEnc As String For i = 1 To Len(Str1) strDeEnc = strDeEnc & Chr(Asc(Mid(Str1, i, 1)) - deEncCode) Next i deEnc = strDeEnc End Functio
3. java環境下實現idea演算法的加密解密
基於Java的IDEA加密演算法探討
隨著Internet的迅速發展,電子商務的浪潮勢不可擋,日常工作和數據傳輸都放在Internet網上進行傳輸,大大提高了效率,降低了成本,創造了良好的效益。但是,由於 Internet網路協議本身存在著重要的安全問題(IP包本身並不繼承任何安全特性,很容易偽造出IP包的地址、修改其內容、重播以前的包以及在傳輸途中攔截並查看包的內容),使網上的信息傳輸存在巨大的安全風險電子商務的安全問題也越來越突出。加密是電子商務中最主要的安全技術,加密方法的選取直接影響電子商務活動中信息的安全程度,在電子商務系統中,主要的安全問題都可以通過加密來解決。數據的保密性可通過不同的加密演算法對數據加密來實現。
對我國來講,雖然可以引進很多的外國設備,但加密設備不能依靠引進,因為它涉及到網路安全、國家機密信息的安全,所以必須自己研製。當前國際上有許多加密演算法,其中DES(Data Encryption Standard)是發明最早的用得最廣泛的分組對稱加密演算法,DES用56位蜜鑰加密64位明文,輸出64位密文,DES的56位密鑰共有256 種可能的密鑰,但歷史上曾利用窮舉攻擊破解過DES密鑰,1998年電子邊境基金會(EFF)用25萬美元製造的專用計算機,用56小時破解了DES的密鑰,1999年,EFF用22小時完成了破解工作,使DES演算法受到了嚴重打擊,使它的安全性受到嚴重威脅。因為JAVA語言的安全性和網路處理能力較強,本文主要介紹使用IDEA(Internation Data Encryption Algorithm )數據加密演算法在Java環境下實現數據的安全傳輸。
一、IDEA數據加密演算法
IDEA數據加密演算法是由中國學者來學嘉博士和著名的密碼專家 James L. Massey 於1990年聯合提出的。它的明文和密文都是64比特,但密鑰長為128比特。IDEA 是作為迭代的分組密碼實現的,使用 128 位的密鑰和 8 個循環。這比 DES 提供了更多的 安全性,但是在選擇用於 IDEA 的密鑰時,應該排除那些稱為「弱密鑰」的密鑰。DES 只有四個弱密鑰和 12 個次弱密鑰,而 IDEA 中的弱密鑰數相當可觀,有 2 的 51 次方個。但是,如果密鑰的總數非常大,達到 2 的 128 次方個,那麼仍有 2 的 77 次方個密鑰可供選擇。IDEA 被認為是極為安全的。使用 128 位的密鑰,蠻力攻擊中需要進行的測試次數與 DES 相比會明顯增大,甚至允許對弱密鑰測試。而且,它本身也顯示了它尤其能抵抗專業形式的分析性攻擊。
二、Java密碼體系和Java密碼擴展
Java是Sun公司開發的一種面向對象的編程語言,並且由於它的平台無關性被大量應用於Internet的開發。Java密碼體系(JCA)和Java密碼擴展(JCE)的設計目的是為Java提供與實現無關的加密函數API。它們都用factory方法來創建類的常式,然後把實際的加密函數委託給提供者指定的底層引擎,引擎中為類提供了服務提供者介面在Java中實現數據的加密/解密,是使用其內置的JCE(Java加密擴展)來實現的。Java開發工具集1.1為實現包括數字簽名和信息摘要在內的加密功能,推出了一種基於供應商的新型靈活應用編程介面。Java密碼體系結構支持供應商的互操作,同時支持硬體和軟體實現。Java密碼學結構設計遵循兩個原則:(1)演算法的獨立性和可靠性。(2)實現的獨立性和相互作用性。演算法的獨立性是通過定義密碼服務類來獲得。用戶只需了解密碼演算法的概念,而不用去關心如何實現這些概念。實現的獨立性和相互作用性通過密碼服務提供器來實現。密碼服務提供器是實現一個或多個密碼服務的一個或多個程序包。軟體開發商根據一定介面,將各種演算法實現後,打包成一個提供器,用戶可以安裝不同的提供器。安裝和配置提供器,可將包含提供器的ZIP和JAR文件放在CLASSPATH下,再編輯Java安全屬性文件來設置定義一個提供器。Java運行環境Sun版本時,提供一個預設的提供器Sun。
三、Java環境下的實現
1.加密過程的實現
void idea_enc( int data11[], /*待加密的64位數據首地址*/ int key1[]){
int i ;
int tmp,x;
int zz[]=new int[6];
for ( i = 0 ; i < 48 ; i += 6) { /*進行8輪循環*/
for(int j=0,box=i; j<6; j++,box++){
zz[j]=key1[box];
}
x = handle_data(data11,zz);
tmp = data11[1]; /*交換中間兩個*/
data11[1] = data11[2];
data11[2] = tmp;
}
tmp = data11[1]; /*最後一輪不交換*/
data11[1] = data11[2];
data11[2] = tmp;
data11[0] = MUL(data11[0],key1[48]);
data11[1] =(char)((data11[1] + key1[49])%0x10000);
data11[2] =(char)((data11[2] + key1[50])%0x10000);
data11[3] = MUL(data11[3],key1[51]);
}
2.解密過程的實現
void key_decryExp(int outkey[])/*解密密鑰的變逆處理*/
{ int tmpkey[] = new int[52] ;
int i;
for ( i = 0 ; i < 52 ; i++) {
tmpkey[i] = outkey[ wz_spkey[i] ] ; /*換位*/
}
for ( i = 0 ; i < 52 ; i++) {
outkey[i] = tmpkey[i];
}
for ( i = 0 ; i < 18 ; i++) {
outkey[wz_spaddrever[i]] = (char)(65536-outkey[wz_spaddrever[i]]) ; /*替換成加法逆*/
}
for ( i = 0 ; i < 18 ; i++){
outkey[wz_spmulrevr[i]] =(char)(mulInv(outkey[wz_spmulrevr[i]] )); /*替換成乘法逆*/
}
}
四、總結
在實際應用中,我們可以使用Java開發工具包(JDK)中內置的對Socket通信的支持,通過JCE中的Java流和鏈表,加密基於Socket的網路通信.我們知道,加密/解密是數據傳輸中保證數據完整性的常用方法,Java語言因其平台無關性,在Internet上的應用非常之廣泛.使用Java實現基於IDEA的數據加密傳輸可以在不同的平台上實現並具有實現簡潔、安全性強等優點。
4. 求!!!用java編寫的用戶透明加密的源代碼 急!!!各位高手幫助!!!
很久以前在網上找到一,不知道對LZ有無幫助
package TestNativeOutOfMemoryError;
import java.security.Key;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
public class DesEncrypt {
Key key;
/**
* generate a KEY with the param
*
* @param strKey
*/
public void getKey(String strKey) {
try {
KeyGenerator _generator = KeyGenerator.getInstance("DES");
_generator.init(new SecureRandom(strKey.getBytes()));
this.key = _generator.generateKey();
_generator = null;
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* encrypt
*
* @param strMing to be encrypt
* @return
*/
public String getEncString(String strMing) {
byte[] byteMi = null;
byte[] byteMing = null;
String strMi = "";
try {
return byte2hex(getEncCode(strMing.getBytes()));
// byteMing = strMing.getBytes("UTF8");
// byteMi = this.getEncCode(byteMing);
// strMi = new String( byteMi,"UTF8");
} catch (Exception e) {
e.printStackTrace();
} finally {
byteMing = null;
byteMi = null;
}
return strMi;
}
/**
* decrypt
*
* @param strMi
* @return
*/
public String getDesString(String strMi) {
byte[] byteMing = null;
byte[] byteMi = null;
String strMing = "";
try {
return new String(getDesCode(hex2byte(strMi.getBytes())));
// byteMing = this.getDesCode(byteMi);
// strMing = new String(byteMing,"UTF8");
} catch (Exception e) {
e.printStackTrace();
} finally {
byteMing = null;
byteMi = null;
}
return strMing;
}
private byte[] getEncCode(byte[] byteS) {
byte[] byteFina = null;
Cipher cipher;
try {
cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, key);
byteFina = cipher.doFinal(byteS);
} catch (Exception e) {
e.printStackTrace();
} finally {
cipher = null;
}
return byteFina;
}
private byte[] getDesCode(byte[] byteD) {
Cipher cipher;
byte[] byteFina = null;
try {
cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, key);
byteFina = cipher.doFinal(byteD);
} catch (Exception e) {
e.printStackTrace();
} finally {
cipher = null;
}
return byteFina;
}
public static String byte2hex(byte[] b) {
String hs = "";
String stmp = "";
for (int n = 0; n < b.length; n++) {
stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
if (stmp.length() == 1)
hs = hs + "0" + stmp;
else
hs = hs + stmp;
}
return hs.toUpperCase();
}
public static byte[] hex2byte(byte[] b) {
if ((b.length % 2) != 0)
throw new IllegalArgumentException("illlegal data");
byte[] b2 = new byte[b.length / 2];
for (int n = 0; n < b.length; n += 2) {
String item = new String(b, n, 2);
b2[n / 2] = (byte) Integer.parseInt(item, 16);
}
return b2;
}
public static void main(String[] args) {
DesEncrypt des = new DesEncrypt();
des.getKey("aadd");
String strEnc = des.getEncString("Testing of DES encrypt");
System.out.println(strEnc);
String strDes = des.getDesString(strEnc);
System.out.println(strDes);
new DesEncrypt();
}
}
5. DES加密演算法 java實現
c語言的源代碼,供參考:
http://hi..com/gaojinshan/blog/item/8b2710c4ece4b3ce39db49e9.html
6. DES加密演算法的問題
優點:DES加密演算法密鑰只用到了64位中的56位,這樣具有高的安全性。
缺點:分組比較短、密鑰太短、密碼生命周期短、運算速度較慢。
7. 加密的文本存在網路上,然後讀取文本,解密文本內容,然後寫出文本
Private Sub Command1_Click()
Open App.Path & "\aaa.txt" For Output As #1
Print #1, Enc(Text1.Text, 1)
Close #1
Debug.Print "加密內容:" & Enc(Text1.Text, 1)
MsgBox "加密文件已輸出!"
End Sub
Private Sub Command2_Click()
Open App.Path & "\aaa.txt" For Input As #1
Do While Not EOF(1)
Line Input #1, tmp
Text1.Text = deEnc(tmp, 1)
Debug.Print "解密:" & tmp & "為:" & deEnc(tmp, 1)
Loop
Close #1
End Sub
Function Enc(ByVal Str1 As String, ByVal EncCode As Long) As String
' EncCode為加密密碼,你可以自己修改
'日曜星君原創作品,QQ:575837484
Dim strEnc As String
For i = 1 To Len(Str1)
strEnc = strEnc & Chr(Asc(Mid(Str1, i, 1)) + EncCode)
Next i
Enc = strEnc
End Function
Function deEnc(ByVal Str1 As String, ByVal deEncCode As Long) As String
' EncCode為解密密碼,必須和加密密碼一樣,否則是亂碼!
'日曜星君原創作品,QQ:575837484
Dim strDeEnc As String
For i = 1 To Len(Str1)
strDeEnc = strDeEnc & Chr(Asc(Mid(Str1, i, 1)) - deEncCode)
Next i
deEnc = strDeEnc
End Function
8. VB求高手寫下面代碼的反運算。。
解密如下:
Private Sub Command2_Click()
Dim strPsw As String, pPsw As Integer, lenPsw As Integer 'psw:password
Dim strEnc As String, pEnc As Long 'enc: 加密後的字元串
Dim strOri As String 'ori:原始字元串
Dim strWordAsc As String, strTmp As String
Dim i As Long, j As Long, k As Long
strPsw = TxMy.Text
lenPsw = Len(strPsw)
strEnc = TxNew.Text
'
'---------------------------------'建立映射表1
Dim intMap(11) As Integer, intMapID As Integer
intMap(0) = Asc("B")
intMap(1) = Asc("C")
intMap(2) = Asc("D")
intMap(3) = Asc("E")
intMap(4) = Asc("F")
intMap(5) = Asc("G")
intMap(6) = Asc("H")
intMap(7) = Asc("I")
intMap(8) = Asc("J")
intMap(9) = Asc("K")
intMap(10) = Asc("A") '-
intMap(11) = Asc("V") '空
'---------------------------------'建立密碼的單位元組的列表
Dim bytPswMap() As Byte, intPswMapID As Integer
bytPswMap() = StrConv(strPsw, vbFromUnicode)
For i = 0 To lenPsw - 1
bytPswMap(i) = bytPswMap(i) - 48
Next i
Dim intTmp As Integer, a As String
pEnc = 1 '記錄strEnc的下一個寫入位點
pPsw = 0 '記錄bytPswMap的下一個讀取位點
For i = 1 To Len(strEnc) / 6
a = ""
strWordAsc = Mid(strEnc, (i - 1) * 6 + 1, 6)
For j = 1 To Len(strWordAsc)
intTmp = Asc(Mid(strWordAsc, j, 1)) - bytPswMap(pPsw)
Mid(strEnc, pEnc, 1) = Chr(intMap(intMapID) + bytPswMap(pPsw))
pEnc = pEnc + 1
pPsw = (pPsw + 1) Mod lenPsw
For n = 0 To 11
If intMap(n) = intTmp Then
Select Case n
Case 10: a = a & "-"
Case 11: a = a & " "
Case Else: a = a & CStr(n)
End Select
Exit For
End If
Next n
Next j
strOri = strOri & Chr(Trim(a))
Next i
TxYw.Text = strOri
End Sub
信守承若-》 追加
9. 如何用IBM的jdk 寫DES 加密程序
public class DES{
public static Key key;
//根據參數生成KEY
public static void getKey(String strKey){
try{
KeyGenerator _generator = KeyGenerator.getInstance("DES");
_generator.init(new SecureRandom(strKey.getBytes()));
key = _generator.generateKey();
_generator=null;
}catch(Exception e){
e.printStackTrace();
}
}
//考試大提示加密String明文輸入,String密文輸出
public static String getEncString(String strMing){
byte[] byteMi = null;
byte[] byteMing = null;
String strMi ="";
BASE64Encoder base64en = new BASE64Encoder();
try{
byteMing = strMing.getBytes("UTF-8");
byteMi = getEncCode(byteMing);
strMi = base64en.encode(byteMi);
}catch(Exception e){
e.printStackTrace();
}finally{
base64en = null;
byteMing = null;
byteMi = null;
}
return strMi;
}
//解密 以String密文輸入,String明文輸出
public static String getDesString(String strMi) throws Exception{
BASE64Decoder base64De = new BASE64Decoder();
byte[] byteMing = null;
byte[] byteMi = null;
String strMing = "";
try{
byteMi = base64De.decodeBuffer(strMi);
byteMing = getDesCode(byteMi);
strMing = new String(byteMing,"UTF-8");
}catch(Exception e){
throw e;
}finally{
base64De = null;
byteMing = null;
byteMi = null;
}
return strMing;
}
//加密以byte[]明文輸入,byte[]密文輸出
private static byte[] getEncCode(byte[] byteS){
byte[] byteFina = null;
Cipher cipher;
try{
cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE,key);
byteFina = cipher.doFinal(byteS);
}catch(Exception e){
e.printStackTrace();
}finally{
cipher = null;
}
return byteFina;
}
//考試大解密以byte[]密文輸入,以byte[]明文輸出
//@param byteD
//@return
private static byte[] getDesCode(byte[] byteD) throws Exception{
Cipher cipher;
byte[] byteFina=null;
try{
cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE,key);
byteFina = cipher.doFinal(byteD);
}catch(Exception e){
throw e;
}finally{
cipher=null;
}
return byteFina;
}
//加密字元串,返回String的密文
public static String getCryptograph(String str){
getKey("MYKEY11ERDCDVEIOKPUE");//生成密匙
String strEnc = getEncString(str);
return strEnc;
}
//把String 類型的密文解密
public static String getDisplay(String cryptograph) throws Exception{
String strEnc=null;
try {
getKey("MYKEY11ERDCDVEIOKPUE");
strEnc = getDesString(cryptograph);
} catch (Exception e) {
throw e;
}
return strEnc;
}
public static void main(String[] args){
try {
while(true){
Scanner san=new Scanner(System.in);
String str=san.nextLine();
getKey("MYKEY11ERDCDVEIOKPUE");//生成密匙
String strEnc = getEncString(str);
System.out.println(strEnc);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}