我通过swift编写应用程序,我需要AES加密和解密功能,我从另一个.Net解决方案接收加密数据,但我找不到要做的事情.
这是我的.net加密:
public static byte[] AES_Encrypt(byte[] bytesToBeEncrypted, byte[] passwordBytes)
{
byte[] encryptedBytes = null;
byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
using (MemoryStream ms = new MemoryStream())
{
using (RijndaelManaged AES = new RijndaelManaged())
{
AES.KeySize = 256;
AES.BlockSize = 128;
var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000);
AES.Key = key.GetBytes(AES.KeySize / 8);
AES.IV = key.GetBytes(AES.BlockSize / 8);
AES.Mode = CipherMode.CBC;
using (var cs = new CryptoStream(ms, AES.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(bytesToBeEncrypted, 0, …Run Code Online (Sandbox Code Playgroud) 我有一个字符串和一个密钥,我想从中生成一个 HMAC SHA256。虽然我使用了 2 个库
IDZSwiftCommonCrypto和CryptoSwift
而这个答案
没有什么对我有用。我的真相来源是这两个网站
和
https://www.freeformatter.com/hmac-generator.html#ad-output
他们总是为我的情况生成正确的哈希键。知道什么可以在这里工作吗?一些代码示例
对于 IDZSwiftCommonCrypto
func getHMacSHA256(forMessage message: String, key: String) -> String? {
let hMacVal = HMAC(algorithm: HMAC.Algorithm.sha256, key: key).update(string: message)?.final()
if let encryptedData = hMacVal {
let decData = NSData(bytes: encryptedData, length: Int(encryptedData.count))
let base64String = decData.base64EncodedString(options: .lineLength64Characters)
print("base64String: \(base64String)")
return base64String
} else {
return nil
}
}
Run Code Online (Sandbox Code Playgroud)
对于 CryptoSwift
let password: Array<UInt8> = Array(payload.utf8)
let salt: Array<UInt8> = Array("somekey".utf8)
let signedBody = try? HKDF(password: …Run Code Online (Sandbox Code Playgroud) 我正在构建一个社交媒体应用程序,我想帮助在Swift中将密码字符串编码为SHA512.我在GitHub上找到了CryptoSwift库,但我很难将它加载到我的Swift项目中并将其链接到我的项目文件.有谁知道如何相对容易地完成这个?先谢谢你,凯尔
(Xcode 8,Swift 3)
使用CryptoSwift库,我想加密字符串并将其存储在coredata中,出于某种原因,密码字符串结果为nil,尽管密文具有128个值:
let aes = try AES(key: pw, iv: nil, blockMode: .CBC, padding: PKCS7())
let ciphertext = try aes.encrypt(token.utf8.map({$0}))
let cipherstring = String(bytes:ciphertext, encoding: String.Encoding.utf8) // always nil
Run Code Online (Sandbox Code Playgroud)
我也尝试过使用数据:字符串重载,将字节数组转换为数据对象.这也是零结果.
编辑/解决方案(根据Rob Napier的回答)
// encode/convert to string
let aes = try AES(key: pw, iv: nil, blockMode: .CBC, padding: PKCS7())
let ciphertext = try aes.encrypt(token.utf8.map({$0}))
let cipherstring = Data(bytes: ciphertext).base64EncodedString()
// decode
let aes = try AES(key: pw, iv: nil, blockMode: .CBC, padding: PKCS7())
let cipherdata = Data(base64Encoded: cipherstring)
let ciphertext …Run Code Online (Sandbox Code Playgroud) 我正在尝试构建和编译我的项目。这是一位老同事的遗产,他已经不在公司了,而且我对 Xcode 也不是那么了解。现在的问题是,当我在 Xcode 中清理并构建项目时,我收到“Command PhaseScriptExecution failed with a nonzero exit code”
该项目在文件夹中包含应用程序 swift 文件,并且它正在使用 cryptoswift 库。因此,我使用 Cocoapods 1.7.4 运行,在项目文件夹中使用提到的库创建 pod,并成功添加了 Cryptoswift (CryptoSwift (1.0.0) )。
我正在使用Xcode 9.0和CryptoSwift(0.7.2).我正在尝试扩展String以解密AES128加密字符串.我已经使用Pod成功添加了CryptoSwift,但是我收到了以下编译错误 - 我做错了什么?
无法构造'PKCS7',因为它没有可访问的初始值设定项
这是扩展名:
import Foundation
import CryptoSwift
extension String {
// https://stackoverflow.com/questions/27072021/aes-encrypt-and-decrypt
func aesDecrypt(key: String, iv: String) throws -> String {
let data = Data(base64Encoded: self)!
let decrypted = try! AES(key: key, iv: iv, blockMode: .CBC, padding: PKCS7()).decrypt([UInt8](data))
let decryptedData = Data(decrypted)
return String(bytes: decryptedData.bytes, encoding: .utf8) ?? "Could not decrypt"
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个从 API 检索数据并显示在应用程序中的项目。但API使用AES加密,我有aesKey和aesIV密钥,这些密钥是base64编码的。我需要用这些键对另一个字符串进行编码。为此,我使用 CryptoSwift 库,但当我尝试使用此密钥来加密字符串时,Swift 控制台会警告我有关 invalidKeySize 的问题。我尝试从 Base64 解码为字符串,但效果不佳。这些是示例的键;
密钥和 IV 大小为 AES256,为了加密和解密,我需要使用 PKCS7 填充类型和 ECB/CBC 块模式
aesKey = lHLBfVxlGoKoaCqWORJEHh3jOvC2EBx2VHGyNAdqYV0=
aesIV = 2spaSfljZ/cunRbuVkdphQ==
and CryptoSwift code block is:
let aes = try AES(key: "\(aesKeyString)", iv: "\(aesIVString)")
let cipherText = try aes.encrypt(Array("all".utf8))
Run Code Online (Sandbox Code Playgroud) 我目前正在尝试制作类似于master password 的我自己的密码管理器应用程序,它使用一种算法来生成密码,因此它们不必存储在客户端计算机上或在线。
为了实现这一点,我决定使用使用CryptoSwift库的 ChaCha20 密码算法。这是我目前拥有的代码(OS X 应用程序):
import Cocoa
import CryptoSwift
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
@IBOutlet weak var window: NSWindow!
func applicationDidFinishLaunching(aNotification: NSNotification) {
do
{
let UInt8String = "Test".utf8
print("UTF8 string: \(UInt8String)")
let UInt8Array = Array(UInt8String)
print("UTF8 array: \(UInt8Array)")
let encrypted = try ChaCha20(key: "Key", iv: "Iv")!.encrypt(UInt8Array)
print("Encrypted data: \(encrypted)")
} catch _ {
}
}
func applicationWillTerminate(aNotification: NSNotification) {
// Insert code here to tear down your application
}
}
Run Code Online (Sandbox Code Playgroud)
我得到错误的那一行是 …
cryptoswift ×8
swift ×6
ios ×3
aes ×2
cocoapods ×2
cryptography ×2
encryption ×2
base64 ×1
hmac ×1
optional ×1
sha512 ×1
swift2 ×1
swift3 ×1
swift4 ×1
xcode ×1