IOS、java支持DES加密
来源:互联网 发布:韩国在线直播软件 编辑:程序博客网 时间:2024/04/28 04:06
最近在考虑数据加密方面的需求,所以对数据加密简单的看了一下,当然不是看的原理,只是看看怎么能够实现。现在我们需要实现的是移动端和后台(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];
-
- }
-
-
-
-
-
-
- - (NSString *) encryptUseDES:(NSString *)plainText key:(NSString *)key
- {
- NSString *ciphertext = nil;
- const charchar *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;
- }
-
-
- @end
java 代码:需要自己引入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 = "123 456";
- String key = "abcdefgh";
- System.err.println(encrypt(data, key));
- System.err.println(decrypt(encrypt(data, key), key));
-
- System.out.println(decrypt("JF5dX/TlOg529KAhh+vywjzIp5Msktmf", key));
-
- }
-
-
-
-
-
-
-
-
- public static String encrypt(String data, String key) throws Exception {
- byte[] bt = encrypt(data.getBytes(), key.getBytes());
- String strs = new BASE64Encoder().encode(bt);
- return strs;
- }
-
-
-
-
-
-
-
-
-
- public static String decrypt(String data, String key) throws IOException,
- Exception {
- if (data == null)
- return null;
- BASE64Decoder decoder = new BASE64Decoder();
- byte[] buf = decoder.decodeBuffer(data);
- byte[] bt = decrypt(buf,key.getBytes());
- return new String(bt);
- }
-
-
-
-
-
-
-
-
- private static byte[] encrypt(byte[] data, byte[] key) throws Exception {
-
- SecureRandom sr = new SecureRandom();
-
-
- DESKeySpec dks = new DESKeySpec(key);
-
-
- SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
- SecretKey securekey = keyFactory.generateSecret(dks);
-
-
- Cipher cipher = Cipher.getInstance(DES);
-
-
- cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
-
- return cipher.doFinal(data);
- }
-
-
-
-
-
-
-
-
-
- private static byte[] decrypt(byte[] data, byte[] key) throws Exception {
-
- SecureRandom sr = new SecureRandom();
-
-
- DESKeySpec dks = new DESKeySpec(key);
-
-
- SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
- SecretKey securekey = keyFactory.generateSecret(dks);
-
-
- Cipher cipher = Cipher.getInstance(DES);
-
-
- cipher.init(Cipher.DECRYPT_MODE, securekey, sr);
-
- return cipher.doFinal(data);
- }
- }
1 0