浏览代码

Do not throw error for incorrect padding #184

Marcin Krzyżanowski 9 年之前
父节点
当前提交
39bf366ac4

+ 1 - 1
CryptoSwiftTests/AESTests.swift

@@ -194,7 +194,7 @@ final class AESTests: XCTestCase {
         XCTAssertTrue(aes.blockMode == .CBC, "Invalid block mode")
         let encrypted = try! aes.encrypt(plaintext, padding: PKCS7())
         let decrypted = try? aes2.decrypt(encrypted, padding: PKCS7())
-        XCTAssertTrue(decrypted == nil, "failed")
+        XCTAssertTrue(decrypted! != plaintext, "failed")
     }
 
 }

+ 1 - 1
Sources/CryptoSwift/AES.swift

@@ -187,7 +187,7 @@ final public class AES {
         }
         
         if let padding = padding {
-            return try padding.remove(out, blockSize: AES.blockSize)
+            return padding.remove(out, blockSize: AES.blockSize)
         }
         
         return out

+ 4 - 4
Sources/CryptoSwift/PKCS7.swift

@@ -35,16 +35,16 @@ public struct PKCS7: Padding {
         }
         return withPadding
     }
-    
-    public func remove(bytes: [UInt8], blockSize:Int?) throws -> [UInt8] {
+
+    public func remove(bytes: [UInt8], blockSize:Int?) -> [UInt8] {
         let lastByte = bytes.last!
         let padding = Int(lastByte) // last byte
         let finalLength = bytes.count - padding
 
         if finalLength < 0 {
-            throw Error.InvalidPaddingValue
+            return bytes
         }
-        
+
         if padding >= 1 {
             return Array(bytes[0..<finalLength])
         }

+ 1 - 1
Sources/CryptoSwift/Padding.swift

@@ -8,5 +8,5 @@
 
 public protocol Padding {
     func add(data: [UInt8], blockSize:Int) -> [UInt8]
-    func remove(data: [UInt8], blockSize:Int?) throws -> [UInt8]
+    func remove(data: [UInt8], blockSize:Int?) -> [UInt8]
 }