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

來源:互聯網
上載者:User

標籤:

1.MD5演算法

無法復原

128位或者64位串,byte數字長度就是16和8,一般表示是使用16進位來表示的話,1個byte轉換成2個16bit,分別表示高地位,所以產生的字串是16位或者是32位的,16位其實是從32位中的中間部分抽出來的。

我們所說的密碼多少位,是表示多少bit,轉換成byte數組的話,就是除以8,但是如果輸出16進位的話就是除以4,因為"1111 1111"="FF";

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

 

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代碼:

//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]];}

 

最後結果是:

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

 

 

2.SHA演算法

無法復原

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

 

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.