您好,我需要帮助使用 crypto 模块将我的 PHP 加密函数转换为 Nodejs:
这段代码已经可以运行了
// Constructor params
$this->algorithm = "blowfish";
$this->token = "3SzzaErRzj0#RuGr@JTkh[MO0AMIW*d!Sul/CEL!*rPnq$oOEgYaH}fNw{jw1b/DyLUdL])+JOMES@Z7MIRI>(p*nY{yl%h]4ylx";
public function decrypt($string)
{
$key = hash('sha256', $this->token);
list($encrypted_data, $iv) = explode('::', base64_decode($string), 2);
return openssl_decrypt($encrypted_data, $this->algorithm, $key, 0, $iv);
}
public function encrypt($string)
{
$output = false;
$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length($this->algorithm));
// hash
$key = hash('sha256', $this->token);
$output = openssl_encrypt($string, $this->algorithm, $key, 0, $iv);
return base64_encode($output . '::' . $iv);
}
Run Code Online (Sandbox Code Playgroud)
和nodejs中的代码,我使解密工作正常,但密码不工作
const crypto = require('crypto');
const decipher = async (alg, key, value) => …Run Code Online (Sandbox Code Playgroud) 我已经实现了 CBC 模式 AES 加密和解密机制,其中我为每次推荐的加密尝试生成随机 IV 和随机密钥。
\n\n现在,我已将密钥保存在一个单独的文件中,将 IV 保存在另一个文件中,但是在浏览不同的论坛后,我发现 IV 不应该保持安全,并且应在加密时和解密时附加密文我们可以从该密码字节数组中取出 16 个字节。
\n\n现在,我尝试了一段代码来实现相同的目的,但结果并不好,因为第一个块没有正确加密;然而该块的其余部分却如此。
\n\n有人可以告诉我我的方法有什么问题吗?
\n\n任何帮助将不胜感激,谢谢:)。
\n\npublic static byte[] encrypt (byte[] plaintext,SecretKey key,byte[] IV ) throws Exception {\n\n //Get Cipher Instance\n Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");\n\n //Create SecretKeySpec\n SecretKeySpec keySpec = new SecretKeySpec(key.getEncoded(), "AES");\n\n //Create IvParameterSpec\n IvParameterSpec ivSpec = new IvParameterSpec(IV);\n\n System.out.println( "IV encrypt= " + ivSpec );\n\n //Initialize Cipher for ENCRYPT_MODE\n cipher.init(Cipher.ENCRYPT_MODE, keySpec);\n\n //Perform Encryption\n byte[] cipherText = cipher.doFinal(plaintext);\n\n ByteArrayOutputStream b = new ByteArrayOutputStream();\n\n …Run Code Online (Sandbox Code Playgroud) 在 Linux 机器上,我有一个使用 openssl 加密的 MP4 视频:
openssl enc -aes-128-ecb -a -in video.mp4 -out video.enc -K `cat aes.key`
Run Code Online (Sandbox Code Playgroud)
请注意,这是一个练习,算法的强度并不重要。
该文件被发送到 Android 应用程序,我正在尝试使用 ExoPlayer 播放它。
我事先对文本文件进行了一些测试,以确保解密正常工作
fun decrypt(key: ByteArray, data: ByteArray): ByteArray {
val spec = SecretKeySpec(key, "AES")
val cipher = Cipher.getInstance("AES/ECB/PKCS5Padding")
cipher.init(Cipher.DECRYPT_MODE, spec)
globalCipher.init(Cipher.DECRYPT_MODE, spec)
return cipher.doFinal(data)
}
Run Code Online (Sandbox Code Playgroud)
AesCipherDataSource关于ExoPlayer,在、AesCipherDataSink等之间有点让人不知所措。SimpleCache我无法组合出一个简单的方法来播放视频。
fun playVideo() {
val player = SimpleExoPlayer.Builder(this).build()
playerView.player = player
val dataSourceFactory = DefaultDataSourceFactory? // <-- what's the factory?
val dataSource = AesCipherDataSource(globalCipher, ?) // <-- …Run Code Online (Sandbox Code Playgroud) 我在使用 Java 密码学时遇到以下问题。
错误 decrjavax.crypto.BadPaddingException:给定的最终块未正确填充。如果在解密过程中使用错误的密钥,可能会出现此类问题。
我检查了所有可能的答案,但找不到这背后的确切原因。
一项观察结果是,当我使用 AES/CBC/NoPadding 代替 AES/CBC/PKCS5Padding 时,我可以成功执行它。
这是我的代码片段。
package demo;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;
public class TestEncryption {
private static final int BUFFER_SIZE = 32;
private static final int KEY_ITERATIONS = 65535;
private static final int DEFAULT_KEY_BITS = …Run Code Online (Sandbox Code Playgroud) 我正在加密用户 ID,crypto-js通过 url 发送它,如下所示,以便用户在单击时验证它:
http://localhost:3000/api/customer/activate/U2FsdGVkX1DXzzLuf9TgBf31Mc2V/QBVAN05PovlNM
这是加密的用户 ID:
U2FsdGVkX1DXzzLuf9TgBf31Mc2V/QBVAN05PovlNM
使用加密js encrypt:
crypto.AES.encrypt(term.toString(), config.CRYPTO_PASSPHRASE_RES).toString(crypto.enc.Utf8)
Run Code Online (Sandbox Code Playgroud)
当我运行 API 时,它返回route not found,当我/从加密的用户 ID 中删除时,它起作用了。
如何防止 crypto-js 添加/到加密 ID 中?
我有一个遗留数据库,其内容是使用 DES 通过 mcrypt 加密的(是的,我知道,那是很久以前的事了)加密方法如下:
/**
* General encryption routine for generating a reversible ciphertext
* @param String $string the plain text to encrypt
* @param String $key the encryption key to use
* @return String the cypher text result
*/
function encrypt($string, $key)
{
srand((double) microtime() * 1000000);
/* Open module, and create IV */
$td = mcrypt_module_open('des', '', 'cfb', '');
$ksub = substr(md5($key), 0, mcrypt_enc_get_key_size($td));
$iv_size = mcrypt_enc_get_iv_size($td);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
/* Initialize encryption handle */
if …Run Code Online (Sandbox Code Playgroud) 我尝试在 C# 中解密的数据在 Nodejs 中使用 AES-256 算法进行加密,代码如下。
const crypto = require('crypto');
const validator = require('validator');
const algorithm = 'aes256';
const inputEncoding = 'utf8';
const outputEncoding = 'hex';
const iv = crypto.randomBytes(16)
function encrypt(key,text) {
key = processKey(key);
let cipher = crypto.createCipheriv(algorithm, key, iv);
let ciphered = cipher.update(text, inputEncoding, outputEncoding);
ciphered += cipher.final(outputEncoding);
return ciphered;
}
Run Code Online (Sandbox Code Playgroud)
现在,我获得了长度为 32 的加密数据(如“1234567304e07a5d2e93fbeefd0e417e”)和长度为 32 的密钥(如“123456673959499f9d37623168b2c977”)。
我尝试使用下面的 C# 代码解密相同的内容,但收到错误“要解密的数据长度无效”。请提供建议。
public static string Decrypt(string combinedString, string keyString)
{
string plainText;
byte[] combinedData = StringToByteArray(combinedString);
Aes aes = …Run Code Online (Sandbox Code Playgroud) 我从这里开始写:
但由于我在这里的声誉,我无法添加评论(我在 AskUbuntu 上表现更好,但我不能从那里获取我的代表点)。我还在那里开始了赏金,如果有人在两天内在这里回答并提供可接受的解决方案,我将在那里奖励。
现在,问题是:SetProtection 方法没有按预期工作。
想要的行为:使用 TCPDF 库创建受保护/加密的 PDF 文档,以便始终向每个人授予文档视图而无需询问任何密码,但如果尝试编辑,则需要密码。
我使用以下语法:
$pdf->SetProtection(array('修改', '复制', '注释表单', '填充表单', '提取', '汇编'), null, 'mypwd', 1);
正确的语法是什么(如果有),让每个人都可以阅读 pdf,但只能使用提供的“mypwd”进行编辑?
编辑:
您将看到一个带有空白用户密码和强主密码的文件。Ilovepdf.com 发现它已解锁,Libreoffice Draw 可以对其进行编辑。这不是预期的行为。
https://www.dropbox.com/s/864p8xjh1ue041z/tracking_12750_16.pdf?dl=0
我正在尝试使用 CryptoJS 加密 32 个字符的字符串:
var string = '12345678901234567890123456789012'.
Run Code Online (Sandbox Code Playgroud)
我使用 AES256 的 32 个字符的密钥:
var key = '12345678901234567890123456789012':
var _key = CryptoJS.enc.Utf8.parse(key);
var encryptedECB = CryptoJS.AES.encrypt(string.trim(), _key, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
}).toString();
Run Code Online (Sandbox Code Playgroud)
我预计加密的 ECB 字符串长度为 44 个字符。相反,它是 64 个字符。我做错了什么?
我想通过 GitHub REST API 设置存储库机密。我使用文档中的示例:
const sodium = require('tweetsodium');
const key = "base64-encoded-public-key";
const value = "plain-text-secret";
// Convert the message and key to Uint8Array's (Buffer implements that interface)
const messageBytes = Buffer.from(value);
const keyBytes = Buffer.from(key, 'base64');
// Encrypt using LibSodium.
const encryptedBytes = sodium.seal(messageBytes, keyBytes);
// Base64 the encrypted secret
const encrypted = Buffer.from(encryptedBytes).toString('base64');
console.log(encrypted);
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
(node:6008) UnhandledPromiseRejectionWarning: Error: bad public key size
at checkBoxLengths (C:\Users\User\probot\node_modules\tweetnacl\nacl-fast.js:2158:54)
at Function.nacl.box.before (C:\Users\User\probot\node_modules\tweetnacl\nacl-fast.js:2231:3)
at Object.nacl.box (C:\Users\User\probot\node_modules\tweetnacl\nacl-fast.js:2225:20)
at Object.tweetSodium.seal (C:\Users\User\probot\node_modules\tweetsodium\dist\index.umd.js:53:33)
at …Run Code Online (Sandbox Code Playgroud) encryption ×10
node.js ×4
aes ×2
cryptography ×2
cryptojs ×2
java ×2
javascript ×2
php ×2
.net ×1
android ×1
c# ×1
exoplayer2.x ×1
express ×1
github ×1
libsodium ×1
padding ×1
pdf ×1
tcpdf ×1