首页 > 系统 > iOS > 正文

3des加密Android和iOS的加密算法有时候位数是不同的。解决办法

2019-11-09 16:33:56
字体:
来源:转载
供稿:网友
 http://www.iteye.com/topic/1127949java代码:package org.liuyq.des3;    import java.security.Key;    import javax.crypto.Cipher;  import javax.crypto.SecretKeyFactory;  import javax.crypto.spec.DESedeKeySpec;  import javax.crypto.spec.IvParameterSpec;    /**  * 3DES加密工具类  */  public class Des3 {      // 密钥      PRivate final static String secretKey = "双方约定的KEY";      // 向量      private final static String iv = "01234567";      // 加解密统一使用的编码方式      private final static String encoding = "utf-8";        /**      * 3DES加密      *       * @param plainText 普通文本      * @return      * @throws Exception       */      public static String encode(String plainText) throws Exception {          Key deskey = null;          DESedeKeySpec spec = new DESedeKeySpec(secretKey.getBytes());          SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");          deskey = keyfactory.generateSecret(spec);            Cipher cipher = Cipher.getInstance("desede/CBC/PKCS5Padding");          IvParameterSpec ips = new IvParameterSpec(iv.getBytes());          cipher.init(Cipher.ENCRYPT_MODE, deskey, ips);          byte[] encryptData = cipher.doFinal(plainText.getBytes(encoding));          return Base64.encode(encryptData);      }        /**      * 3DES解密      *       * @param encryptText 加密文本      * @return      * @throws Exception      */      public static String decode(String encryptText) throws Exception {          Key deskey = null;          DESedeKeySpec spec = new DESedeKeySpec(secretKey.getBytes());          SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");          deskey = keyfactory.generateSecret(spec);          Cipher cipher = Cipher.getInstance("desede/CBC/PKCS5Padding");          IvParameterSpec ips = new IvParameterSpec(iv.getBytes());          cipher.init(Cipher.DECRYPT_MODE, deskey, ips);            byte[] decryptData = cipher.doFinal(Base64.decode(encryptText));            return new String(decryptData, encoding);      }  }IOS代码:#import "DES3Util.h"  #import <CommonCrypto/CommonCryptor.h>  #import "GTMBase64.h"    #define gkey            @"双方约定的KEY"  #define gIv             @"01234567"    @implementation DES3Util    // 加密方法  + (NSString*)encrypt:(NSString*)plainText {      NSData* data = [plainText dataUsingEncoding:NSUTF8StringEncoding];      size_t plainTextBufferSize = [data length];      const void *vplainText = (const void *)[data bytes];            CCCryptorStatus ccStatus;      uint8_t *bufferPtr = NULL;      size_t bufferPtrSize = 0;      size_t movedBytes = 0;            bufferPtrSize = (plainTextBufferSize + kCCBlockSize3DES) & ~(kCCBlockSize3DES - 1);      bufferPtr = malloc( bufferPtrSize * sizeof(uint8_t));      memset((void *)bufferPtr, 0x0, bufferPtrSize);            const void *vkey = (const void *) [gkey UTF8String];      const void *vinitVec = (const void *) [gIv UTF8String];            ccStatus = CCCrypt(kCCEncrypt,                         kCCAlgorithm3DES,                         kCCOptionPKCS7Padding,                         vkey,                         kCCKeySize3DES,                         vinitVec,                         vplainText,                         plainTextBufferSize,                         (void *)bufferPtr,                         bufferPtrSize,                         &movedBytes);            NSData *myData = [NSData dataWithBytes:(const void *)bufferPtr length:(NSUInteger)movedBytes];      NSString *result = [GTMBase64 stringByEncodingData:myData];      return result;  }    // 解密方法  + (NSString*)decrypt:(NSString*)encryptText {      NSData *encryptData = [GTMBase64 decodeData:[encryptText dataUsingEncoding:NSUTF8StringEncoding]];      size_t plainTextBufferSize = [encryptData length];      const void *vplainText = [encryptData bytes];            CCCryptorStatus ccStatus;      uint8_t *bufferPtr = NULL;      size_t bufferPtrSize = 0;      size_t movedBytes = 0;            bufferPtrSize = (plainTextBufferSize + kCCBlockSize3DES) & ~(kCCBlockSize3DES - 1);      bufferPtr = malloc( bufferPtrSize * sizeof(uint8_t));      memset((void *)bufferPtr, 0x0, bufferPtrSize);            const void *vkey = (const void *) [gkey UTF8String];      const void *vinitVec = (const void *) [gIv UTF8String];            ccStatus = CCCrypt(kCCDecrypt,                         kCCAlgorithm3DES,                         kCCOptionPKCS7Padding,                         vkey,                         kCCKeySize3DES,                         vinitVec,                         vplainText,                         plainTextBufferSize,                         (void *)bufferPtr,                         bufferPtrSize,                         &movedBytes);            NSString *result = [[[NSString alloc] initWithData:[NSData dataWithBytes:(const void *)bufferPtr                                   length:(NSUInteger)movedBytes] encoding:NSUTF8StringEncoding] autorelease];      return result;  } 
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表