導航:首頁 > 編程語言 > java密碼md5加密

java密碼md5加密

發布時間:2024-10-19 10:26:32

『壹』 java的md5的加密演算法代碼

import java.lang.reflect.*;

/*******************************************************************************
* keyBean 類實現了RSA Data Security, Inc.在提交給IETF 的RFC1321中的keyBean message-digest
* 演算法。
******************************************************************************/
public class keyBean {
/*
* 下面這些S11-S44實際上是一個4*4的矩陣,在原始的C實現中是用#define 實現的, 這里把它們實現成為static
* final是表示了只讀,切能在同一個進程空間內的多個 Instance間共享
*/
static final int S11 = 7;

static final int S12 = 12;

static final int S13 = 17;

static final int S14 = 22;

static final int S21 = 5;

static final int S22 = 9;

static final int S23 = 14;

static final int S24 = 20;

static final int S31 = 4;

static final int S32 = 11;

static final int S33 = 16;

static final int S34 = 23;

static final int S41 = 6;

static final int S42 = 10;

static final int S43 = 15;

static final int S44 = 21;

static final byte[] PADDING = { -128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0 };

/*
* 下面的三個成員是keyBean計算過程中用到的3個核心數據,在原始的C實現中 被定義到keyBean_CTX結構中
*/
private long[] state = new long[4]; // state (ABCD)

private long[] count = new long[2]; // number of bits, molo 2^64 (lsb

// first)

private byte[] buffer = new byte[64]; // input buffer

/*
* digestHexStr是keyBean的唯一一個公共成員,是最新一次計算結果的 16進制ASCII表示.
*/

public String digestHexStr;

/*
* digest,是最新一次計算結果的2進制內部表示,表示128bit的keyBean值.
*/
private byte[] digest = new byte[16];

/*
* getkeyBeanofStr是類keyBean最主要的公共方法,入口參數是你想要進行keyBean變換的字元串
* 返回的是變換完的結果,這個結果是從公共成員digestHexStr取得的.
*/
public String getkeyBeanofStr(String inbuf) {
keyBeanInit();
keyBeanUpdate(inbuf.getBytes(), inbuf.length());
keyBeanFinal();
digestHexStr = "";
for (int i = 0; i < 16; i++) {
digestHexStr += byteHEX(digest[i]);
}
return digestHexStr;
}

// 這是keyBean這個類的標准構造函數,JavaBean要求有一個public的並且沒有參數的構造函數
public keyBean() {
keyBeanInit();
return;
}

/* keyBeanInit是一個初始化函數,初始化核心變數,裝入標準的幻數 */
private void keyBeanInit() {
count[0] = 0L;
count[1] = 0L;
// /* Load magic initialization constants.
state[0] = 0x67452301L;
state[1] = 0xefcdab89L;
state[2] = 0x98badcfeL;
state[3] = 0x10325476L;
return;
}

/*
* F, G, H ,I 是4個基本的keyBean函數,在原始的keyBean的C實現中,由於它們是
* 簡單的位運算,可能出於效率的考慮把它們實現成了宏,在java中,我們把它們 實現成了private方法,名字保持了原來C中的。
*/
private long F(long x, long y, long z) {
return (x & y) | ((~x) & z);
}

private long G(long x, long y, long z) {
return (x & z) | (y & (~z));
}

private long H(long x, long y, long z) {
return x ^ y ^ z;
}

private long I(long x, long y, long z) {
return y ^ (x | (~z));
}

/*
* FF,GG,HH和II將調用F,G,H,I進行近一步變換 FF, GG, HH, and II transformations for
* rounds 1, 2, 3, and 4. Rotation is separate from addition to prevent
* recomputation.
*/
private long FF(long a, long b, long c, long d, long x, long s, long ac) {
a += F(b, c, d) + x + ac;
a = ((int) a << s) | ((int) a >>> (32 - s));
a += b;
return a;
}

private long GG(long a, long b, long c, long d, long x, long s, long ac) {
a += G(b, c, d) + x + ac;
a = ((int) a << s) | ((int) a >>> (32 - s));
a += b;
return a;
}

private long HH(long a, long b, long c, long d, long x, long s, long ac) {
a += H(b, c, d) + x + ac;
a = ((int) a << s) | ((int) a >>> (32 - s));
a += b;
return a;
}

private long II(long a, long b, long c, long d, long x, long s, long ac) {
a += I(b, c, d) + x + ac;
a = ((int) a << s) | ((int) a >>> (32 - s));
a += b;
return a;
}

/*
* keyBeanUpdate是keyBean的主計算過程,inbuf是要變換的位元組串,inputlen是長度,這個
* 函數由getkeyBeanofStr調用,調用之前需要調用keyBeaninit,因此把它設計成private的
*/
private void keyBeanUpdate(byte[] inbuf, int inputLen) {
int i, index, partLen;
byte[] block = new byte[64];
index = (int) (count[0] >>> 3) & 0x3F;
// /* Update number of bits */
if ((count[0] += (inputLen << 3)) < (inputLen << 3))
count[1]++;
count[1] += (inputLen >>> 29);
partLen = 64 - index;
// Transform as many times as possible.
if (inputLen >= partLen) {
keyBeanMemcpy(buffer, inbuf, index, 0, partLen);
keyBeanTransform(buffer);
for (i = partLen; i + 63 < inputLen; i += 64) {
keyBeanMemcpy(block, inbuf, 0, i, 64);
keyBeanTransform(block);
}
index = 0;
} else
i = 0;
// /* Buffer remaining input */
keyBeanMemcpy(buffer, inbuf, index, i, inputLen - i);
}

/*
* keyBeanFinal整理和填寫輸出結果
*/
private void keyBeanFinal() {
byte[] bits = new byte[8];
int index, padLen;
// /* Save number of bits */
Encode(bits, count, 8);
// /* Pad out to 56 mod 64.
index = (int) (count[0] >>> 3) & 0x3f;
padLen = (index < 56) ? (56 - index) : (120 - index);
keyBeanUpdate(PADDING, padLen);
// /* Append length (before padding) */
keyBeanUpdate(bits, 8);
// /* Store state in digest */
Encode(digest, state, 16);
}

/*
* keyBeanMemcpy是一個內部使用的byte數組的塊拷貝函數,從input的inpos開始把len長度的
* 位元組拷貝到output的outpos位置開始
*/
private void keyBeanMemcpy(byte[] output, byte[] input, int outpos,
int inpos, int len) {
int i;
for (i = 0; i < len; i++)
output[outpos + i] = input[inpos + i];
}

/*
* keyBeanTransform是keyBean核心變換程序,有keyBeanUpdate調用,block是分塊的原始位元組
*/
private void keyBeanTransform(byte block[]) {
long a = state[0], b = state[1], c = state[2], d = state[3];
long[] x = new long[16];
Decode(x, block, 64);
/* Round 1 */
a = FF(a, b, c, d, x[0], S11, 0xd76aa478L); /* 1 */
d = FF(d, a, b, c, x[1], S12, 0xe8c7b756L); /* 2 */
c = FF(c, d, a, b, x[2], S13, 0x242070dbL); /* 3 */
b = FF(b, c, d, a, x[3], S14, 0xc1bdceeeL); /* 4 */
a = FF(a, b, c, d, x[4], S11, 0xf57c0fafL); /* 5 */
d = FF(d, a, b, c, x[5], S12, 0x4787c62aL); /* 6 */
c = FF(c, d, a, b, x[6], S13, 0xa8304613L); /* 7 */
b = FF(b, c, d, a, x[7], S14, 0xfd469501L); /* 8 */
a = FF(a, b, c, d, x[8], S11, 0x698098d8L); /* 9 */
d = FF(d, a, b, c, x[9], S12, 0x8b44f7afL); /* 10 */
c = FF(c, d, a, b, x[10], S13, 0xffff5bb1L); /* 11 */
b = FF(b, c, d, a, x[11], S14, 0x895cd7beL); /* 12 */
a = FF(a, b, c, d, x[12], S11, 0x6b901122L); /* 13 */
d = FF(d, a, b, c, x[13], S12, 0xfd987193L); /* 14 */
c = FF(c, d, a, b, x[14], S13, 0xa679438eL); /* 15 */
b = FF(b, c, d, a, x[15], S14, 0x49b40821L); /* 16 */
/* Round 2 */
a = GG(a, b, c, d, x[1], S21, 0xf61e2562L); /* 17 */
d = GG(d, a, b, c, x[6], S22, 0xc040b340L); /* 18 */
c = GG(c, d, a, b, x[11], S23, 0x265e5a51L); /* 19 */
b = GG(b, c, d, a, x[0], S24, 0xe9b6c7aaL); /* 20 */
a = GG(a, b, c, d, x[5], S21, 0xd62f105dL); /* 21 */
d = GG(d, a, b, c, x[10], S22, 0x2441453L); /* 22 */
c = GG(c, d, a, b, x[15], S23, 0xd8a1e681L); /* 23 */
b = GG(b, c, d, a, x[4], S24, 0xe7d3fbc8L); /* 24 */
a = GG(a, b, c, d, x[9], S21, 0x21e1cde6L); /* 25 */
d = GG(d, a, b, c, x[14], S22, 0xc33707d6L); /* 26 */
c = GG(c, d, a, b, x[3], S23, 0xf4d50d87L); /* 27 */
b = GG(b, c, d, a, x[8], S24, 0x455a14edL); /* 28 */
a = GG(a, b, c, d, x[13], S21, 0xa9e3e905L); /* 29 */
d = GG(d, a, b, c, x[2], S22, 0xfcefa3f8L); /* 30 */
c = GG(c, d, a, b, x[7], S23, 0x676f02d9L); /* 31 */
b = GG(b, c, d, a, x[12], S24, 0x8d2a4c8aL); /* 32 */
/* Round 3 */
a = HH(a, b, c, d, x[5], S31, 0xfffa3942L); /* 33 */
d = HH(d, a, b, c, x[8], S32, 0x8771f681L); /* 34 */
c = HH(c, d, a, b, x[11], S33, 0x6d9d6122L); /* 35 */
b = HH(b, c, d, a, x[14], S34, 0xfde5380cL); /* 36 */
a = HH(a, b, c, d, x[1], S31, 0xa4beea44L); /* 37 */
d = HH(d, a, b, c, x[4], S32, 0x4bdecfa9L); /* 38 */
c = HH(c, d, a, b, x[7], S33, 0xf6bb4b60L); /* 39 */
b = HH(b, c, d, a, x[10], S34, 0xbebfbc70L); /* 40 */
a = HH(a, b, c, d, x[13], S31, 0x289b7ec6L); /* 41 */
d = HH(d, a, b, c, x[0], S32, 0xeaa127faL); /* 42 */
c = HH(c, d, a, b, x[3], S33, 0xd4ef3085L); /* 43 */
b = HH(b, c, d, a, x[6], S34, 0x4881d05L); /* 44 */
a = HH(a, b, c, d, x[9], S31, 0xd9d4d039L); /* 45 */
d = HH(d, a, b, c, x[12], S32, 0xe6db99e5L); /* 46 */
c = HH(c, d, a, b, x[15], S33, 0x1fa27cf8L); /* 47 */
b = HH(b, c, d, a, x[2], S34, 0xc4ac5665L); /* 48 */
/* Round 4 */
a = II(a, b, c, d, x[0], S41, 0xf4292244L); /* 49 */
d = II(d, a, b, c, x[7], S42, 0x432aff97L); /* 50 */
c = II(c, d, a, b, x[14], S43, 0xab9423a7L); /* 51 */
b = II(b, c, d, a, x[5], S44, 0xfc93a039L); /* 52 */
a = II(a, b, c, d, x[12], S41, 0x655b59c3L); /* 53 */
d = II(d, a, b, c, x[3], S42, 0x8f0ccc92L); /* 54 */
c = II(c, d, a, b, x[10], S43, 0xffeff47dL); /* 55 */
b = II(b, c, d, a, x[1], S44, 0x85845dd1L); /* 56 */
a = II(a, b, c, d, x[8], S41, 0x6fa87e4fL); /* 57 */
d = II(d, a, b, c, x[15], S42, 0xfe2ce6e0L); /* 58 */
c = II(c, d, a, b, x[6], S43, 0xa3014314L); /* 59 */
b = II(b, c, d, a, x[13], S44, 0x4e0811a1L); /* 60 */
a = II(a, b, c, d, x[4], S41, 0xf7537e82L); /* 61 */
d = II(d, a, b, c, x[11], S42, 0xbd3af235L); /* 62 */
c = II(c, d, a, b, x[2], S43, 0x2ad7d2bbL); /* 63 */
b = II(b, c, d, a, x[9], S44, 0xeb86d391L); /* 64 */
state[0] += a;
state[1] += b;
state[2] += c;
state[3] += d;
}

/*
* Encode把long數組按順序拆成byte數組,因為java的long類型是64bit的, 只拆低32bit,以適應原始C實現的用途
*/
private void Encode(byte[] output, long[] input, int len) {
int i, j;
for (i = 0, j = 0; j < len; i++, j += 4) {
output[j] = (byte) (input[i] & 0xffL);
output[j + 1] = (byte) ((input[i] >>> 8) & 0xffL);
output[j + 2] = (byte) ((input[i] >>> 16) & 0xffL);
output[j + 3] = (byte) ((input[i] >>> 24) & 0xffL);
}
}

/*
* Decode把byte數組按順序合成成long數組,因為java的long類型是64bit的,
* 只合成低32bit,高32bit清零,以適應原始C實現的用途
*/
private void Decode(long[] output, byte[] input, int len) {
int i, j;

for (i = 0, j = 0; j < len; i++, j += 4)
output[i] = b2iu(input[j]) | (b2iu(input[j + 1]) << 8)
| (b2iu(input[j + 2]) << 16) | (b2iu(input[j + 3]) << 24);
return;
}

/*
* b2iu是我寫的一個把byte按照不考慮正負號的原則的」升位」程序,因為java沒有unsigned運算
*/
public static long b2iu(byte b) {
return b < 0 ? b & 0x7F + 128 : b;
}

/*
* byteHEX(),用來把一個byte類型的數轉換成十六進制的ASCII表示,
* 因為java中的byte的toString無法實現這一點,我們又沒有C語言中的 sprintf(outbuf,"%02X",ib)
*/
public static String byteHEX(byte ib) {
char[] Digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A',
'B', 'C', 'D', 'E', 'F' };
char[] ob = new char[2];
ob[0] = Digit[(ib >>> 4) & 0X0F];
ob[1] = Digit[ib & 0X0F];
String s = new String(ob);
return s;
}

public static void main(String args[]) {

keyBean m = new keyBean();
if (Array.getLength(args) == 0) { // 如果沒有參數,執行標準的Test Suite
System.out.println("keyBean Test suite:");
System.out.println("keyBean(\"):" + m.getkeyBeanofStr(""));
System.out.println("keyBean(\"a\"):" + m.getkeyBeanofStr("a"));
System.out.println("keyBean(\"abc\"):" + m.getkeyBeanofStr("abc"));
System.out.println("keyBean(\"message digest\"):"
+ m.getkeyBeanofStr("message digest"));
System.out.println("keyBean(\"abcdefghijklmnopqrstuvwxyz\"):"
+ m.getkeyBeanofStr("abcdefghijklmnopqrstuvwxyz"));
System.out
.println("keyBean(\"\"):"
+ m
.getkeyBeanofStr(""));
} else
System.out.println("keyBean(" + args[0] + ")="
+ m.getkeyBeanofStr(args[0]));

}
}

『貳』 java的MD5withRSA演算法可以看到解密的內容么

您好,
<一>. MD5加密演算法:
? ? ? ?消息摘要演算法第五版(Message Digest Algorithm),是一種單向加密演算法,只能加密、無法解密。然而MD5加密演算法已經被中國山東大學王小雲教授成功破譯,但是在安全性要求不高的場景下,MD5加密演算法仍然具有應用價值。
?1. 創建md5對象:?
<pre name="code" class="java">MessageDigest md5 = MessageDigest.getInstance("md5");
?2. ?進行加密操作:歲宏?
byte[] cipherData = md5.digest(plainText.getBytes());

?3. ?將其中的每個位元組轉成十六進制字元串:byte類型的數據最高位是符號位,通過和0xff進行與操作,轉換為int類型的正整數。?
String toHexStr = Integer.toHexString(cipher & 0xff);

?4. 如果該正數小於16(長度為1個字元),前面拼接0佔位:確保最後生成的是32位字元串。?
builder.append(toHexStr.length() == 1 ? "0" + toHexStr : toHexStr);

?5.?加密轉換之後的字元串為:?
?6. 完整的MD5演算法應用如下所示:?
/**
* 功能簡述: 測試MD5單向加密.
* @throws Exception
*/
@Test
public void test01() throws Exception {
String plainText = "Hello , world !"乎派冊;
MessageDigest md5 = MessageDigest.getInstance("md5");
byte[] cipherData = md5.digest(plainText.getBytes());
StringBuilder builder = new StringBuilder();
for(byte cipher : cipherData) {
String toHexStr = Integer.toHexString(cipher & 0xff);
builder.append(toHexStr.length() == 1 ? "0" + toHexStr : toHexStr);
}
System.out.println(builder.toString());
//
}

??
<二>. 使用BASE64進行加密/解密:
? ? ? ? 使用BASE64演算法通常用作對二進制數據進行加密,加密之後的數據不易被肉眼識別。嚴格來說,經過BASE64加密的數據其實沒有安全性可言,因為它的加密解密演算法都是公開的,典型的防菜鳥不防程序羨逗猿的呀。?經過標準的BASE64演算法加密後的數據,?通常包含/、+、=等特殊符號,不適合作為url參數傳遞,幸運的是Apache的Commons Codec模塊提供了對BASE64的進一步封裝。? (參見最後一部分的說明)
?1.?使用BASE64加密:?
BASE64Encoder encoder = new BASE64Encoder();
String cipherText = encoder.encode(plainText.getBytes());

? 2.?使用BASE64解密:?
BASE64Decoder decoder = new BASE64Decoder();
plainText = new String(decoder.decodeBuffer(cipherText));

? 3. 完整代碼示例:?
/**
* 功能簡述: 使用BASE64進行雙向加密/解密.
* @throws Exception
*/
@Test
public void test02() throws Exception {
BASE64Encoder encoder = new BASE64Encoder();
BASE64Decoder decoder = new BASE64Decoder();
String plainText = "Hello , world !";
String cipherText = encoder.encode(plainText.getBytes());
System.out.println("cipherText : " + cipherText);
//cipherText : SGVsbG8gLCB3b3JsZCAh
System.out.println("plainText : " +
new String(decoder.decodeBuffer(cipherText)));
//plainText : Hello , world !
}

??
<三>. 使用DES對稱加密/解密:
? ? ? ? ?數據加密標准演算法(Data Encryption Standard),和BASE64最明顯的區別就是有一個工作密鑰,該密鑰既用於加密、也用於解密,並且要求密鑰是一個長度至少大於8位的字元串。使用DES加密、解密的核心是確保工作密鑰的安全性。
?1.?根據key生成密鑰:?
DESKeySpec keySpec = new DESKeySpec(key.getBytes());
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("des");
SecretKey secretKey = keyFactory.generateSecret(keySpec);

? 2.?加密操作:?
Cipher cipher = Cipher.getInstance("des");
cipher.init(Cipher.ENCRYPT_MODE, secretKey, new SecureRandom());
byte[] cipherData = cipher.doFinal(plainText.getBytes());

? 3.?為了便於觀察生成的加密數據,使用BASE64再次加密:?
String cipherText = new BASE64Encoder().encode(cipherData);

? ? ?生成密文如下:PtRYi3sp7TOR69UrKEIicA==?
? 4.?解密操作:?
cipher.init(Cipher.DECRYPT_MODE, secretKey, new SecureRandom());
byte[] plainData = cipher.doFinal(cipherData);
String plainText = new String(plainData);

? 5. 完整的代碼demo:?
/**
* 功能簡述: 使用DES對稱加密/解密.
* @throws Exception
*/
@Test
public void test03() throws Exception {
String plainText = "Hello , world !";
String key = "12345678"; //要求key至少長度為8個字元

SecureRandom random = new SecureRandom();
DESKeySpec keySpec = new DESKeySpec(key.getBytes());
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("des");
SecretKey secretKey = keyFactory.generateSecret(keySpec);

Cipher cipher = Cipher.getInstance("des");
cipher.init(Cipher.ENCRYPT_MODE, secretKey, random);
byte[] cipherData = cipher.doFinal(plainText.getBytes());
System.out.println("cipherText : " + new BASE64Encoder().encode(cipherData));
//PtRYi3sp7TOR69UrKEIicA==

cipher.init(Cipher.DECRYPT_MODE, secretKey, random);
byte[] plainData = cipher.doFinal(cipherData);
System.out.println("plainText : " + new String(plainData));
//Hello , world !
}

??
<四>. 使用RSA非對稱加密/解密:
? ? ? ? RSA演算法是非對稱加密演算法的典型代表,既能加密、又能解密。和對稱加密演算法比如DES的明顯區別在於用於加密、解密的密鑰是不同的。使用RSA演算法,只要密鑰足夠長(一般要求1024bit),加密的信息是不能被破解的。用戶通過https協議訪問伺服器時,就是使用非對稱加密演算法進行數據的加密、解密操作的。
? ? ? ?伺服器發送數據給客戶端時使用私鑰(private key)進行加密,並且使用加密之後的數據和私鑰生成數字簽名(digital signature)並發送給客戶端。客戶端接收到伺服器發送的數據會使用公鑰(public key)對數據來進行解密,並且根據加密數據和公鑰驗證數字簽名的有效性,防止加密數據在傳輸過程中被第三方進行了修改。
? ? ? ?客戶端發送數據給伺服器時使用公鑰進行加密,伺服器接收到加密數據之後使用私鑰進行解密。
?1.?創建密鑰對KeyPair:
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("rsa");
keyPairGenerator.initialize(1024); //密鑰長度推薦為1024位.
KeyPair keyPair = keyPairGenerator.generateKeyPair();

? 2.?獲取公鑰/私鑰:
PublicKey publicKey = keyPair.getPublic();
PrivateKey privateKey = keyPair.getPrivate();

? 3.?伺服器數據使用私鑰加密:
Cipher cipher = Cipher.getInstance("rsa");
cipher.init(Cipher.ENCRYPT_MODE, privateKey, new SecureRandom());
byte[] cipherData = cipher.doFinal(plainText.getBytes());

? 4.?用戶使用公鑰解密:
cipher.init(Cipher.DECRYPT_MODE, publicKey, new SecureRandom());
byte[] plainData = cipher.doFinal(cipherData);

? 5.?伺服器根據私鑰和加密數據生成數字簽名:
Signature signature = Signature.getInstance("MD5withRSA");
signature.initSign(privateKey);
signature.update(cipherData);
byte[] signData = signature.sign();

? 6.?用戶根據公鑰、加密數據驗證數據是否被修改過:
signature.initVerify(publicKey);
signature.update(cipherData);
boolean status = signature.verify(signData);

? 7. RSA演算法代碼demo:<img src="http://www.cxyclub.cn/Upload/Images/2014081321/99A5FC9C0C628374.gif" alt="尷尬" title="尷尬" border="0">
/**
* 功能簡述: 使用RSA非對稱加密/解密.
* @throws Exception
*/
@Test
public void test04() throws Exception {
String plainText = "Hello , world !";

KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("rsa");
keyPairGenerator.initialize(1024);
KeyPair keyPair = keyPairGenerator.generateKeyPair();

PublicKey publicKey = keyPair.getPublic();
PrivateKey privateKey = keyPair.getPrivate();

Cipher cipher = Cipher.getInstance("rsa");
SecureRandom random = new SecureRandom();

cipher.init(Cipher.ENCRYPT_MODE, privateKey, random);
byte[] cipherData = cipher.doFinal(plainText.getBytes());
System.out.println("cipherText : " + new BASE64Encoder().encode(cipherData));
//gDsJxZM98U2GzHUtUTyZ/Ir/
///ONFOD0fnJoGtIk+T/+3yybVL8M+RI+HzbE/jdYa/+
//yQ+vHwHqXhuzZ/N8iNg=

cipher.init(Cipher.DECRYPT_MODE, publicKey, random);
byte[] plainData = cipher.doFinal(cipherData);
System.out.println("plainText : " + new String(plainData));
//Hello , world !

Signature signature = Signature.getInstance("MD5withRSA");
signature.initSign(privateKey);
signature.update(cipherData);
byte[] signData = signature.sign();
System.out.println("signature : " + new BASE64Encoder().encode(signData));
//+
//co64p6Sq3kVt84wnRsQw5mucZnY+/+vKKXZ3pbJMNT/4
///t9ewo+KYCWKOgvu5QQ=

signature.initVerify(publicKey);
signature.update(cipherData);
boolean status = signature.verify(signData);
System.out.println("status : " + status);
//true
}

『叄』 java中md5加密

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class md5 {
public String str;

public void md5s(String plainText) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(plainText.getBytes());
byte b[] = md.digest();

int i;

StringBuffer buf = new StringBuffer("");
for (int offset = 0; offset < b.length; offset++) {
i = b[offset];
if (i < 0)
i += 256;
if (i < 16)
buf.append("0");
buf.append(Integer.toHexString(i));
}
str = buf.toString();
System.out.println("result: " + buf.toString());// 32位的加密
System.out.println("result: " + buf.toString().substring(8, 24));// 16位的加密
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();

}
}

public static void main(String agrs[]) {
md5 md51 = new md5();
md51.md5s("4");//加密4
}

}

『肆』 Java MD5和SHA256等常用加密演算法

在Java項目開發中,數據安全是至關重要的。特別是在前後端介面交互時,為了保護信息的完整性和安全性,我們需要對介面簽名、用戶登錄密碼等進行加密處理。加密演算法作為基礎技術,在身份驗證、單點登錄、信息通信和支付交易等多個場景中扮演著關鍵角色。

MD5,全稱信息摘要演算法,是一種常見的128位(16位元組)散列函數。它通過復雜的演算法操作,將明文轉化為無法還原的密文,確保信息傳輸的一致性。盡管MD5常用於密碼的存儲,但需注意,由於其本質上是摘要而非加密,生成的128位字元串是單向的,無法逆向獲取原始信息。在找回密碼時,我們只能通過對比用戶輸入的MD5值來驗證,而無法獲取原密碼。

SHA系列,如SHA-1,盡管有碰撞的潛在風險,但其安全性相對較高,適用於對信息安全要求較高的場景。HMAC(Hash-based Message Authentication Code)是基於哈希函數的認證碼,推薦使用SHA256、SHA384、SHA512以及它們的HMAC變種,如HMAC-SHA256等,以提供更高級別的加密和認證功能。

對於實際應用中的對稱加密演算法,如常見的加密鹽,它可以增強密碼的安全性,防止暴力破解。至於在線加密網站,選擇適合項目的加密演算法至關重要。在眾多演算法中,SHA256、SHA384和SHA512因其較高的安全性,以及HMAC-SHA變種的認證能力,被廣泛認為是更推薦的選擇。

『伍』 求Java的MD5加密解密實現類。 要實現對用戶的密碼進行加密! 然後驗證用戶的密碼!

我簡單說下吧,加密就是存進資料庫的時候變成MD5存進去,解密,就是對比的時候,將用戶輸入的密碼轉換成MD5和資料庫裡面的對比。

『陸』 java md5 16位和32位的區別

32位比16位更安全。
MD5加密演算法是一種可加密不可解密(單向)的加密演算法,一般用來比較兩個字元串是否相同。
因為之前16位的加密演算法被武漢某大學教授破解了,所以官方推出了32位加密演算法。
這里的位,與MD5算出來之後的位數沒關系。

『柒』 java的32位MD5加密與php中的32位MD5加密結果不一樣。求幫助。急急急

Java的字元串是unicode編碼,不受源碼文件的編碼影響;而PHP的編碼是和源碼文件的編碼一致,受源碼編碼影響。例中java字元數據在散列時的編碼和php編碼未能保持一致,我認為這是導致輸出不同的「病理」

由於未知mad.toMd5的具體實現,問題直接成因應該在toMd5的String到byte[]轉換時的編碼上,或者未設置或者設置了錯誤的編碼
但只要java的字元串先getBytes獲得位元組串,並和php源碼編碼一致,就能獲得一致結果。

閱讀全文

與java密碼md5加密相關的資料

熱點內容
塘管家源碼 瀏覽:698
台劇無邪什麼app可以看 瀏覽:586
筆記本配置伺服器怎麼連接 瀏覽:368
android代理上網設置 瀏覽:658
u盤加密不能顯示盤符 瀏覽:665
去伺服器玩什麼游戲 瀏覽:134
哪個同步盤支持多個文件夾 瀏覽:886
蘋果版的我的世界如何轉到安卓 瀏覽:274
linuxswap空間 瀏覽:409
如何搭建網路到各個伺服器 瀏覽:966
oa系統crm源碼 瀏覽:584
安卓生態為什麼一直很爛 瀏覽:147
數字加密傳輸流程 瀏覽:733
義隆8p單片機怎麼樣 瀏覽:271
什麼app能看到入團時間 瀏覽:898
建築設計資料集第三版pdf 瀏覽:150
怎麼知道郵箱伺服器是什麼 瀏覽:381
程序員有什麼可以玩的 瀏覽:601
當伺服器造轟炸機會發生什麼 瀏覽:94
什麼是mc伺服器ip地址 瀏覽:436