我正在使用 RNCrytor 加密我的 JWT,但是,无法从支持的 Django 验证加密的字符串。我正在使用 SwiftJWT 库来生成 jwt。
这是我的 Swift 代码
let header = Header(kid: "KeyID1")
var authHeader: String? = nil
let clntConfig = ClientConfig()
if clntConfig.isValid() {
let clientID = clntConfig.getClientsID()
let deviceID = clntConfig.getDevicesID()
let appSecret = clntConfig.getAppsSecret()
let deviceSecret = clntConfig.getDeviceSecrets()
let myClaims = AuthClientClaims(clientKey: clientID,deviceUid: deviceID,deviceSecret: deviceSecret)
do {
print("****\(deviceSecret)***")
let jwtSecret = passToPhrase(password: deviceSecret)
print("****\(jwtSecret)***")
let base64encodedSecretString = jwtSecret.data(using: .utf8)?.base64EncodedString()
print("****\(base64encodedSecretString!)***")
//let jwtSigner = JWTSigner.rs
var myJWT = JWT(header: header, claims: myClaims)
let privateKey: Data = base64encodedSecretString!.data(using: .utf8)!
let jwtSigner = JWTSigner.hs256(key: privateKey)
let signedJWT = try myJWT.sign(using: jwtSigner)
print("****\(signedJWT)****")
let ciphertext = RNCryptor.encrypt(data: signedJWT.data(using: .utf8)!, withPassword: appSecret)
authHeader = ciphertext.base64EncodedString()
}
这是后端代码
def do_decrypt(en_data, secret):
en_data = en_data + '==============='
encrypted_data = base64.b64decode(en_data)
decrypted_data = rncryptor.decrypt(encrypted_data, secret)
return decrypted_data