iOS&JAVA AES加密解密

AES加密与解密,iOS端代码:
AESCipher.h文件

#import 

@interface AESCipher : NSObject

+(NSString*)aesEncryptString:(NSString *)content;

+(NSString*)aesDecryptString:(NSString*)content;

+(NSString *)aesEncryptString:(NSString *)content withKey:(NSString *)key withIv:(NSString*)iv;

+(NSString *)aesDecryptString:(NSString *)content withKey:(NSString *)key withIv:(NSString*)iv;

@end

AESCipher.m文件:

//
//  AESCipher.m
//  WLPoetryProject
//
//  Created by 变啦 on 2019/10/30.
//  Copyright © 2019 龙培. All rights reserved.
//

#import "AESCipher.h"
#import 
@implementation AESCipher

static NSString *publicKey = @"12345678876543211234567887654321";
static NSString *publicIv = @"8888777766665544";

+(NSString *)aesEncryptString:(NSString *)content{
    return [self aesEncryptString:content withKey:publicKey withIv:publicIv];
}
+(NSString *)aesDecryptString:(NSString *)content{
    return [self aesDecryptString:content withKey:publicKey withIv:publicIv];
}

+(NSString *)aesEncryptString:(NSString *)content withKey:(NSString *)key withIv:(NSString*)iv{
    NSData *contentData = [content dataUsingEncoding:NSUTF8StringEncoding];
    NSData *keyData = [key dataUsingEncoding:NSUTF8StringEncoding];
    NSData *ivData = [iv dataUsingEncoding:NSUTF8StringEncoding];
    NSData *encrptData = [self aesEncryptData:contentData withKey:keyData withIv:ivData];
    return [encrptData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
}

+(NSString *)aesDecryptString:(NSString *)content withKey:(NSString *)key withIv:(NSString*)iv {
    NSData *contentData = [[NSData alloc] initWithBase64EncodedString:content options:NSDataBase64DecodingIgnoreUnknownCharacters];
    NSData *keyData = [key dataUsingEncoding:NSUTF8StringEncoding];
    NSData *ivData = [iv dataUsingEncoding:NSUTF8StringEncoding];
    NSData *decryptedData = [self aesDecryptData:contentData withKey:keyData withIv:ivData];
    return [[NSString alloc] initWithData:decryptedData encoding:NSUTF8StringEncoding];
}

+(NSData *)aesEncryptData:(NSData *)contentData withKey:(NSData *)keyData withIv:(NSData*)ivData {
    return [self cipherAction:contentData withKey:keyData withIv:ivData withOperation:kCCEncrypt];
}

+(NSData *)aesDecryptData:(NSData *)contentData withKey:(NSData *)keyData withIv:(NSData*)ivData {
    return [self cipherAction:contentData withKey:keyData withIv:ivData withOperation:kCCDecrypt];
}


+(NSData *) cipherAction:(NSData *)contentData withKey:(NSData *)keyData withIv:(NSData*)ivData withOperation:(CCOperation) operation{
    NSUInteger dataLength = contentData.length;
    
    void const *ivBytes = ivData.bytes;
    void const *contentBytes = contentData.bytes;
    void const *keyBytes = keyData.bytes;
    
    size_t bufferSize = dataLength + kCCKeySizeAES256;
    void *buffer = malloc(bufferSize);
    size_t numBytesEncrypted = 0;
    
    CCCryptorStatus cryptStatus = CCCrypt(operation,
                                          kCCAlgorithmAES,
                                          kCCOptionPKCS7Padding,
                                          keyBytes,
                                          kCCKeySizeAES256,
                                          ivBytes,
                                          contentBytes,
                                          dataLength,
                                          buffer,
                                          bufferSize,
                                          &numBytesEncrypted);
    
    if (cryptStatus == kCCSuccess) {
        return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
    }
    free(buffer);
    return nil;
}

@end

iOS端调用:


iOS&JAVA AES加密解密_第1张图片
iOS端调用

效果:


iOS端效果

可以得知我们可以顺利解密,且加密得到的值为:klswbkdjrFj8auI1/9K5DQ==

接下来是Java端,AESHelper.java文件:

package com.wql.utils.security;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;

public class AESHelper {

    private static final String keyString = "12345678876543211234567887654321";
    private static final String ivString = "8888777766665544";
    private static final String charset = "UTF-8";

    public static String encryptString(String content){
        try {
            byte[] contentBytes = content.getBytes(charset);
            byte[] keyBytes = keyString.getBytes(charset);

            byte[] encryptData = cipherAction(contentBytes,keyBytes,Cipher.ENCRYPT_MODE);

            byte[] base64Bytes = Base64.getEncoder().encode(encryptData);
            return new String(base64Bytes,charset);

        }catch (Exception e){
            e.printStackTrace();
        }
        return "";
    }

    public static String decryptString(String encryptString){
        try {
            byte[] decryptBytes = Base64.getDecoder().decode(encryptString);
            byte[] keyBytes = keyString.getBytes(charset);

            byte[] decryptData = cipherAction(decryptBytes,keyBytes,Cipher.DECRYPT_MODE);
            return new String(decryptData,charset);

        }catch (Exception e){
            e.printStackTrace();
        }
        return "";

    }


    private static byte[] cipherAction(byte[] contentBytes, byte[] keyBytes, int mode) {
        try {
            SecretKeySpec secretKey = new SecretKeySpec(keyBytes, "AES");
            byte[] initParam = ivString.getBytes(charset);
            IvParameterSpec ivParameterSpec = new IvParameterSpec(initParam);

            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(mode, secretKey, ivParameterSpec);

            return cipher.doFinal(contentBytes);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        String enString = AESHelper.encryptString("王啟龙");
        String deString = AESHelper.decryptString(enString);

        System.out.println("加密后:"+enString+"\n解密后:"+deString);
    }

}

Java端运行效果:


iOS&JAVA AES加密解密_第2张图片
Java端加密与解密效果

可以得知我们在Java端顺利解密,且加密得到的值为:klswbkdjrFj8auI1/9K5DQ==
与iOS端的加密结果一致。
由此可以得出结果:iOS端加密,Java端解密可以成功的。Java端加密,iOS端解密也是可以成功的。

代码在这里(另含iOS端RSA加解密、验签;Java端RSA加解密、验签)

加油~

你可能感兴趣的:(iOS&JAVA AES加密解密)