首页 > 代码库 > IOS、java支持DES加密
IOS、java支持DES加密
转载请注明博客地址:http://blog.csdn.net/mengxiangyue/article/details/40015727
最近在考虑数据加密方面的需求,所以对数据加密简单的看了一下,当然不是看的原理,只是看看怎么能够实现。现在我们需要实现的是移动端和后台(java)数据加解密的配合,开始的时候考虑的使用RSA,因为RSA是非对称加密,更加安全点,但是RSA加密的过程中,ios公钥加密的数据,后台java是能够解密成功,但是后台java私钥加密的东西,前端ios,就没有解密成功,实验了很多方法,最终也没有成功,所以就放弃了,转向了安全性差一点的DES加密。
对于DES、RSA的介绍,自己百度去吧,因为我也说不明白。(上面我没有提Android,因为Android使用的是java,所以应该跟后台一致)
下面废话不多说,直接贴上代码:
IOS,需要引入GTMBase64.h、GTMBase64.m、GTMDefines.h,这个github上面有我,自己搜搜吧,还有<CommonCrypto/CommonCryptor.h>。
#import "ViewController.h" #import <CommonCrypto/CommonCryptor.h> #import "GTMBase64.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; NSString *key = @"这是个key"; NSString *encryptedData = [self encryptUseDES:@"this is a text" key:key]; NSLog(@"加密后的数据是:%@", encryptedData); NSLog(@"解密后的数据是:%@", [self decryptUseDES:encryptedData key:key]); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /*字符串加密 *参数 *plainText : 加密明文 *key : 密钥 64位 */ - (NSString *) encryptUseDES:(NSString *)plainText key:(NSString *)key { NSString *ciphertext = nil; const char *textBytes = [plainText UTF8String]; NSUInteger dataLength = [plainText length]; unsigned char buffer[1024]; memset(buffer, 0, sizeof(char)); Byte iv[] = {1,2,3,4,5,6,7,8}; size_t numBytesEncrypted = 0; CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmDES, kCCOptionPKCS7Padding, [key UTF8String], kCCKeySizeDES, iv, textBytes, dataLength, buffer, 1024, &numBytesEncrypted); if (cryptStatus == kCCSuccess) { NSData *data = [NSData dataWithBytes:buffer length:(NSUInteger)numBytesEncrypted]; ciphertext = [[NSString alloc] initWithData:[GTMBase64 encodeData:data] encoding:NSUTF8StringEncoding]; } return ciphertext; } //解密 - (NSString *) decryptUseDES:(NSString*)cipherText key:(NSString*)key { NSData* cipherData = [GTMBase64 decodeString:cipherText]; unsigned char buffer[1024]; memset(buffer, 0, sizeof(char)); size_t numBytesDecrypted = 0; Byte iv[] = {1,2,3,4,5,6,7,8}; CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmDES, kCCOptionPKCS7Padding, [key UTF8String], kCCKeySizeDES, iv, [cipherData bytes], [cipherData length], buffer, 1024, &numBytesDecrypted); NSString* plainText = nil; if (cryptStatus == kCCSuccess) { NSData* data = [NSData dataWithBytes:buffer length:(NSUInteger)numBytesDecrypted]; plainText = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; } return plainText; } @endjava 代码:需要自己引入sun.misc.BASE64Decoder.jar
package com.yue; import java.io.IOException; import java.security.SecureRandom; import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESKeySpec; import Decoder.BASE64Decoder; import Decoder.BASE64Encoder; public class DesUtil { private final static String DES = "DES"; public static void main(String[] args) throws Exception { String data = http://www.mamicode.com/"123 456";>代码下载地址:http://download.csdn.net/detail/mengxiangyue/8028311
IOS、java支持DES加密
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。