整理經常使用加密 iOS 與 Android 加密 MD5-SHA1

1.MD5算法java

不可逆算法

128位或者64位串,byte數字長度就是16和8,通常表示是使用16進制來表示的話,1個byte轉換成2個16bit,分別表示高地位,因此生成的字符串是16位或者是32位的,16位實際上是從32位中的中間部分抽出來的。數組

咱們所說的密碼多少位,是表示多少bit,轉換成byte數組的話,就是除以8,可是若是輸出16進制的話就是除以4,由於"1111 1111"="FF";app

舉例來講:256位 byte數組或者NSData的length就是256/8=32 輸出16進制就是32*2=64位this

 

MD5算法 Java 代碼:加密

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


public class EncrypMD5 {

    /**
     * TODO(description of this method)
     * @param args
     * @author 丶貳九  2015-4-29 下午5:33:52
     * @since v1.0
     */
    public static void main(String[] args)  throws NoSuchAlgorithmException{  
        String msg = "丶貳九";  
        EncrypMD5 md5 = new EncrypMD5();  
        byte[] resultBytes = md5.eccrypt(msg);  
        System.out.println("明文是:" + msg);  
        System.out.println("密文是:" + EncrypMD5.hexString(resultBytes));  
    }
    //byte字節轉換成16進制的字符串MD5Utils.hexString  
    public static String hexString(byte[] bytes){  
        StringBuffer hexValue = new StringBuffer();  
  
        for (int i = 0; i < bytes.length; i++) {  
            int val = ((int) bytes[i]) & 0xff;  
            if (val < 16)  
                hexValue.append("0");  
            hexValue.append(Integer.toHexString(val));  
        }  
        return hexValue.toString();  
    }  
    
    public byte[] eccrypt(String info) throws NoSuchAlgorithmException{  
        MessageDigest md5 = MessageDigest.getInstance("MD5");  
        byte[] srcBytes = info.getBytes();  
        //使用srcBytes更新摘要  
        md5.update(srcBytes);  
        //完成哈希計算,獲得result  
        byte[] resultBytes = md5.digest();  
        return resultBytes;  
    }  
}

 

MD5  iOS  Objective-C代碼:spa

//md5加密
- (NSString *)md5:(NSString *)str
{
    const char *cStrValue = [str UTF8String];
    unsigned char theResult[CC_MD5_DIGEST_LENGTH];
    CC_MD5(cStrValue, (unsigned)strlen(cStrValue), theResult);
    return [NSString stringWithFormat:@"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
            theResult[0], theResult[1], theResult[2], theResult[3],
            theResult[4], theResult[5], theResult[6], theResult[7],
            theResult[8], theResult[9], theResult[10], theResult[11],
            theResult[12], theResult[13], theResult[14], theResult[15]];
}

 

最後結果是:code

明文是:丶貳九
密文是:203ecebd64a8366e58acf19bbb3148ddorm

 

 

2.SHA算法blog

不可逆

SHA1,SHA256,SHA384,SHA512 分別對應160位,256位import java.security.MessageDigest;

 

SHA算法 Java 代碼:

 

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

public class EncrypSHA {

    /**
     * TODO(description of this method)
     * 
     * @param args
     * @author丶貳九 2015-4-29 下午5:12:17
     * @since v1.0
     */
    
    //byte字節轉換成16進制的字符串MD5Utils.hexString  
    public byte[] eccrypt(String info, String shaType) throws NoSuchAlgorithmException {
        MessageDigest sha = MessageDigest.getInstance(shaType);
        byte[] srcBytes = info.getBytes();
        // 使用srcBytes更新摘要
        sha.update(srcBytes);
        // 完成哈希計算,獲得result
        byte[] resultBytes = sha.digest();
        return resultBytes;
    }

    public byte[] eccryptSHA1(String info) throws NoSuchAlgorithmException {
        return eccrypt(info, "SHA1");
    }

    public byte[] eccryptSHA256(String info) throws NoSuchAlgorithmException {
        return eccrypt(info, "SHA-256");
    }

    public byte[] eccryptSHA384(String info) throws NoSuchAlgorithmException {
        return eccrypt(info, "SHA-384");
    }

    public byte[] eccryptSHA512(String info) throws NoSuchAlgorithmException {
        return eccrypt(info, "SHA-512");
    }

    public static void main(String[] args) throws NoSuchAlgorithmException {
        String msg = "丶貳九";
        EncrypSHA sha = new EncrypSHA();
        String sha1=sha.hexString(sha.eccryptSHA1(msg));
        System.out.println("明文:"+msg);
        System.out.println("密文:"+sha1);
    }
    
    public static String hexString(byte[] bytes){  
        StringBuffer hexValue = new StringBuffer();  
  
        for (int i = 0; i < bytes.length; i++) {  
            int val = ((int) bytes[i]) & 0xff;  
            if (val < 16)  
                hexValue.append("0");  
            hexValue.append(Integer.toHexString(val));  
        }  
        return hexValue.toString();  
    }
}

 

SHA 算法 iOS  Objective-C代碼:

 

//sha1加密
- (NSString *)sha1:(NSString *)str
{
    const char *cstr = [str UTF8String];
    //使用對應的CC_SHA1,CC_SHA256,CC_SHA384,CC_SHA512的長度分別是20,32,48,64
    unsigned char digest[CC_SHA1_DIGEST_LENGTH];
    //使用對應的CC_SHA256,CC_SHA384,CC_SHA512
    CC_SHA1(cstr,  strlen(cstr), digest);
    NSMutableString* result = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH * 2];
    for(int i = 0; i < CC_SHA1_DIGEST_LENGTH; i++) {
        [result appendFormat:@"%02x", digest[i]];
    }
    return result;
}

 

明文:丶貳九密文:600c7ca56a913a86a501d683846752113ed65824

相關文章
相關標籤/搜索