国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > iOS > 正文

3des加密Android和iOS的加密算法有時候位數(shù)是不同的。解決辦法

2019-11-09 16:33:56
字體:
供稿:網(wǎng)友
 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";      // 加解密統(tǒng)一使用的編碼方式      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;  } 
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 石门县| 泽州县| 溧阳市| 沁水县| 友谊县| 扬州市| 灵石县| 柳州市| 澳门| 体育| 蓬莱市| 钦州市| 孝昌县| 静海县| 遵化市| 金沙县| 彭州市| 桂林市| 阿拉尔市| 方山县| 古浪县| 西林县| 安远县| 河北省| 宣威市| 龙江县| 新建县| 赣州市| 十堰市| 林周县| 五家渠市| 保德县| 云和县| 金华市| 安丘市| 曲阜市| 天津市| 沂南县| 南部县| 仙居县| 奉节县|