- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有使用 AES/CBC 的 java 代码,我需要用 perl 重写。我的目标是在两种代码中重现相同的结果。可能吗?
import java.nio.charset.Charset;
import java.security.GeneralSecurityException;
import java.security.InvalidKeyException;
import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.lang3.StringUtils;
public class GenPass {
public static void main(String[] args) {
String secret = "secret"; /* same secret */
String password = args[0];
if (args.length > 1) {
secret = args[1];
}
System.out.println("Using secret: [" + secret + "]");
System.out.println("Password [" + password + "]");
String encodePassword = new GenPass().encodePassword(password, secret);
System.out.println("Encoded password [" + encodePassword + "]");
}
private String encodePassword(String password, String secret) {
try {
final SecretKeySpec secretKey = new SecretKeySpec(this.decodeHex(secret.toCharArray()), "AES"); /* same set*/
final Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding"); // same set here
cipher.init(Cipher.ENCRYPT_MODE, secretKey, new IvParameterSpec(new byte[16]));
String paddedString = StringUtils.left(StringUtils.rightPad(password, 16, (char) 0), 16);
byte[] encryptedBytes = cipher.doFinal(paddedString.getBytes(Charset.forName("UTF-8")));
return Base64.getEncoder().encodeToString(encryptedBytes);
} catch (InvalidKeyException invalidKeyException) {
System.out.println("Chave incorreta durante criptografia da senha" + invalidKeyException);
throw new RuntimeException(invalidKeyException);
} catch (GeneralSecurityException generalSecurityException) {
System.out.println("Falha inesperada durante criptografia da senha" + generalSecurityException.toString());
throw new RuntimeException(generalSecurityException);
}
}
private String normalize(String password) {
char[] array = new char[16];
char[] temp = password.toCharArray();
System.arraycopy(temp, 0, array, 0, 16);
return String.valueOf(array);
}
public static byte[] decodeHex(final char[] data) {
final int len = data.length;
if ((len & 0x01) != 0) {
throw new RuntimeException("Odd number of characters.");
}
final byte[] out = new byte[len >> 1];
// two characters form the hex value.
for (int i = 0, j = 0; j < len; i++) {
int f = toDigit(data[j], j) << 4;
j++;
f = f | toDigit(data[j], j);
j++;
out[i] = (byte) (f & 0xFF);
}
return out;
}
protected static int toDigit(final char ch, final int index) {
final int digit = Character.digit(ch, 16);
if (digit == -1) {
throw new RuntimeException("Illegal hexadecimal character " + ch + " at index " + index);
}
return digit;
}
}
结果不一样。我无法弄清楚错误在哪里...
#!/usr/bin/perl -w
use strict;
use warnings;
use Crypt::CBC;
use MIME::Base64;
use Digest::MD5 qw(md5_hex);
my $plainText = "textsample";
my $iv = " ";
my $key = "secret";
my $cipher = Crypt::CBC->new(
-key => $key,
-iv => $iv,
-cipher => 'OpenSSL::AES',
-literal_key => 0,
-header => "none",
-padding => "null",
-keysize => 16
);
my $encrypted = $cipher->encrypt($plainText);
my $base64 = encode_base64($encrypted);
chomp $base64;
print("The '$plainText' encrypted is '$base64'\n");
最佳答案
我修复了,问题出在 -iv 参数上。
my $iv = ("\x0") x 16;
关于java - 基于 Java 代码的 Crypt::CBC 的 Perl 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33426033/
我只想使用这 3 种模式从 openSSL 测试 AES: key 长度为 128,192 和 256,但我的解密文本与我的输入不同,我不知道为什么。此外,当我传递一个巨大的输入长度(比如说 1024
我正在使用 CBC 求解器在 PyCharm 上解决优化问题,但一直遇到此错误: 错误:求解器 (cbc) 返回非零返回代码 (3221225781) 和 pyutilib.common._excep
Java AES/CBC/PKCS5PADDING 函数 public static String encrypt_key_data(String password, String message)
我安装了 Laravel 并将其上传到 git现在我下载了但是当我尝试进入该网站时,出现此错误: The only supported ciphers are AES-128-CBC and AES-
我对 C# 和加密比较陌生,所以请多多包涵。我正在将一些数据写入文件,我想使用 Aes (CBC) 对其进行加密,但我每天都会获取新数据,我想每周、每月、每年将所有数据写入一个文件,这取决于在内容上。
我在解密之前在 PHP 应用程序中加密的 pgcrypto 数据时遇到问题。 我尝试了 3 种加密方式: 1) mcrypt - RIJNDAEL 128 CBC 2) mcrypt - RIJNDA
Laravel 5.3(在 Windows 服务器上)我也有同样的问题。我做了所有可能的尝试:检查 .env 文件、配置、 artisan 新 key 生成、缓存:清除、配置:清除、 Composer
我目前正在使用 AES/CBC/PKCS5Padding 来加密具有 256 字节 key 大小的 Java 文件,但在搜索时我在 stackexchange PKCS#5-PKCS#7 Paddin
我使用 Composer 安装了 Laravel 5.3 的新副本,但收到此错误: The only supported ciphers are AES-128-CBC and AES-256-CBC
这是我的网络应用程序的 Laravel 5.4 设置。页面加载时重复发生一件事。因此,我无法在我的页面上获取数据。 运行时异常:唯一受支持的密码是具有正确 key 长度的 AES-128-CBC 和
我想知道在下面的代码中是否添加了 PKCS#5 填充?如果不是如何添加? $message = "insert plaintext message here"; $iv = pack('H*', '
我正在根据解释实现希尔密码 Wikipedia 但我想使用 CBC 模式来实现它,该模式表示每个 block 必须与前一个 block 进行异或,那么第一个 block 怎么样?学士学位每个 bloc
我在 PL/SQL 中有一个用于 aes256cbc(使用 PAD_PKCS5)的加密和解密函数,这似乎工作正常。 DECLARE -- https://docs.oracle.com/data
我正在从事一个 HIPAA 项目,我注意到我需要加密所有包含或可能包含私有(private)健康信息的列级数据。也就是说,我希望在 block_encryption_mode=aes-256-cbc
我正在尝试在 Linux 上开发一种安全程序。所以,我目前的计划是使用 AES128bit-CBC。我听说 OSX 基本上支持 AES 128。 Linux 上有这样的库吗? 最佳答案 libcryp
我在标准 ecb 模式下使用 nettle 的 twofish 没有问题,但是我不确定这个 cbc 模式有什么问题?解密的消息将与原始消息不匹配。 (仅出于测试目的使用一些硬编码值,如 iv)。 ht
最近项目需要选择一套对称加密算法,来满足前后端之间的加解密操作。初步打算前端使用crypto-js来实现,后端使用java本身的加密算法实现,但遇到了一个问题:java本身只支持NoPadding和
帮助,谁能给我一个 RSA/CBC 的 Java 实现? 审计署表示 RSA/ECB/PKCS1Padding 太弱,无法提交法庭。 (他实际上是在挑战 Java PKI 吗?他建议使用 AES...
我目前正在学习加密,我希望能找到更多关于我学到的东西的说明。 假设消息“100 美元应该从账户 123456 转移到 555555”是使用 aes-128-cbc 和随机 IV 加密的。我的教授说可以
我的 php 脚本和我的 c# 应用程序将相互传递一个 32 个字符长的哈希字符串,最好的模式是什么?我认为是欧洲央行,但我不确定是否使用超过 1 个区 block 就不要使用。我怎么知道 block
我是一名优秀的程序员,十分优秀!