ios - 使用 CryptoSwift 在 java 和 ios 中进行等效加密

标签 ios swift swift3 cryptoswift

 public static String encryptStringToBase64(String messageString) { 
        byte[] messageBytes = messageString.getBytes("UTF-8"); 
        byte[] encrypted = convert(1, messageBytes); 
        return Base64.encodeBytes(encrypted); 
    } 

private static byte[] convert(int mode, byte[] messageBytes) { 

    MessageDigest sha256 = MessageDigest.getInstance("SHA-256"); 
    sha256.update("abcdefgh".getBytes("UTF-8")); 
    byte[] keyBytes = sha256.digest(); 
    byte[] hash = Arrays.copyOfRange(keyBytes, 0, 16); 

    SecretKeySpec keySpec = new SecretKeySpec(hash, "AES"); 
    byte[] ivBytes = new byte[16]; 
    IvParameterSpec ivSpec = new IvParameterSpec(ivBytes); 
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); 
    cipher.init(mode, keySpec, ivSpec); 
    return cipher.doFinal(messageBytes); 
}

以上是java中用于加密的逻辑尝试下面的加密技术 https://gist.github.com/m1entus/f70d4d1465b90d9ee024 https://github.com/krzyzanowskim/CryptoSwift 但我无法在 java 和 iOS 中生成相同的加密字符串。有什么方法可以在 iOS 中重现相同的数据。

Swift 3.0 代码

import CryptoSwift

let ram = "aaaa"
let pas = "abbbb"

let usernameutf8data = ram.data(using: String.Encoding.utf8)
let passwordutf8data = pas.data(using: String.Encoding.utf8)

let copyRight = "abcdefgh"
let copyRightUtf8 = copyRight.data(using: .utf8)

let hash =  copyRightUtf8?.sha256()
let key: Array<UInt8> = Array(hash!.bytes[0..<16])

let iv: Array<UInt8> = [0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00]//AES.randomIV(AES.blockSize)

let encrypted = try AES(key:key , iv: iv, blockMode: .CBC, padding: PKCS7()).encrypt(usernameutf8data!)

let encryptedpas = try AES(key:key , iv: iv, blockMode: .CBC, padding: PKCS7()).encrypt(passwordutf8data!)

最佳答案

Base64EncodedString:

 func encryptStringToBaseSixtyFour(value : String) -> String {
       let data = value.data(using: .utf8)
       return data?.base64EncodedString()
 }

AES加密字符串

func aesEncrypt(value: String, key: String, iv: String) throws -> String {
    let data = value.data(using: .utf8)!
    let encrypted = try! AES(key: key, iv: iv, blockMode: .CBC, padding: PKCS7()).encrypt([UInt8](data))
    let encryptedData = Data(encrypted)
    return encryptedData.base64EncodedString()
}

AES 解密值

func aesDecrypt(encryptedString: String,key: String, iv: String) throws -> String {
    let data = Data(base64Encoded: encryptedString)!
    let decrypted = try! AES(key: key, iv: iv, blockMode: .CBC, padding: PKCS7()).decrypt([UInt8](data))
    let decryptedData = Data(decrypted)
    return String(bytes: decryptedData.bytes, encoding: .utf8) ?? "Could not decrypt"
}

关于ios - 使用 CryptoSwift 在 java 和 ios 中进行等效加密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45030293/

相关文章:

ios - 需要修复我的复选框 : to change states in one click instead of two clicks. Swift 3, IOS

ios - 如何读取 Firebase 子值?

ios - block 回调——当原始对象被释放时

ios - HomeKit 模拟器 : Adding human readable description to custom service characteristic

vapor 对象的 JSON 数组

iOS/swift 4 : change color from a programmatically created label

ios - 删除 subview 不起作用

iphone - Xcode : How would I do a one-off UIAlertView?

ios - 父子关系与引用语义混淆(swift)

ios - 使用 AWS 的 Swift 显示 UIImageView