数据在iOS中加密,并使用AES ECB在Java服务器中解密

时间:2017-04-30 06:15:33

标签: java ios encryption cryptography porting

使用AES / ECB / PKCS5Padding解密消息的Java服务器。 Android设备可以发送消息并由java服务器成功解密。但是当它转向iOS时,它不起作用。

Android设备加密数据在以下代码中

public static String generateSalt() {
    SecureRandom random = new SecureRandom();
    String salt = new BigInteger(130, random).toString(32);
    return salt;
}
public static String hashKey(String key, String salt) throws UnsupportedEncodingException, NoSuchAlgorithmException {
    String combinedKey = key + salt;
    MessageDigest digest = MessageDigest.getInstance("SHA-256");
    digest.update(combinedKey.getBytes("UTF-8"));
    byte[] hash = digest.digest();
    String hashedKey = String.format("%032X", new BigInteger(+1, hash));
    return hashedKey;
}
private static SecretKeySpec getSecretKey(String myKey) {
    SecretKeySpec secretKey = null;
    byte[] key;
    MessageDigest sha = null;
    try {
        key = myKey.getBytes("UTF-8");
        sha = MessageDigest.getInstance("SHA-1");
        key = sha.digest(key);
        System.out.println(Base64.encode(key));
        key = Arrays.copyOf(key, 16);
        String a = Base64.encode(key);
        System.out.println("key:"+a);
        secretKey = new SecretKeySpec(key, "AES");

    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

    return secretKey;
}
public static String encrypt(String stringToEncrypt, String secret) {
    String encodedEncryptedData = null;

    try {
        SecretKeySpec secretKey = getSecretKey(secret);
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        byte[] encryptedData = cipher.doFinal(stringToEncrypt.getBytes("UTF-8"));
        //System.out.println(encryptedData);
        encodedEncryptedData = Base64.encode(encryptedData);
    } catch (Exception e) {
        System.out.println("Error while encrypting: " + e.toString());
    }
    return encodedEncryptedData;
}

public static void main(String[] args) throws NoSuchAlgorithmException, UnsupportedEncodingException {
    String salt = generateSalt();
    String hash = hashKey("FEAD286FF2678F57C7865B6D6935C0C3",salt);
    //System.out.println(hash);
    String message = "60;50;28042017080701";
    String encryptedData = encrypt(message,hash);
    System.out.println(encryptedData);
}

以下代码是iOS AES加密代码:

+(NSString*) sha256:(NSString *)clear {
const char *s=[clear cStringUsingEncoding:NSASCIIStringEncoding];
NSData *keyData=[NSData dataWithBytes:s length:strlen(s)];
uint8_t digest[CC_SHA256_DIGEST_LENGTH]={0};
CC_SHA256(keyData.bytes, keyData.length, digest);
NSData *out=[NSData dataWithBytes:digest length:CC_SHA256_DIGEST_LENGTH];
NSString *hash=[out description];
hash = [hash stringByReplacingOccurrencesOfString:@" " withString:@""];
hash = [hash stringByReplacingOccurrencesOfString:@"<" withString:@""];
hash = [hash stringByReplacingOccurrencesOfString:@">" withString:@""];
return hash.localizedUppercaseString;}


+(NSString*) getSecretKey:(NSString *)key {
const char *cstr = [key cStringUsingEncoding:NSUTF8StringEncoding];
NSData *data = [NSData dataWithBytes:cstr length:key.length];
uint8_t digest[CC_SHA1_DIGEST_LENGTH];
CC_SHA1(data.bytes, (int)data.length, digest);
NSData * base64 = [[NSData alloc]initWithBytes:digest length:16];
NSString *result = [[NSString alloc] initWithData:[base64 base64EncodedDataWithOptions:kNilOptions] encoding:NSUTF8StringEncoding];
return result;}
+ (NSString *)encryptAES:(NSString *)content key:(NSString *)key{
NSData *contentData = [content dataUsingEncoding:NSUTF8StringEncoding];
NSUInteger dataLength = contentData.length;
char keyPtr[kCCKeySizeAES128 + 1];
memset(keyPtr, 0, sizeof(keyPtr));
[key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
size_t encryptSize = dataLength + kCCBlockSizeAES128;
void *encryptedBytes = malloc(encryptSize);
size_t actualOutSize = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt,
                                      kCCAlgorithmAES,
                                      kCCOptionPKCS7Padding|kCCOptionECBMode,  
                                      keyPtr,
                                      kCCKeySizeAES128,
                                      nil,
                                      contentData.bytes,
                                      dataLength,
                                      encryptedBytes,
                                      encryptSize,
                                      &actualOutSize);
if (cryptStatus == kCCSuccess) {

    return [[NSData dataWithBytesNoCopy:encryptedBytes length:actualOutSize] base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
}
free(encryptedBytes);
return nil;
return nil;

}

0 个答案:

没有答案