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