android md5加密怎么用

android md5加密怎么用,第1张


import javasecurityMessageDigest;
public class MD5Tool {
public static String MD5(String str){ 
MessageDigest md5 = null; 
try{ 
md5 = MessageDigestgetInstance("MD5"); 
}catch(Exception e){ 
eprintStackTrace(); 
return ""; 

char[] charArray = strtoCharArray(); 
byte[] byteArray = new byte[charArraylength]; 
for(int i = 0; i < charArraylength; i++){ 
byteArray[i] = (byte)charArray[i]; 

byte[] md5Bytes = md5digest(byteArray); 
StringBuffer hexValue = new StringBuffer(); 
for( int i = 0; i < md5Byteslength; i++) 

int val = ((int)md5Bytes[i])&0xff; 
if(val < 16) 

hexValueappend("0"); A

hexValueappend(IntegertoHexString(val)); 

return hexValuetoString(); 
}
public static String encryptmd5(String str) { 
char[] a = strtoCharArray(); 
for (int i = 0; i < alength; i++) 

a[i] = (char) (a[i] ^ 'l'); 

String s = new String(a); 
return s; 

}

在要加密的地方,调用这个类的MD5方法就可以加密了,解密就调用这个类的encryptmd5方法,不过好像解密方法不完全正确,毕竟是解密,不可能对复杂字符加密后的解密完全正确。不过加密算法是完全没有问题的。

des对称加密,是一种比较传统的加密方式,其加密运算、解密运算使用的是同样的密钥,信息的发送者和信息的接收者在进行信息的传输与处理时,必须共同持有该密码(称为对称密码),是一种对称加密算法。

DES全称Data Encryption Standard,是一种使用密匙加密的块算法。现在认为是一种不安全的加密算法,因为现在已经有用穷举法攻破DES密码的报道了。尽管如此,该加密算法还是运用非常普遍,是一种标准的加密算法。3DES是DES的加强版本。

DES 使用一个 56 位的密钥以及附加的 8 位奇偶校验位,产生最大 64 位的分组大小。这是一个迭代的分组密码,使用称为 Feistel 的技术,其中将加密的文本块分成两半。使用子密钥对其中一半应用循环功能,然后将输出与另一半进行“异或”运算;接着交换这两半,这一过程会继续下去,但最后一个循环不交换。DES 使用 16 个循环,使用异或,置换,代换,移位 *** 作四种基本运算。

有这样2个类:
KeyGenerator:此类提供(对称)密钥生成器的功能。
Cipher:此类为加密和解密提供密码功能。
在加密和解密过程中Cipher会使用到KeyGenerator生成的key进行加密(生成密文)和解密(解析密文)
[java] view plaincopyprint
01public class Main {
02 public static void main(String[] args) throws NoSuchAlgorithmException, UnsupportedEncodingException {
03 //-------------------加密过程---------------------------------
04 //产生一个key,需要关联一种“DES”算法
05 KeyGenerator keyGenerator = KeyGeneratorgetInstance("DES");
06 SecretKey deskey = keyGeneratorgenerateKey();
07 //需要加密的info
08 String info = "12345678";
09 //输出加密前的密文内容
10 Systemoutprintln(""+info);
11 //产生一个Random
12 SecureRandom sr = new SecureRandom();
13 byte[] cipherByteEncrypt = null;
14 try {
15 Cipher c1 = CiphergetInstance("DES");
16 c1init(CipherENCRYPT_MODE, deskey, sr);
17 //生成密文
18 cipherByteEncrypt = c1doFinal(infogetBytes());
19 } catch (Exception e) {
20 eprintStackTrace();
21 }
22 //输出加密后的密文内容
23 Systemoutprintln(""+new String(cipherByteEncrypt,"ISO-8859-1"));
24
25
26 //-------------------解密过程-----------------------------------
27 //产生一个Random
28 sr = new SecureRandom();
29 byte[] cipherByteDecrypt = null;
30 try {
31 Cipher c1 = CiphergetInstance("DES");
32 c1init(CipherDECRYPT_MODE, deskey, sr);
33 //解析密文
34 cipherByteDecrypt = c1doFinal(cipherByteEncrypt);
35 } catch (Exception e) {
36 eprintStackTrace();
37 }
38 Systemoutprintln(""+new String(cipherByteDecrypt,"ISO-8859-1"));
39
40 }
41
42}
public class Main {
public static void main(String[] args) throws NoSuchAlgorithmException, UnsupportedEncodingException {
//-------------------加密过程---------------------------------
//产生一个key,需要关联一种“DES”算法
KeyGenerator keyGenerator = KeyGeneratorgetInstance("DES");
SecretKey deskey = keyGeneratorgenerateKey();
//需要加密的info
String info = "12345678";
//输出加密前的密文内容
Systemoutprintln(""+info);
//产生一个Random
SecureRandom sr = new SecureRandom();
byte[] cipherByteEncrypt = null;
try {
Cipher c1 = CiphergetInstance("DES");
c1init(CipherENCRYPT_MODE, deskey, sr);
//生成密文
cipherByteEncrypt = c1doFinal(infogetBytes());
} catch (Exception e) {
eprintStackTrace();
}
//输出加密后的密文内容
Systemoutprintln(""+new String(cipherByteEncrypt,"ISO-8859-1"));
//-------------------解密过程-----------------------------------
//产生一个Random
sr = new SecureRandom();
byte[] cipherByteDecrypt = null;
try {
Cipher c1 = CiphergetInstance("DES");
c1init(CipherDECRYPT_MODE, deskey, sr);
//解析密文
cipherByteDecrypt = c1doFinal(cipherByteEncrypt);
} catch (Exception e) {
eprintStackTrace();
}
Systemoutprintln(""+new String(cipherByteDecrypt,"ISO-8859-1"));
}
}输出:
12345678
3M±@:;+j ---------------->这是密文
12345678


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/12651707.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-27
下一篇 2023-05-27

发表评论

登录后才能评论

评论列表(0条)

保存