Perfect-Crypto
Perfect-Crypto copied to clipboard
aes_256_cbc encrypted results cannot be decrypted
this is mycode(in my project):
func testCipherString() {
let str = "123456"
let password = "45678"
let salt = "7891"
let result = str.encrypt(Cipher.aes_256_cbc, password: password, salt: salt)
print("result=\(result!)")
guard let str2 = result!.decrypt(Cipher.aes_256_cbc, password: password, salt: salt) else {
print("str2 is nil")
return
}
print("str2 = \(str2)")
}
and the log:
result=-----BEGIN CMS-----
MIAGCSqGSIb3DQEHBqCAMIACAQAwgAYJKoZIhvcNAQcBMB0GCWCGSAFlAwQBKgQQ
4cSbalm6wfgU2gmXJxgVdqCABBBzc6JwEsL221rWXM/oBz/PAAAAAAAAAAAAAA==
-----END CMS-----
str2 is nil
But I added my code to the PerfectCryptoTests.swift
func testCipherString() {
let str = "123456"
let password = "45678"
let salt = "7891"
let result = str.encrypt(Cipher.aes_256_cbc, password: password, salt: salt)
print("result=\(result!)")
guard let str2 = result!.decrypt(Cipher.aes_256_cbc, password: password, salt: salt) else {
print("str2 is nil")
return XCTAssert(false)
}
print("str2 = \(str2)")
XCTAssertEqual(str, str2)
}
test is ok, this log:
Test Case '-[PerfectCryptoTests.PerfectCryptoTests testCipherString]' started.
result=-----BEGIN CMS-----
MIAGCSqGSIb3DQEHBqCAMIACAQAwgAYJKoZIhvcNAQcBMB0GCWCGSAFlAwQBKgQQ
5/NY293XO9NMMtm4nSay7aCABBBeLr0OYPbC8WL4cziMFndeAAAAAAAAAAAAAA==
-----END CMS-----
str2 = 123456
Test Case '-[PerfectCryptoTests.PerfectCryptoTests testCipherString]' passed (0.052 seconds).
I do not know why,help
Thanks.
You have initialise the framework once like this
_ = PerfectCrypto.isInitialized