加密 JWT 的有效负载如何提高安全性

问题描述 投票:0回答:2

在这个网站上,它说:

请注意,对于签名令牌,此信息尽管受到保护 防止篡改,任何人都可读。不要把秘密 JWT 的有效负载或标头元素中的信息,除非它是 已加密。

这部分对我来说很清楚,即使使用内置的 https://jwt.io/ 调试器,我也可以看到标头和有效负载仅是 base64 编码的。

但是,我不明白这里加密(JWE是术语?)如何能够提高安全性?

在示例中,我看到用户传递了他们的凭据like this,然后使用他们收到的令牌(我知道该示例有缺陷,因为它不使用 HTTPS - 我在这里专门询问有关 HTTPS 请求的信息)。

所以我的问题是,如果通过 HTTPS POST 的请求正文发送用户凭据是可以的并且是最佳实践,为什么 JWT 的标头或有效负载不也是如此?

security https jwt jwe
2个回答
0
投票

初步说明:

每次必须保守秘密时,您需要问自己谁可以分享这个秘密,以了解为什么应该保守秘密以及如何保守秘密。

这里,我们有两个需要保密的对象,令牌及其有效负载:

  • 令牌必须由客户端和服务共享:它让客户端向服务器证明客户端有权执行请求,因为该信息只有这两方知道;
  • 令牌的有效负载不需要与客户端共享,有效负载中的信息(有关客户端的身份和有关其执行某些操作的权利的声明)仅供服务器读取并从中做出决策。

因此,在 HTTPS 流内发送令牌只能让客户端和服务器知道该令牌,其他任何人都无法知道。

并且对令牌的内容进行加密,让该令牌中的信息只有服务器知道。

这样,信息只会披露给需要了解信息的各方。这是信息安全的基本规则。

但是,我不明白这里加密(JWE是术语?)如何能够提高安全性?

如前所述,这提高了安全性,因为有效负载不必透露给客户端。

我的问题是,如果通过 HTTPS POST 的请求正文发送用户凭据是可以的并且是最佳实践,为什么 JWT 的标头或有效负载不也是如此?

如前所述,这是因为在处理信息安全时,您永远不应该共享不需要共享的信息。由于客户端发送的凭据需要向服务器公开,因为这是这种凭据类型的唯一做法,可以让服务器检查客户端是否拥有良好的凭据,并且由于它是通过安全通道发送的,没有其他人可以获得这些秘密,因此这种做法是一种最佳做法。但是执行身份验证不需要向客户端公开令牌的内容(有效负载和签名)。所以这样做不是一个好的做法。


0
投票

我认为这可能会有所帮助

import jwt
import base64
from datetime import datetime
import hashlib


# Get the current timestamp in seconds
def get_current_unix_timestamp():
    return str(int(datetime.now().timestamp()))


# Convert the current Unix timestamp to a human-readable format
def get_human_readable_timestamp(timestamp):
    current_datetime = datetime.fromtimestamp(int(timestamp))
    return current_datetime.strftime("%Y-%m-%d %H:%M:%S")


# Encode the user ID with the timestamp using XOR operation
def encode_userid_with_timestamp(userid, current_unix_timestamp, secret_key):
    # Hash the timestamp and trim to 32 characters
    hashed_timestamp = hashlib.sha256((current_unix_timestamp + secret_key.decode()).encode()).hexdigest()[:32]

    # Combine the hashed timestamp with the user ID
    data = userid + "|" + hashed_timestamp
    data_bytes = data.encode()

    # Perform XOR operation
    repeated_key = secret_key * ((len(data_bytes) // len(secret_key)) + 1)
    repeated_key = repeated_key[:len(data_bytes)]
    encoded_data_bytes = bytes([a ^ b for a, b in zip(data_bytes, repeated_key)])

    # Encode the result in Base64 and return
    encoded_data = base64.b64encode(encoded_data_bytes).decode()
    return encoded_data


# Decode the XOR-encoded data
def decode_userid_timestamp(encoded_data, key):
    # Decode from Base64
    encoded_data_bytes = base64.b64decode(encoded_data.encode())

    # Perform XOR operation
    repeated_key = key * ((len(encoded_data_bytes) // len(key)) + 1)
    repeated_key = repeated_key[:len(encoded_data_bytes)]
    decoded_data_bytes = bytes([a ^ b for a, b in zip(encoded_data_bytes, repeated_key)])

    # Separate the user ID and the hashed timestamp
    decoded_data = decoded_data_bytes.decode()
    userid, hashed_timestamp = decoded_data.split("|")

    # Return the hashed timestamp
    return userid, hashed_timestamp


def main():
    userid = "23243232"
    xor_secret_key = b'generally_user_salt_or_hash_or_random_uuid_this_value_must_be_in_dbms'
    jwt_secret_key = 'yes_your_service_jwt_secret_key'

    current_unix_timesstamp = get_current_unix_timestamp()
    human_readable_timestamp = get_human_readable_timestamp(current_unix_timesstamp)

    encoded_userid_timestamp = encode_userid_with_timestamp(userid, current_unix_timesstamp, xor_secret_key)
    decoded_userid, hashed_timestamp = decode_userid_timestamp(encoded_userid_timestamp, xor_secret_key)

    jwt_token = jwt.encode({'timestamp': human_readable_timestamp, 'userid': encoded_userid_timestamp}, jwt_secret_key, algorithm='HS256')
    decoded_token = jwt.decode(jwt_token, jwt_secret_key, algorithms=['HS256'])

    print("")
    print("- Current Unix Timestamp:", current_unix_timesstamp)
    print("- Current Unix Timestamp to Human Readable:", human_readable_timestamp)
    print("")
    print("- userid:", userid)
    print("- XOR Symmetric key:", xor_secret_key)
    print("- JWT Secret key:", jwt_secret_key)
    print("")
    print("- Encoded UserID and Timestamp:", encoded_userid_timestamp)
    print("- Decoded UserID and Hashed Timestamp:", decoded_userid + "|" + hashed_timestamp)
    print("")
    print("- JWT Token:", jwt_token)
    print("- Decoded JWT:", decoded_token)

if __name__ == "__main__":
    main()

https://github.com/password123456/some-tweak-to-hide-jwt-payload-values

© www.soinside.com 2019 - 2024. All rights reserved.