Perfect-Crypto icon indicating copy to clipboard operation
Perfect-Crypto copied to clipboard

aes_256_cbc encrypted results cannot be decrypted

Open a363211861 opened this issue 6 years ago • 1 comments

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.

a363211861 avatar Aug 10 '18 02:08 a363211861

You have initialise the framework once like this _ = PerfectCrypto.isInitialized

sarfraz-ios avatar Oct 29 '20 15:10 sarfraz-ios