AES加密解密(java、web、app)
AES加密解密(java、web、app)
java端
package com.unicomSmartCity.common.core.utils;
import com.unicomSmartCity.common.core.exception.CustomException;
import org.apache.commons.codec.binary.Hex;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
java
/**ios
-
@Author: liqiweb
-
@Description: aes加密解密算法
-
@Date: 2020/12/14 10:51apache
-
@Version: 1.0
*/
public class AesEncryptUtils {
private final static String key = 「abcdef0123456789」;
json/**後端
-
加密app
-
@param input函數
-
@return
*/
public static String encrypt(String input) {
byte[] crypted = null;
try {
工具SecretKeySpec skey = new SecretKeySpec(key.getBytes(), "AES"); Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, skey); crypted = cipher.doFinal(input.getBytes());
} catch (Exception e) {
throw new CustomException(e.getMessage());
}
return new String(Hex.encodeHex(crypted));
}
/**
- aes解碼
- @param input
- @return
*/
public static String decrypt(String input) {
byte[] output = null;
try {
SecretKeySpec skey = new SecretKeySpec(key.getBytes(), 「AES」);
Cipher cipher = Cipher.getInstance(「AES/ECB/PKCS5Padding」);
cipher.init(Cipher.DECRYPT_MODE, skey);
output = cipher.doFinal(Hex.decodeHex(input.toCharArray()));
} catch (Exception e) {
System.out.println(e.toString());
throw new CustomException(e.getMessage());
}
return new String(output);
}
-
}
Web端
封裝
封裝全局解密加密工具類文件
下載CryptoJS而且從依賴包引入
import CryptoJS from ‘crypto-js/crypto-js’ //引用AES源碼js
定義key值要與後端相同不一樣的話解密算法會出問題
var key = CryptoJS.enc.Utf8.parse(「abcdef0123456789」);
加密函數
export function Encrypt(word) {
let srcs = CryptoJS.enc.Utf8.parse(word);
let encrypted = CryptoJS.AES.encrypt(srcs, key, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 });
return encrypted.ciphertext.toString().toUpperCase();
}
解密函數
export function Decrypt(word) {
let encryptedHexStr = CryptoJS.enc.Hex.parse(word);
let srcs = CryptoJS.enc.Base64.stringify(encryptedHexStr);
let decrypt = CryptoJS.AES.decrypt(srcs, key, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 });
let decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
return decryptedStr.toString();
}
解密
在解密頁面import 引入解密函數
import {Decrypt} from 「@/utils/aes/security.js」;
把後端出餐加密字符解密以後利用JSON.parse轉成json對象
加密
在參數發送頁面引入加密函數import {Encrypt} from 「@/utils/aes/security.js」;
Query形式傳參須要以Encrypt轉換以後 以鍵值對的對象形式發送
Body形式只需將參數直接轉換後發送便可
ios端
NSData+AES128.m
// 加密
- (NSData *)aes128_encrypt:(NSString *)key {
char keyPtr[kCCKeySizeAES128 + 1];
memset(keyPtr, 0, sizeof(keyPtr));
[key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
NSUInteger dataLength = [self length];
size_t bufferSize = dataLength + kCCBlockSizeAES128;
void *buffer = malloc(bufferSize);
size_t numBytesEncrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding | kCCOptionECBMode, keyPtr, kCCBlockSizeAES128, NULL, [self bytes], dataLength, buffer, bufferSize, &numBytesEncrypted);
if (cryptStatus == kCCSuccess) {
return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
}
free(buffer);
return nil;
}
// 解密 - (NSData *)aes128_decrypt:(NSString *)key {
char keyPtr[kCCKeySizeAES128+1];
bzero(keyPtr, sizeof(keyPtr));
[key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
NSUInteger dataLength = [self length];
size_t bufferSize = dataLength + kCCBlockSizeAES128;
void *buffer = malloc(bufferSize);
size_t numBytesDecrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmAES128,
kCCOptionPKCS7Padding | kCCOptionECBMode, keyPtr, kCCBlockSizeAES128,
NULL, [self bytes], dataLength, buffer, bufferSize, &numBytesDecrypted);
if (cryptStatus == kCCSuccess) {
return [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted];
}
free(buffer);
return nil;
}
NSString+AES128.h
// 加密 - (NSString *)aes128_encrypt:(NSString *)key{
//const char *cstr = [self cStringUsingEncoding:NSUTF8StringEncoding];
//NSData data = [NSData dataWithBytes:cstr length:self.length];
NSData
data = [self dataUsingEncoding:NSUTF8StringEncoding];
//對數據進行加密
NSData *result = [data aes128_encrypt:key];
//轉換爲2進制字符串
if (result && result.length > 0) {
Byte datas = (Byte)[result bytes];
NSMutableString *output = [NSMutableString stringWithCapacity:result.length * 2];
for(int i = 0; i < result.length; i++){
[output appendFormat:@"%02x", datas[i]];
}
return output;
}
return nil;
}
// 解密 - (NSString )aes128_decrypt:(NSString )key {
//轉換爲2進制Data
NSMutableData data = [NSMutableData dataWithCapacity:self.length / 2]; unsigned char whole_byte; char byte_chars[3] = {’\0’,’\0’,’\0’}; int i; for (i = 0; i < [self length] / 2; i++) { byte_chars[0] = [self characterAtIndex:i2]; byte_chars[1] = [self characterAtIndex:i2+1]; whole_byte = strtol(byte_chars, NULL, 16); [data appendBytes:&whole_byte length:1]; } //對數據進行解密 NSData result = [data aes128_decrypt:key]; if (result && result.length > 0) { return [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding]; } return nil; } 加密時引入#import 「NSString+AES128.h」 調用加密方法或解密方法,參數是密鑰 NSString *enStr = [paramStr aes128_encrypt:XACAceEncrypt_KEY]; NSString *deStr = [enStr aes128_decrypt:XACAceEncrypt_KEY];