如何仅通过密码使用AES

时间:2019-05-28 17:54:12

标签: swift aes

我正在开发应用程序,现在完成android版本后,我已使用swift 5启动了ios版本。我在android版本中使用了一个功能,希望以Java编写,并使用此功能进行加密/使用aes-256-cbc算法解密纯文本。

如何在CryptoSwift中使用此方法?

    // Java Code

    public class EncryptionDecryption {
    String strResult;

    public  String Encrypt(String text, String key) throws Exception {
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        byte[] keyBytes = new byte[16];
        byte[] b = key.getBytes("UTF-8");
        int len = b.length;
        if (len > keyBytes.length)
            len = keyBytes.length;
        System.arraycopy(b, 0, keyBytes, 0, len);
        SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");
        IvParameterSpec ivSpec = new IvParameterSpec(keyBytes);
        cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);

        byte[] results = cipher.doFinal(text.getBytes("UTF-8"));
        Log.v("GET Result from  final:", results.toString());
        strResult = Base64.encodeToString(results, 1);
        return strResult;
    }


    public String Decrypt(String text, String key) throws Exception {
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); 

        byte[] keyBytes = new byte[16];
        byte[] b = key.getBytes("UTF-8");
        int len = b.length;
        if (len > keyBytes.length)
            len = keyBytes.length;
        System.arraycopy(b, 0, keyBytes, 0, len);
        SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");
        IvParameterSpec ivSpec = new IvParameterSpec(keyBytes);
        cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
        byte[] results = new byte[text.length()];
        //BASE64Decoder decoder = new BASE64Decoder();
        try {
            results = cipher.doFinal(Base64.decode(text, Base64.DEFAULT));
        } catch (Exception e) {
            Log.i("Error in Decryption", e.toString());
        }
        return new String(results, "UTF-8");
    }

    }

1 个答案:

答案 0 :(得分:0)

这是解决方法

import UIKit
import CryptoSwift

class AES : NSObject {

    public static func encrypt(text:String) -> String{

        var salted = Data.init(count: 0)
        var dx = Data.init(count: 0)
        let salt = self.randomData(ofLength: 8)
        let passpharse = "passpharse".bytes
        let passAndSalt = passpharse + salt
        while( salted.count < 48){
            dx = MD5(messageData: dx + Data.init(_: passAndSalt))
            salted += dx
        }

        let iv = salted.subdata(in: 32..<48)
        let key = salted.subdata(in: 0..<32)

        let bytesIV = [UInt8](iv as Data)
        let bytesKey = [UInt8](key as Data)

        do {
            let encrypted = try AES(key: bytesKey, blockMode: CBC(iv: bytesIV), padding: .pkcs5)
            let cipherText = try! encrypted.encrypt(text.bytes)
            let saltData = "Salted__".bytes
            let finalData = saltData + salt + cipherText
            return finalData.toBase64()!
        } catch {
            return ""
        }

    }

    private static func randomData(ofLength length: Int) -> Array<UInt8> {
        var bytes = [UInt8](repeating: 0, count: length)
        let status = SecRandomCopyBytes(kSecRandomDefault, length, &bytes)
        if status == errSecSuccess {
            return bytes
        }
        return Data.init(count: 0).bytes
    }


    private static func MD5(messageData: Data) -> Data {
        let length = Int(CC_MD5_DIGEST_LENGTH)
        //let messageData = string.data(using:.utf8)!
        var digestData = Data(count: length)

        _ = digestData.withUnsafeMutableBytes { digestBytes -> UInt8 in
            messageData.withUnsafeBytes { messageBytes -> UInt8 in
                if let messageBytesBaseAddress = messageBytes.baseAddress, let digestBytesBlindMemory = digestBytes.bindMemory(to: UInt8.self).baseAddress {
                    let messageLength = CC_LONG(messageData.count)
                    CC_MD5(messageBytesBaseAddress, messageLength, digestBytesBlindMemory)
                }
                return 0
            }
        }
        return digestData
    }

}