Skip to content

First few characters in AES decrypted text are strange #70

@chessboy

Description

@chessboy

Hi IDZ,

I'm getting a few strange characters at the beginning of the decrypted text using the following extensions on String, based on your example code. The [UInt8] data passed to decrypt is coming from a stored file (of the encrypted data). After the first few characters, the rest of the decrypted text is perfect.

Any help would be greatly appreciated! Thank you.

public extension String {

	func encryptAsData(key: [UInt8]) -> [UInt8]? {
				
		let cryptor = Cryptor(operation:.encrypt, algorithm: .aes, options: .PKCS7Padding, key: key, iv: Array<UInt8>())
		
		return cryptor.update(string: self)?.final()
	}
	
	static func decrypt(data: [UInt8], key: [UInt8]) -> String? {
		
		let cryptor = Cryptor(operation: .decrypt, algorithm: .aes, options: .PKCS7Padding, key: key, iv: Array<UInt8>())
		
		if let decryptedData = cryptor.update(byteArray: data)?.final() {
			
			return decryptedData.reduce("") { $0 + String(UnicodeScalar($1)) }
		}
		
		return nil
	}
}

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions