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

  http://www.iteye.com/topic/1127949
java代码:
  1. package org.liuyq.des3;  
  2.   
  3. import java.security.Key;  
  4.   
  5. import javax.crypto.Cipher;  
  6. import javax.crypto.SecretKeyFactory;  
  7. import javax.crypto.spec.DESedeKeySpec;  
  8. import javax.crypto.spec.IvParameterSpec;  
  9.   
  10. /** 
  11.  * 3DES加密工具类 
  12.  */  
  13. public class Des3 {  
  14.     // 密钥  
  15.     private final static String secretKey = "双方约定的KEY";  
  16.     // 向量  
  17.     private final static String iv = "01234567";  
  18.     // 加解密统一使用的编码方式  
  19.     private final static String encoding = "utf-8";  
  20.   
  21.     /** 
  22.      * 3DES加密 
  23.      *  
  24.      * @param plainText 普通文本 
  25.      * @return 
  26.      * @throws Exception  
  27.      */  
  28.     public static String encode(String plainText) throws Exception {  
  29.         Key deskey = null;  
  30.         DESedeKeySpec spec = new DESedeKeySpec(secretKey.getBytes());  
  31.         SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");  
  32.         deskey = keyfactory.generateSecret(spec);  
  33.   
  34.         Cipher cipher = Cipher.getInstance("desede/CBC/PKCS5Padding");  
  35.         IvParameterSpec ips = new IvParameterSpec(iv.getBytes());  
  36.         cipher.init(Cipher.ENCRYPT_MODE, deskey, ips);  
  37.         byte[] encryptData = cipher.doFinal(plainText.getBytes(encoding));  
  38.         return Base64.encode(encryptData);  
  39.     }  
  40.   
  41.     /** 
  42.      * 3DES解密 
  43.      *  
  44.      * @param encryptText 加密文本 
  45.      * @return 
  46.      * @throws Exception 
  47.      */  
  48.     public static String decode(String encryptText) throws Exception {  
  49.         Key deskey = null;  
  50.         DESedeKeySpec spec = new DESedeKeySpec(secretKey.getBytes());  
  51.         SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");  
  52.         deskey = keyfactory.generateSecret(spec);  
  53.         Cipher cipher = Cipher.getInstance("desede/CBC/PKCS5Padding");  
  54.         IvParameterSpec ips = new IvParameterSpec(iv.getBytes());  
  55.         cipher.init(Cipher.DECRYPT_MODE, deskey, ips);  
  56.   
  57.         byte[] decryptData = cipher.doFinal(Base64.decode(encryptText));  
  58.   
  59.         return new String(decryptData, encoding);  
  60.     }  
  61. }
  62. IOS代码:
  63. #import "DES3Util.h"  
  64. #import <CommonCrypto/CommonCryptor.h>  
  65. #import "GTMBase64.h"  
  66.   
  67. #define gkey            @"双方约定的KEY"  
  68. #define gIv             @"01234567"  
  69.   
  70. @implementation DES3Util  
  71.   
  72. // 加密方法  
  73. + (NSString*)encrypt:(NSString*)plainText {  
  74.     NSData* data = [plainText dataUsingEncoding:NSUTF8StringEncoding];  
  75.     size_t plainTextBufferSize = [data length];  
  76.     const void *vplainText = (const void *)[data bytes];  
  77.       
  78.     CCCryptorStatus ccStatus;  
  79.     uint8_t *bufferPtr = NULL;  
  80.     size_t bufferPtrSize = 0;  
  81.     size_t movedBytes = 0;  
  82.       
  83.     bufferPtrSize = (plainTextBufferSize + kCCBlockSize3DES) & ~(kCCBlockSize3DES - 1);  
  84.     bufferPtr = malloc( bufferPtrSize * sizeof(uint8_t));  
  85.     memset((void *)bufferPtr, 0x0, bufferPtrSize);  
  86.       
  87.     const void *vkey = (const void *) [gkey UTF8String];  
  88.     const void *vinitVec = (const void *) [gIv UTF8String];  
  89.       
  90.     ccStatus = CCCrypt(kCCEncrypt,  
  91.                        kCCAlgorithm3DES,  
  92.                        kCCOptionPKCS7Padding,  
  93.                        vkey,  
  94.                        kCCKeySize3DES,  
  95.                        vinitVec,  
  96.                        vplainText,  
  97.                        plainTextBufferSize,  
  98.                        (void *)bufferPtr,  
  99.                        bufferPtrSize,  
  100.                        &movedBytes);  
  101.       
  102.     NSData *myData = [NSData dataWithBytes:(const void *)bufferPtr length:(NSUInteger)movedBytes];  
  103.     NSString *result = [GTMBase64 stringByEncodingData:myData];  
  104.     return result;  
  105. }  
  106.   
  107. // 解密方法  
  108. + (NSString*)decrypt:(NSString*)encryptText {  
  109.     NSData *encryptData = [GTMBase64 decodeData:[encryptText dataUsingEncoding:NSUTF8StringEncoding]];  
  110.     size_t plainTextBufferSize = [encryptData length];  
  111.     const void *vplainText = [encryptData bytes];  
  112.       
  113.     CCCryptorStatus ccStatus;  
  114.     uint8_t *bufferPtr = NULL;  
  115.     size_t bufferPtrSize = 0;  
  116.     size_t movedBytes = 0;  
  117.       
  118.     bufferPtrSize = (plainTextBufferSize + kCCBlockSize3DES) & ~(kCCBlockSize3DES - 1);  
  119.     bufferPtr = malloc( bufferPtrSize * sizeof(uint8_t));  
  120.     memset((void *)bufferPtr, 0x0, bufferPtrSize);  
  121.       
  122.     const void *vkey = (const void *) [gkey UTF8String];  
  123.     const void *vinitVec = (const void *) [gIv UTF8String];  
  124.       
  125.     ccStatus = CCCrypt(kCCDecrypt,  
  126.                        kCCAlgorithm3DES,  
  127.                        kCCOptionPKCS7Padding,  
  128.                        vkey,  
  129.                        kCCKeySize3DES,  
  130.                        vinitVec,  
  131.                        vplainText,  
  132.                        plainTextBufferSize,  
  133.                        (void *)bufferPtr,  
  134.                        bufferPtrSize,  
  135.                        &movedBytes);  
  136.       
  137.     NSString *result = [[[NSString alloc] initWithData:[NSData dataWithBytes:(const void *)bufferPtr   
  138.                                 length:(NSUInteger)movedBytes] encoding:NSUTF8StringEncoding] autorelease];  
  139.     return result;  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值