zoukankan      html  css  js  c++  java
  • 整理常用加密 iOS 与 Android 加密 MD5-SHA1

    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

  • 相关阅读:
    Java实现 LeetCode 735 行星碰撞(栈)
    Java实现 LeetCode 735 行星碰撞(栈)
    Java实现 LeetCode 887 鸡蛋掉落(动态规划,谷歌面试题,蓝桥杯真题)
    Java实现 LeetCode 887 鸡蛋掉落(动态规划,谷歌面试题,蓝桥杯真题)
    Java实现 LeetCode 887 鸡蛋掉落(动态规划,谷歌面试题,蓝桥杯真题)
    Java实现 蓝桥杯算法提高 求最大值
    Java实现 蓝桥杯算法提高 求最大值
    Java实现 蓝桥杯算法提高 求最大值
    Python eval() 函数
    Python repr() 函数
  • 原文地址:https://www.cnblogs.com/kevin-chen/p/4466742.html
Copyright © 2011-2022 走看看