2017-02-23 163 views
-1

請幫我加密全字符串「測試加密字符串」。 這裏只對16個字節進行加密。 以下是我用於md5加密和解密的代碼。 請幫我加密更多的字節。md5加密和解密java無法加密超過16bytes

import java.security.MessageDigest; 
import java.util.Arrays; 
import javax.crypto.Cipher; 
import javax.crypto.SecretKey; 
import javax.crypto.spec.IvParameterSpec; 
import javax.crypto.spec.SecretKeySpec; 
public class Md5Encryption 
{ 
    private static final String ALGORITHM = "md5"; 
    private static final String DIGEST_STRING = "HG58YZ3CR9"; 
    private static final String CHARSET_UTF_8 = "utf-8"; 
    private static final String SECRET_KEY_ALGORITHM = "DESede"; 
    private static final String TRANSFORMATION_PADDING = "DESede/CBC/PKCS5Padding"; 

    /* Encryption Method */ 
    public String encrypt(String message) throws Exception 
    { 
     final MessageDigest md = MessageDigest.getInstance(ALGORITHM); 
     final byte[] digestOfPassword = md.digest(DIGEST_STRING.getBytes(CHARSET_UTF_8)); 
     final byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24); 
     for (int j = 0, k = 16; j < 8;) { 
       keyBytes[k++] = keyBytes[j++]; 
     } 

     final SecretKey key = new SecretKeySpec(keyBytes, SECRET_KEY_ALGORITHM); 
     final IvParameterSpec iv = new IvParameterSpec(new byte[8]); 
     final Cipher cipher = Cipher.getInstance(TRANSFORMATION_PADDING); 
     cipher.init(Cipher.ENCRYPT_MODE, key, iv); 

     final byte[] plainTextBytes = message.getBytes(CHARSET_UTF_8); 
     final byte[] cipherText = cipher.doFinal(plainTextBytes); 

     return new String(cipherText); 
    } 

    /* Decryption Method */ 
    public String decrypt(String message) throws Exception { 
     final MessageDigest md = MessageDigest.getInstance(ALGORITHM); 
     final byte[] digestOfPassword = md.digest(DIGEST_STRING.getBytes(CHARSET_UTF_8)); 
     final byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24); 
     for (int j = 0, k = 16; j < 8;) { 
       keyBytes[k++] = keyBytes[j++]; 
     } 

     final SecretKey key = new SecretKeySpec(keyBytes, SECRET_KEY_ALGORITHM); 
     final IvParameterSpec iv = new IvParameterSpec(new byte[8]); 
     final Cipher decipher = Cipher.getInstance(TRANSFORMATION_PADDING); 
     decipher.init(Cipher.DECRYPT_MODE, key, iv); 

     final byte[] plainText = decipher.update(message.getBytes()); 

     return new String(plainText, CHARSET_UTF_8); 
    } 

    public static void main(String[] args) throws Exception { 


     String text = "TEST STRING TO ENCRYPT"; 
     String codedtext = new Md5Encryption().encrypt(text); 
//  String codedtext = ".ªÉ…U$L§U`8ˉ­?¦」›°„"; 
     String decodedtext = new Md5Encryption().decrypt(codedtext); 

     System.out.println(codedtext); // this is a byte array, you'll just see a reference to an array 
     System.out.println(decodedtext); // This correctly shows "TEST STRING TO ENCRYPT" 
    } 


} 
+1

MD5不是加密算法 - 它是一個(不安全)單程哈希算法。 – chrixm

+2

MD5是一種哈希算法,不是加密,因此你也不能解密它。多年來它也被認爲是毫無意義的,因爲它太快地被打破。 –

+0

它只是供內部使用......所以沒問題 – vimal

回答

0

嗨,你是不是做MD5加密,你得到一個字符串的MD5摘要,然後用它來生成密鑰,做DESede/CBC加密你。請注意,MD5不是加密,而是散列算法。細節available at this Link

散列是一種方法。你不能從一個哈希碼轉換你的數據/字符串。加密是2種方式 - 如果您有密鑰,您可以再次解密加密的字符串。加密函數通過​​使用加密密鑰將文本轉換爲無意義的密文,反之亦然。

而不是做一個decipher.update(..)你需要一個doFinal(..),並且也代替轉換encrytped byte[]String,嘗試返回byte[]encrypt和傳遞byte[]解密作爲參數;它意味着使用的方式。下面應該解決你的問題:

import java.security.MessageDigest; 
import java.util.Arrays; 
import javax.crypto.Cipher; 
import javax.crypto.SecretKey; 
import javax.crypto.spec.IvParameterSpec; 
import javax.crypto.spec.SecretKeySpec; 
public class Md5Encryption 
{ 
    private static final String ALGORITHM = "md5"; 
    private static final String DIGEST_STRING = "HG58YZ3CR9"; 
    private static final String CHARSET_UTF_8 = "utf-8"; 
    private static final String SECRET_KEY_ALGORITHM = "DESede"; 
    private static final String TRANSFORMATION_PADDING = "DESede/CBC/PKCS5Padding"; 

    /* Encryption Method */ 
    public byte[] encrypt(String message) throws Exception 
    { 
     final MessageDigest md = MessageDigest.getInstance(ALGORITHM); 
     final byte[] digestOfPassword = md.digest(DIGEST_STRING.getBytes(CHARSET_UTF_8)); 
     final byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24); 
     for (int j = 0, k = 16; j < 8;) { 
       keyBytes[k++] = keyBytes[j++]; 
     } 

     final SecretKey key = new SecretKeySpec(keyBytes, SECRET_KEY_ALGORITHM); 
     final IvParameterSpec iv = new IvParameterSpec(new byte[8]); 
     final Cipher cipher = Cipher.getInstance(TRANSFORMATION_PADDING); 
     cipher.init(Cipher.ENCRYPT_MODE, key, iv); 

     final byte[] plainTextBytes = message.getBytes(CHARSET_UTF_8); 
     final byte[] cipherText = cipher.doFinal(plainTextBytes); 

     return cipherText; 
    } 

    /* Decryption Method */ 
    public String decrypt(byte[] message) throws Exception { 
     final MessageDigest md = MessageDigest.getInstance(ALGORITHM); 
     final byte[] digestOfPassword = md.digest(DIGEST_STRING.getBytes(CHARSET_UTF_8)); 
     final byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24); 
     for (int j = 0, k = 16; j < 8;) { 
       keyBytes[k++] = keyBytes[j++]; 
     } 

     final SecretKey key = new SecretKeySpec(keyBytes, SECRET_KEY_ALGORITHM); 
     final IvParameterSpec iv = new IvParameterSpec(new byte[8]); 
     final Cipher decipher = Cipher.getInstance(TRANSFORMATION_PADDING); 
     decipher.init(Cipher.DECRYPT_MODE, key, iv); 

     final byte[] plainText = decipher.doFinal(message); 

     return new String(plainText, CHARSET_UTF_8); 
    } 

    public static void main(String[] args) throws Exception { 


     String text = "TEST STRING TO ENCRYPT"; 
     byte[] codedtext = new Md5Encryption().encrypt(text); 
//  String codedtext = ".ªÉ…U$L§U`8ˉ­?¦」›°„"; 
     String decodedtext = new Md5Encryption().decrypt(codedtext); 

     System.out.println(codedtext); // this is a byte array, you'll just see a reference to an array 
     System.out.println(decodedtext); // This correctly shows "TEST STRING TO ENCRYPT" 
    } 


} 
0

上面的代碼有兩個問題。 1.您應該使用doFinal解密而不是更新,因爲它只會解密部分數據。 2.您應該傳遞字節數組而不是將其轉換爲可能會導致差異的字符串,並且您可能會得到badpaddingexcpetion。 更正代碼是如下:

import java.security.MessageDigest; 
import java.util.Arrays; 

import javax.crypto.Cipher; 
import javax.crypto.SecretKey; 
import javax.crypto.spec.IvParameterSpec; 
import javax.crypto.spec.SecretKeySpec; 
import sun.misc.BASE64Decoder; 
import sun.misc.BASE64Encoder; 

public class Md5Encryption 
{ 
    private static final String ALGORITHM = "md5"; 
    private static final String DIGEST_STRING = "HG58YZ3CR9"; 
    private static final String CHARSET_UTF_8 = "utf-8"; 
    private static final String SECRET_KEY_ALGORITHM = "DESede"; 
    private static final String TRANSFORMATION_PADDING = "DESede/CBC/PKCS5Padding"; 

/* Encryption Method */ 
public byte[] encrypt(String message) throws Exception 
{ 
    final MessageDigest md = MessageDigest.getInstance(ALGORITHM); 
    final byte[] digestOfPassword = md.digest(DIGEST_STRING.getBytes(CHARSET_UTF_8)); 
    final byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24); 
    for (int j = 0, k = 16; j < 8;) { 
      keyBytes[k++] = keyBytes[j++]; 
    } 
    System.out.println(new String(keyBytes)); 
    final SecretKey key = new SecretKeySpec(keyBytes, SECRET_KEY_ALGORITHM); 
    final IvParameterSpec iv = new IvParameterSpec(new byte[8]); 
    final Cipher cipher = Cipher.getInstance(TRANSFORMATION_PADDING); 
    cipher.init(Cipher.ENCRYPT_MODE, key, iv); 

    final byte[] plainTextBytes = message.getBytes(CHARSET_UTF_8); 
    System.out.println(new String(plainTextBytes)); 
    final byte[] cipherText = cipher.doFinal(plainTextBytes); 

    //BASE64Encoder base64encoder = new BASE64Encoder(); 
    //return base64encoder.encode(cipherText); 
    return cipherText; 
} 



/* Decryption Method */ 
    public String decrypt(byte[] message) throws Exception { 
     final MessageDigest md = MessageDigest.getInstance(ALGORITHM); 
     final byte[] digestOfPassword = md.digest(DIGEST_STRING.getBytes(CHARSET_UTF_8)); 
     final byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24); 
     for (int j = 0, k = 16; j < 8;) { 
       keyBytes[k++] = keyBytes[j++]; 
     } 
     System.out.println(new String(keyBytes)); 
     final SecretKey key = new SecretKeySpec(keyBytes, SECRET_KEY_ALGORITHM); 
     final IvParameterSpec iv = new IvParameterSpec(new byte[8]); 
     final Cipher decipher = Cipher.getInstance(TRANSFORMATION_PADDING); 
     decipher.init(Cipher.DECRYPT_MODE, key, iv); 

     final byte[] plainText = decipher.doFinal(message); 

     return new String(plainText); 
    } 

    public static void main(String[] args) throws Exception { 


     String text = "TEST STRING TO ENCRYPT"; 
     byte[] codedtext = new Md5Encryption().encrypt(text); 
//  String codedtext = ".ªÉ…U$L§U`8ˉ­?¦」›°„"; 
     String decodedtext = new Md5Encryption().decrypt(codedtext); 

     System.out.println(codedtext); // this is a byte array, you'll just see a reference to an array 
     System.out.println(decodedtext); // This correctly shows "TEST STRING TO ENCRYPT" 
    } 


}