1. 程式人生 > >MD5加密原理

MD5加密原理

public class MD5Util {
    
                                         //使用MD5的演算法進行加密
    
                                         public static String md5(String plainText) {
        
                                                  byte[] secretBytes = null;
        
                                                  try {
            
                                                                  secretBytes = MessageDigest.getInstance("md5").digest(plainText.getBytes());
            
                                                        } catch (NoSuchAlgorithmException e) {
            
                                                                   throw new RuntimeException("沒有MD5這個演算法!");
            
                                                        }
        
                                                   // 16進位制數字        
                                                   String md5code = new BigInteger(1, secretBytes).toString(16);
                
                                                   // 如果生成數字未滿32位,需要前面補0        
                                                   while(md5code.length() < 32){
            
                                                                 md5code = "0" + md5code;
            
                                                   }    
        
                                                          return md5code;
        
                                                   }

                                      //使用MD5的演算法進行加密
                                      public static boolean md5s(String plainText,String pwd) {
        
                                                    byte[] secretBytes = null;
        
                                                     try {
            
                                                                  secretBytes = MessageDigest.getInstance("md5").digest(plainText.getBytes());
            
                                                          } catch (NoSuchAlgorithmException e) {
            
                                                                       throw new RuntimeException("沒有MD5這個演算法!");
            
                                                          }
        
                                                       // 16進位制數字        
                                                        String md5code = new BigInteger(1, secretBytes).toString(16);
                
                                                       // 如果生成數字未滿32位,需要前面補0        
                                                       while(md5code.length() < 32){
            
                                                                   md5code = "0" + md5code;
            
                                                         }    
        
                                                         if(md5code.equals(pwd)){
            
                                                                    return true;
            
                                                         }else{
            
                                                                     return false;
            
                                                          }
        
                                         }
                                }