• iphone 下用mac libSystem 中的CCCrypt 进行des加密


    /*此例子没测试过

    #import <CommonCrypto/CommonCryptor.h>
    
    

    + (NSString *)DESEncryptStr:(NSString *)sTextIn key:(NSString *)sKey
    {
    NSStringEncoding EnC = NSUTF8StringEncoding;

    NSMutableData * dTextIn = [[sTextIn dataUsingEncoding: EnC] mutableCopy];
        NSMutableData * dKey = [[sKey dataUsingEncoding:EnC] mutableCopy];

    [dKey setLength:kCCBlockSizeDES];
    uint8_t *bufferPtr1 = NULL;
    size_t bufferPtrSize1 = 0;
    size_t movedBytes1 = 0;

    Byte iv[] = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
    bufferPtrSize1 = ([sTextIn length] + kCCKeySizeDES) & ~(kCCKeySizeDES -1);
    bufferPtr1 = malloc(bufferPtrSize1 * sizeof(uint8_t));
    memset((void *)bufferPtr1, 0x00, bufferPtrSize1);
    CCCryptorStatus ccStatus = CCCrypt(kCCEncrypt, // CCOperation op (kCCEncrypt/kCCDecrypt)
                    kCCAlgorithmDES, // CCAlgorithm alg
                     kCCOptionPKCS7Padding|kCCOptionECBMode, // CCOptions options
                     [dKey bytes], // const void *key
                     [dKey length], // size_t keyLength
                    iv, // const void *iv
                    [dTextIn bytes], // const void *dataIn
                    [dTextIn length], // size_t dataInLength
                    (void *)bufferPtr1, // void *dataOut
                    bufferPtrSize1, // size_t dataOutAvailable
                    &movedBytes1); // size_t *dataOutMoved  
     //if (ccStatus == kCCSuccess) NSLog(@"SUCCESS");
        /*else if (ccStatus == kCC ParamError) return @"PARAM ERROR";
        else if (ccStatus == kCCBufferTooSmall) return @"BUFFER TOO SMALL";
        else if (ccStatus == kCCMemoryFailure) return @"MEMORY FAILURE";
        else if (ccStatus == kCCAlignmentError) return @"ALIGNMENT";
        else if (ccStatus == kCCDecodeError) return @"DECODE ERROR";
        else if (ccStatus == kCCUnimplemented) return @"UNIMPLEMENTED"; */ 

    NSString * sResult = [[[ NSString alloc] initWithData:[NSData dataWithBytes:bufferPtr1
    length:movedBytes1] encoding:EnC] autorelease];
    free(bufferPtr1);
    return sResult;
    }


    最后转成base64串输出。

    =============下面是c#的========(之前就做iphone的一个和C#通信用到des  后来Mode 和Padding 没一至,导至加出来的不一样。

    C#里的加密方法


    using System;

    using System.Collections.Generic;

    using System.Text;

    using System.IO;

    using System.Security.Cryptography;

    using DesTest;

    namespace DesTest

    {

        class Program

        {

            public static void Main(String[] args)

            {

                string strDes = "I dont konw";

                string strKey = "12345678";

                byte[] IVArrary = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };

                Encoding sEncoding = Encoding.GetEncoding("utf-8");

                byte[] keyArray = sEncoding.GetBytes(strKey);

                byte[] inByte = sEncoding.GetBytes(strDes);

                byte[] retByte = DESEncryptWithCBCZeros(inByte, keyArray, IVArrary);

              //string Output = sEncoding.GetString(retByte);

              //Console.Write(Output);

                String strout = Convert.ToBase64String(retByte);

                Console.Write(strout);

            }

            public static byte[] DESEncryptWithCBCZeros(byte[] inBytes, byte[] keyBytes, byte[] ivBytes)

            {

                using (MemoryStream memoryStream = new MemoryStream())

                {

                    DES des = DES.Create();

          //        des.Mode = CipherMode.CBC;

                    des.Mode = CipherMode.ECB;

         //         des.Padding = PaddingMode.Zeros;

                    des.Padding = PaddingMode.PKCS7;

                    using (CryptoStream cryptoStream = new CryptoStream(memoryStream, des.CreateEncryptor(keyBytes, ivBytes), CryptoStreamMode.Write))

                    {

                        cryptoStream.Write(inBytes, 0, inBytes.Length);

                        cryptoStream.FlushFinalBlock();

                        byte[] bytes = memoryStream.ToArray();

                        cryptoStream.Close();

                        memoryStream.Close();

                        return bytes;

                    

                    }

                }

            }  

           

        };

    }

  • 相关阅读:
    机器学习项目流程
    机器学习之数据归一化问题
    python三数之和
    从不订购的客户
    case when then的用法-leetcode交换工资
    .NET Core Api 集成 swagger
    .NET CORE 2.1 导出excel文件的两种方法
    Dapper的基本使用
    (转)深入研究MiniMVC之后续篇
    (转)深入研究 蒋金楠(Artech)老师的 MiniMvc(迷你 MVC),看看 MVC 内部到底是如何运行的
  • 原文地址:https://www.cnblogs.com/sanjin/p/2185594.html
Copyright © 2020-2023  润新知