Rsa生成swift并在c#

时间:2018-06-02 04:38:13

标签: swift rsa

我有连接到c#wcf的ios app和android app (Android应用程序工作正常,获取和发送的每个块大小为344) 在swift我生成Rsa公钥和私钥并希望在c#中使用,但问题是(1)当我将公钥发送到加密数据的c#时,他们发给我encryptdata.count = 684(当我测试时我的块大小在swift中是349)并且我无法解密,我能做什么,块大小在swift和c#中是相同的 我的Rsa课程

导入基金会 类RSAWrapper {

private var publicKey : SecKey?

private var privateKey : SecKey?


func generateKeyPair(keySize: UInt, privateTag: String, publicTag: String) -> Bool {

    self.publicKey = nil
    self.privateKey = nil

    if (keySize != 512 && keySize != 1024 && keySize != 2048) {
        // Failed
        print("kelid kharab ast")
        return false
    }

    let publicKeyParameters: [NSString: AnyObject] = [
        kSecAttrIsPermanent: true as AnyObject,
        kSecAttrApplicationTag: publicTag as AnyObject
    ]
    let privateKeyParameters: [NSString: AnyObject] = [
        kSecAttrIsPermanent: true as AnyObject,
        kSecAttrApplicationTag: publicTag as AnyObject
    ]
    let parameters: [String: AnyObject] = [
        kSecAttrKeyType as String: kSecAttrKeyTypeRSA,
        kSecAttrKeySizeInBits as String: keySize as AnyObject,
        kSecPrivateKeyAttrs as String: privateKeyParameters as AnyObject,
        kSecPublicKeyAttrs as String: publicKeyParameters as AnyObject
    ];

    let status : OSStatus = SecKeyGeneratePair(parameters as CFDictionary, &(self.publicKey), &(self.privateKey))

    return (status == errSecSuccess && self.publicKey != nil && self.privateKey != nil)
}

func encrypt(text: String) -> [UInt8] {
    let plainBuffer = [UInt8](text.utf8)
    var cipherBufferSize : Int = Int(SecKeyGetBlockSize((self.publicKey)!))
    var cipherBuffer = [UInt8](repeating:0, count:Int(cipherBufferSize))

    // Encrypto  should less than key length
    let status = SecKeyEncrypt((self.publicKey)!, SecPadding.PKCS1, plainBuffer, plainBuffer.count, &cipherBuffer, &cipherBufferSize)
    if (status != errSecSuccess) {
        print("Failed Encryption")
    }
    return cipherBuffer
}

func decprypt(encrpted: [UInt8]) -> String? {
    var plaintextBufferSize = Int(SecKeyGetBlockSize((self.privateKey)!))
    var plaintextBuffer = [UInt8](repeating:0, count:Int(plaintextBufferSize))

    let status = SecKeyDecrypt((self.privateKey)!, SecPadding.PKCS1, encrpted, plaintextBufferSize, &plaintextBuffer, &plaintextBufferSize)

    if (status != errSecSuccess) {
        print("Failed Decrypt")
        return nil
    }
    return NSString(bytes: &plaintextBuffer, length: plaintextBufferSize, encoding: String.Encoding.utf8.rawValue)! as String
}


func encryptBase64(text: String) -> String {
    let plainBuffer = text.bytes
    var cipherBufferSize : Int = Int(SecKeyGetBlockSize((self.publicKey)!))
    var cipherBuffer = [UInt8](repeating:0, count:Int(cipherBufferSize))

    // Encrypto  should less than key length
    let status = SecKeyEncrypt((self.publicKey)!, SecPadding.PKCS1, plainBuffer, plainBuffer.count, &cipherBuffer, &cipherBufferSize)
    if (status != errSecSuccess) {
        print("Failed Encryption")
    }

    let mudata = NSData(bytes: &cipherBuffer, length: cipherBufferSize)
    return mudata.base64EncodedString(options: NSData.Base64EncodingOptions.lineLength64Characters)
}

func decpryptBase64(encrpted: String) -> String? {

    let data : NSData = NSData(base64Encoded: encrpted, options: .ignoreUnknownCharacters)!
    let count = data.length / MemoryLayout<UInt8>.size
    var array = [UInt8](repeating: 0, count: count)
    data.getBytes(&array, length:count * MemoryLayout<UInt8>.size)

    var plaintextBufferSize = Int(SecKeyGetBlockSize((self.privateKey)!))
    var plaintextBuffer = [UInt8](repeating:0, count:Int(plaintextBufferSize))

    let status = SecKeyDecrypt((self.privateKey)!, SecPadding.PKCS1, array, plaintextBufferSize, &plaintextBuffer, &plaintextBufferSize)

    if (status != errSecSuccess) {
        print("Failed Decrypt")
        return nil
    }
    return NSString(bytes: &plaintextBuffer, length: plaintextBufferSize, encoding: String.Encoding.utf8.rawValue)! as String
}


func getPublicKey() -> SecKey? {
    return self.publicKey
}

func getPrivateKey() -> SecKey? {
    return self.privateKey

}

}

我的模块

  

90348B28C9270D14A9161F6EB9C96D7C207832E92B407EC49ECC0A42835FD5345CA39336DCDA9DD8B83FDFF1871BC0D34C406FB7831038F7768E42209DB18E1B7D1DAE9559014B9014D747C801670190D3292377C1645DA2B883B37ADC3B8D4620757FC65D5C6FBC9ECD3FD663F26DCAEE0DE6580EBB2349E2410BF954BF8F29356EFECB228043E9E3F6D5059D47ADAF65470947C59F6304024EA371B513591AA67B006751EA516D547B8EBF258409EDAD471C02D13759D59CDA56DA8C13369E17AFD0081BBB228502849DAB1039B7EB6E87C4D289AA88A78770BEA69CBA1BD5954899BB85D54EF21A305F1E9E7DFB5AC25299F3B1FA38A4E106A0E6B00D535F

我的指数

  

十六进制:10001,十进制:65537

我可以为swift做的吗我读了这个链接(RSA encryption between Swift and C#)但是我在swift中生成rsa键而不是C#

0 个答案:

没有答案