标签: encryption

Node JS crypto.createCipheriv 错误:密钥长度无效

我正在尝试使用 node.js 加密模块加密文本。

这是代码:

const crypto = require('crypto');

const password = 'password';
const key = crypto.scryptSync(password, 'salt', 24);

const iv = crypto.randomBytes(16);
const cipher = crypto.createCipheriv('aes-256-gcm', key, iv);
var encrypted = cipher.update("Hello", 'utf8', 'hex') + cipher.final('hex');

console.log(encrypted);
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

internal/crypto/cipher.js:103
    this[kHandle].initiv(cipher, credential, iv, authTagLength);
                  ^

Error: Invalid key length
[90m    at Cipheriv.createCipherBase (internal/crypto/cipher.js:103:19)[39m
[90m    at Cipheriv.createCipherWithIV (internal/crypto/cipher.js:121:20)[39m
[90m    at new Cipheriv (internal/crypto/cipher.js:225:22)[39m
[90m    at Object.createCipheriv (crypto.js:117:10)[39m
    at Object.<anonymous> (F:\Misc\App\MySQL-Buzzer-Electron\demo.js:7:23)
[90m    at Module._compile (internal/modules/cjs/loader.js:1156:30)[39m
[90m    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1176:10)[39m
[90m    at Module.load (internal/modules/cjs/loader.js:1000:32)[39m …
Run Code Online (Sandbox Code Playgroud)

javascript encryption cryptography node.js node-crypto

4
推荐指数
1
解决办法
7324
查看次数

使用 StringEncryptedType、SQLAlchemy 时出现 VARCHAR 长度错误

我有以下模型:

from enum import Enum
from sqlalchemy_utils.types.encrypted.encrypted_type import StringEncryptedType
from sqlalchemy import (
    Column,
    Integer,
    Float,
    Enum as SQAEnum,
    String
)
from backend.db import Base
from backend.config import Config


class AuthProviders(Enum):
    google = "Google"
    facebook = "Facebook"
    vest = "Vest"


class Users(Base):
    __tablename__ = "users"

    id = Column(Integer, primary_key=True)
    email = Column(String(255), unique=True, index=True)
    hashed_password = Column(StringEncryptedType(String(255), Config.MYSQL_ENCRYPT_KEY))
    auth_provider = Column(SQAEnum(AuthProviders))
    timestamp = Column(Float(precision=32))

Run Code Online (Sandbox Code Playgroud)

alembic 使用哪个生成以下迁移脚本:

from alembic import op
import sqlalchemy as sa
from sqlalchemy_utils.types.encrypted.encrypted_type import StringEncryptedType

from backend.config …
Run Code Online (Sandbox Code Playgroud)

python mysql encryption sqlalchemy alembic

4
推荐指数
1
解决办法
2273
查看次数

Delphi 7 - DCPCrypt - TDCP_rijndael - DecryptString - 如何使其工作?

我正在 PHP 中加密文本(openssl_encrypt / 'aes-256-cbc'),然后尝试在 Delphi 7 中解密(DCPCrypt / TDCP_rijndael)。

PHP脚本文件以ANSI编码保存,希望传输的字符串(它是REST API Web服务)与Delphi兼容。

然而,Delphi 解密产生了错误的结果,我猜测代码中有问题。如果您能看一下并发现我在 Delphi 方面的错误,我将不胜感激:

PHP代码:

function encrypt($key, $payload) {
    $iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('aes-256-cbc'));
    $encrypted = openssl_encrypt($payload, 'aes-256-cbc', $key, 0, $iv);
    return base64_encode($encrypted . '::' . $iv);
}

function decrypt($key, $garble) {
        list($encrypted_data, $iv) = explode('::', base64_decode($garble), 2);
        return openssl_decrypt($encrypted_data, 'aes-256-cbc', $key, 0, $iv);
}
Run Code Online (Sandbox Code Playgroud)

德尔福代码:

var
  DCP_rijndael: TDCP_rijndael;

const
  cPASSWORD = 'myownpassword';

function Decrypt(AStr: string): string;
var
  d, s, iv: String;
  p: Integer;
begin
  d := Base64DecodeStr(AStr);
  p …
Run Code Online (Sandbox Code Playgroud)

delphi encryption delphi-7 encryption-symmetric php-openssl

4
推荐指数
1
解决办法
2994
查看次数

如何在java中解码访问令牌并获取用户名和电子邮件?

我如何从令牌中获取姓名和电子邮件。

使用 jwt.io http://prntscr.com/yzyf2b的令牌结构

任何帮助表示赞赏。

在以下帖子的帮助下更新完整的解决方案,因此请归功于他们。

            String jwtToken = token;
            System.out.println("------------ Decode JWT ------------");
            String[] split_string = jwtToken.split("\\.");
            String base64EncodedHeader = split_string[0];
            String base64EncodedBody = split_string[1];
            String base64EncodedSignature = split_string[2];

            System.out.println("~~~~~~~~~ JWT Header ~~~~~~~");
            Base64 base64Url = new Base64(true);
            String header = new String(base64Url.decode(base64EncodedHeader));
            System.out.println("JWT Header : " + header);

            System.out.println("~~~~~~~~~ JWT Body ~~~~~~~");
            String body = new String(base64Url.decode(base64EncodedBody));
            System.out.println("JWT Body : " + body);

            JSONObject jsonObject = new JSONObject(body);
            System.out.println(jsonObject.get("email"));
            System.out.println(jsonObject.get("name"));
        
Run Code Online (Sandbox Code Playgroud)

java encryption base64 jwt keycloak

4
推荐指数
1
解决办法
4497
查看次数

如何在nodejs应用程序中创建相当于php代码的openssl加密和解密

我有一个在 php 上运行的应用程序,其中使用下面的代码使用 openssl encrption 加密了一些值

<?php
define('OSSLENCKEY','14E2E2D1582A36172AE401CB826003C1');
define('OSSLIVKEY', '747E314D23DBC624E971EE59A0BA6D28');

function encryptString($data) {
    $encrypt_method = "AES-256-CBC";
    $key = hash('sha256', OSSLENCKEY);    
    $iv = substr(hash('sha256', OSSLIVKEY), 0, 16); 
    $output = openssl_encrypt($data, $encrypt_method, $key, 0, $iv);
    $output = base64_encode($output);
    return $output;
}

function decryptString($data){
    $encrypt_method = "AES-256-CBC";
    $key = hash('sha256', OSSLENCKEY);    
    $iv = substr(hash('sha256', OSSLIVKEY), 0, 16);
    $output = openssl_decrypt(base64_decode($data), $encrypt_method, $key, 0, $iv);     
    return $output;
}

echo encryptString("Hello World");
echo "<br>";
echo decryptString("MTZHaEoxb0JYV0dzNnptbEI2UXlPUT09");
?>
Run Code Online (Sandbox Code Playgroud)

我有另一个在 nodejs 上运行的端点,我需要根据上面的 php 加密/解密规则来解密和加密值。我已经搜索过但找不到解决方案。

我尝试使用该库crypto但最终出现错误 …

php encryption openssl node.js node-crypto

4
推荐指数
1
解决办法
4501
查看次数

在现有 s3 存储桶上启用默认加密是否需要对存储桶用户进行任何 IAM 更改?

我想为现有存储桶启用默认加密(Amazon S3 托管加密密钥 (SSE-S3))。未来的对象将被加密,但要加密现有的对象,我需要复制它们或重新上传它们。

我想知道,由于我使用 SSE-S3 对存储桶进行默认加密,我是否需要进行任何 IAM 更改以允许存储桶用户访问现有对象和未来对象?或者在存储桶上启用默认加密 (SSE-S3) 不需要任何 IAM 更改,因为在静态存储数据之前应用加密?

感谢您的帮助。

encryption amazon-s3 amazon-web-services

4
推荐指数
1
解决办法
1989
查看次数

我可以在前端和后端对密码进行加密吗?

我正在制作一个需要登录的应用程序。

我使用node.js作为API。

我知道我必须在后端加密密码,以防数据库被盗。但是,如果由于某种原因 HTTPS 失败或者我们这边的某些开发人员有恶意,如果前端也没有加密,他/她就可以轻松窃取原始密码。

所以我的问题是,如果我在后端使用 Argon2 并在前端使用 BCrypt 会出现问题吗?

(另外我认为 Argon2 更快,所以如果攻击者想要暴力破解,他也必须使用慢速的,而不是我的 api 重载他/她的 shannigans)

我知道有一些类似的问题,但他们问我是否应该使用其中一个或另一个,答案是后端和 HTTPS。

我将使用 HTTPS 和后端加密。但我问是否也可以(或一个好主意)在前端使用加密。

encryption api-design

4
推荐指数
1
解决办法
1752
查看次数

使用 RSA/ECB/OAEPWithSHA-256AndMGF1Padding 在 Java 中加密,使用 OAEPSHA256 Padding 在 C# 中解密

我遇到过 Java 程序使用 RSA/ECB/OAEPWithSHA-256AndMGF1Padding 加密文本的情况。

我需要在 C# 中解密它。

Java 中的加密和解密工作正常。

使用 RSA/ECB/OAEPWithSHA-1AndMGF1Padding 在 Java 中加密并在 C# 中解密工作得非常好。

但是,使用 Java 中的 RSA/ECB/OAEPWithSHA-256AndMGF1Padding 加密和 C# 中的 OaepSHA256 解密会出现错误:参数不正确。

用于加密的Java代码:

public static String encrypt(KeyPair keypair, String data) throws NoSuchAlgorithmException, NoSuchPaddingException,
            InvalidKeyException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
        Cipher c = Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding");
        //Cipher c = Cipher.getInstance("RSA/ECB/OAEPWithSHA-1AndMGF1Padding");
        c.init(Cipher.ENCRYPT_MODE, keypair.getPublic());
        return Base64.getEncoder().encodeToString(c.doFinal(data.getBytes()));
    }
Run Code Online (Sandbox Code Playgroud)

解密的C#代码:

public string DecryptRsa(byte[] encryptedBytes, X509Certificate2 x509Certificate2, RSAEncryptionPadding rSAEncryptionPadding)
        {
            var text = string.Empty;

            using (RSACng csp = (RSACng)x509Certificate2.GetRSAPrivateKey())
            {

                byte[] bytesDecrypted = csp.Decrypt(encryptedBytes, rSAEncryptionPadding); …
Run Code Online (Sandbox Code Playgroud)

c# encryption sha256

4
推荐指数
1
解决办法
2559
查看次数

将低级 OpenSSL API 迁移到高级 OpenSSL API 所需的建议

我是 OpenSSL 的新手,目前正在开发一个依赖于 OpenSSL 的 C++ 项目。最近我开始使用一台安装了 openSSL 3.0 的机器,但无法构建项目,因为我收到如下所示的编译器错误(请注意,警告在项目中被视为错误)

error: 'MD5' is deprecated
    if (!MD5((uint8_t*)key.data(), key.size(), hashOutput)) {
         ^
/usr/local/Cellar/openssl@3/3.0.0_1/include/openssl/md5.h:52:1: note: 'MD5' has been explicitly marked deprecated here
OSSL_DEPRECATEDIN_3_0 unsigned char *MD5(const unsigned char *d, size_t n,
^
/usr/local/Cellar/openssl@3/3.0.0_1/include/openssl/macros.h:182:49: note: expanded from macro 'OSSL_DEPRECATEDIN_3_0'
#   define OSSL_DEPRECATEDIN_3_0                OSSL_DEPRECATED(3.0)
                                                ^
/usr/local/Cellar/openssl@3/3.0.0_1/include/openssl/macros.h:62:52: note: expanded from macro 'OSSL_DEPRECATED'
#     define OSSL_DEPRECATED(since) __attribute__((deprecated))
                                                   ^
1 error generated.
Run Code Online (Sandbox Code Playgroud)

经过一些调查,我发现这些 API 在 OpenSSL 3.0.0 中已被弃用,现在我有三个选择

  1. 忽略警告。它们只是警告。已弃用的函数仍然存在,您仍然可以使用它们。但请注意,它们可能会从 OpenSSL 的未来版本中删除。

  2. 抑制警告。有关如何执行此操作的信息,请参阅编译器文档。

  3. 删除对低级 API 的使用。在这种情况下,您需要重写代码以使用高级 API。

我选择了第三个选项,但即使在阅读了与 OpenSSL …

encryption hash openssl md5

4
推荐指数
1
解决办法
5747
查看次数

在 Go 中解密在 Python 中以 CFB 模式使用 AES 加密的内容

问题

\n

我希望能够在 Go 中解密在 Python 中加密的内容。加密/解密函数分别在每种语言中工作,但当我在 Python 中加密并在 Go 中解密时则不然,我猜测编码有问题,因为我得到了乱码输出:

\n
Rx\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbdd\xef\xbf\xbd\xef\xbf\xbdI\xef\xbf\xbdK|\xef\xbf\xbdap\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbdk\xef\xbf\xbd\xef\xbf\xbdB%F\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbdUV\xef\xbf\xbd~d3h\xef\xbf\xbd\xc3\x91\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbd|\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbd>\xef\xbf\xbdB\xef\xbf\xbd\xef\xbf\xbdB\xef\xbf\xbd\n
Run Code Online (Sandbox Code Playgroud)\n

Python 中的加密/解密

\n
def encrypt(plaintext, key=config.SECRET, key_salt='', no_iv=False):\n    """Encrypt shit the right way"""\n\n    # sanitize inputs\n    key = SHA256.new((key + key_salt).encode()).digest()\n    if len(key) not in AES.key_size:\n        raise Exception()\n    if isinstance(plaintext, string_types):\n        plaintext = plaintext.encode('utf-8')\n\n    # pad plaintext using PKCS7 padding scheme\n    padlen = AES.block_size - len(plaintext) % AES.block_size\n    plaintext += (chr(padlen) * padlen).encode('utf-8')\n\n    # generate random initialization vector using CSPRNG\n    if no_iv:\n        iv = ('\\0' * …
Run Code Online (Sandbox Code Playgroud)

python encryption encoding aes go

4
推荐指数
1
解决办法
1214
查看次数