古詩詞大全網 - 古詩大全 - java的MD5withRSA算法可以看到解密的內容麽?

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.?加密轉換之後的字符串為:c0bb4f54f1d8b14caf6fe1069e5f93ad?

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

//c0bb4f54f1d8b14caf6fe1069e5f93ad

}

<二>. 使用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),加密的信息是不能被破解的。用戶通過/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/NXqRWKUJkl6olrLYCZHY3RnlF3olkWPZ35Dwz9BMRqaTL3oPuyVq

//sehvHExxj9RyrWpIYnYLBSURB1KVUSLMsd/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));

//ADfoeKQn6eEHgLF8ETMXan3TfFO03R5u+cQEWtAQ2lRblLZw1DpzTlJJt1RXjU451I84v3297LhR

//co64p6Sq3kVt84wnRsQw5mucZnY+jRZNdXpcbwh2qsh8287NM2hxWqp4OOCf/+vKKXZ3pbJMNT/4

///t9ewo+KYCWKOgvu5QQ=

signature.initVerify(publicKey);

signature.update(cipherData);

boolean status = signature.verify(signData);

System.out.println("status : " + status);

//true

}