- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我在使用 CryptoJS 解密在 Go 语言中加密的文本时遇到问题。
这是 Go 代码: https://play.golang.org/p/xCbl48T_iN
package main
import (
"crypto/aes"
"crypto/cipher"
"encoding/base64"
"fmt"
)
func main() {
key := []byte("1234567890123456")
plaintext := []byte("text can be a random lenght")
block, err := aes.NewCipher(key)
if err != nil {
panic(err)
}
// The IV needs to be unique, but not secure. Therefore it's common to
// include it at the beginning of the ciphertext.
// BTW (only for test purpose) I don't include it
ciphertext := make([]byte, len(plaintext))
iv := []byte{'\x0f','\x0f','\x0f','\x0f','\x0f','\x0f','\x0f','\x0f','\x0f','\x0f','\x0f','\x0f','\x0f','\x0f','\x0f','\x0f'}
stream := cipher.NewCTR(block, iv)
stream.XORKeyStream(ciphertext, plaintext)
// CTR mode is the same for both encryption and decryption, so we can
// also decrypt that ciphertext with NewCTR.
base := base64.StdEncoding.EncodeToString(ciphertext)
fmt.Printf("encodedHEX: %x\n", ciphertext)
fmt.Printf("encodedBASE: %s\n", base)
plaintext2 := make([]byte, len(plaintext))
stream = cipher.NewCTR(block, iv)
stream.XORKeyStream(plaintext2, ciphertext)
fmt.Printf("decoded: %s\n", plaintext2)
}
JS代码如下: http://jsfiddle.net/Ltkxm64n/
var key = CryptoJS.enc.Hex.parse('31323334353637383930313233343536');
var iv = CryptoJS.enc.Hex.parse('0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f');
var encrypted = CryptoJS.AES.encrypt("text can be a random lenght", key, {
mode: CryptoJS.mode.CTR,
iv: iv
});
console.log(encrypted.ciphertext.toString());
console.log(encrypted.toString());
var decrypted = CryptoJS.AES.decrypt(encrypted, key, {
mode: CryptoJS.mode.CTR,
iv: iv
});
console.log(decrypted.toString(CryptoJS.enc.Utf8));
// text can be a random lenght
两者都可以很好地加密和解密,但是当我将 base64 密文从 GO 复制到 JS(或反之亦然)时,它不起作用。我还注意到 js 输出的第一部分与 Go 输出相同,但是 js 输出中的字节数比 Go 多。
我的目的是在GO中加密一些文本,然后将Base64密文发送给可以解密的JS。
谢谢
最佳答案
好的,这是解决此问题的方法:
将无填充 js 添加到您的源列表:http://crypto-js.googlecode.com/svn/tags/3.1/build/components/pad-nopadding.js
加密/解密时指定参数:padding: CryptoJS.pad.NoPadding
CTR模式不需要在加密前填充明文。
从多个 AES block 生成的 key 流在 XORing 之前被 trim 以匹配纯文本长度。
看起来 CryptoJS 生成 key 流以 xor
它与纯文本但不 trim 它,因为 CryptoJS 生成的密文长度没有 padding: CryptoJS.pad.NoPadding
总是16 字节的倍数(正好是 AES block 大小)。
var key = CryptoJS.enc.Hex.parse('31323334353637383930313233343536');
var iv = CryptoJS.enc.Hex.parse('0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f');
var encrypted = CryptoJS.AES.encrypt("text can be a random lenght", key, {
mode: CryptoJS.mode.CTR,
iv: iv,
padding: CryptoJS.pad.NoPadding
});
document.getElementById("id").innerHTML = encrypted.ciphertext.toString();
document.getElementById("id2").innerHTML = encrypted.toString();
var decrypted = CryptoJS.AES.decrypt(encrypted, key, {
mode: CryptoJS.mode.CTR,
iv: iv,
padding: CryptoJS.pad.NoPadding
});
document.getElementById("decrypt").innerHTML = decrypted.toString(CryptoJS.enc.Utf8); // text can be a random lenght
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/aes.js"></script>
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/mode-ctr.js"></script>
<script src="http://crypto-js.googlecode.com/svn/tags/3.1/build/components/pad-nopadding.js"></script>
<p> Ciphertext in HEX: </p>
<p id="id"> </p>
<p> Ciphertext in BASE64: </p>
<p id="id2"> </p>
<p> PlainText: </p>
<p id="decrypt"></p>
关于javascript - AES-CTR 在 Go 中加密并在 CryptoJS 中解密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36909746/
我有固定宽度的三列的页面布局。请参阅以下 HTML 和 CSS 片段 在显示器的某些分辨率下(尤其是在 firefox 中),当我缩放页面 (CTR-) 时,模板页面正在折叠。我找不到解决此问题的方法
我的问题是我无法从下面的 C 代码中获取 AES 256 CTR 输出以匹配下面 OpenSSL 命令的输出。 C 代码产生这个: 5f b7 18 d1 28 62 7f 50 35 ba e9 6
给定一个简单的 template struct X { T x, y; }; ,我想提供转换构造函数,以便用户可以编写: X a; X b = a; // uses implicit convers
我正在使用这个 example尝试使用 AES-256 加密数据。但是,当我使用密码 WeakPasswordForTesting 作为输入时,出现错误:crypto/aes: invalid key
有人在 windows 上尝试过 microk8s(我只需要在我一直使用 debian 的进程中使用它)我在将图像导入 microk8s 时出错 命令:microk8s ctr image impor
什么是“ctr”文件扩展名?最近我发现一个网站有一个名为“create.ctr”的文件,那是什么语言? 最佳答案 文件扩展名 .CTR 详细信息 http://filext.com/file-exte
所以我有这个代码,它基本上加密两个纯文本消息,然后尝试解密它们,然后打印。问题是第一条消息恢复得很好,但第二条消息是垃圾。我从 this tutorial 下载了这段代码,然后将其修改为使用字符串而不
有人能告诉我衡量访问主页并观看视频然后访问同一网站的另一个页面的用户与访问网站但不观看视频的用户的最佳方法是什么。我想提供一个衡量标准,显示观看视频然后转到同一网站其他地方的用户与未观看视频的用户。我
我有我的服务特定图像的 tar 。我将它导入到 containerd 中,以便 k3s 使用它来部署 POD。用于导入图像的 tar 的命令是- k3s ctr images import XXX.t
我想知道这段代码的作用。我所知道的是,循环(加 1)将在 ctr==20 时停止,但是 if(ctr%2) 这里意味着什么? sum=0; for(ctr=0; ctr< 20; ct
已结束。此问题正在寻求书籍、工具、软件库等的推荐。它不满足Stack Overflow guidelines 。目前不接受答案。 我们不允许提出寻求书籍、工具、软件库等推荐的问题。您可以编辑问题,以便
我正在尝试自己实现 CTR 模式(目前仅解密),仅使用来自 pycrypto 的 AES 内置函数。这意味着我不应该使用 mode=AES.MODE_CTR。不过,我知道使用 AES.MODE_CTR
我正在实现一个小型演示应用程序,它使用 AES CTR 和 OpenSSL 进行加密,是否可以使用不同的操作模式测试向量来测试算法,例如在我的应用程序中测试 ECB 向量并检查结果,或者它是否仅限于
我正在使用以下工作代码来解密文件: #include #include #include struct ctr_state { unsigned char ivec[16]; u
我试图了解使用 CTR 模式的加密是如何工作的,所以我创建了这些函数来测试它: import ( "crypto/cipher" "crypto/rand" ) // generate
当我想用快捷键 ctrl + / 注释或注释掉我的代码时,IntelliJ 将 // 放在第一列而不是电流。当前如何发表评论? 例子 System.out.println(set.co
我有一个使用 libgcrypt 的 CTR 模式实现用 AES-256 加密的文件。 我希望能够分部分解密文件(例如,在不解密整个文件的情况下解密 20 个块中的 5-10 个块)。 我知道通过使用
ctr 模式可以将分组密码用作流密码,但这种模式下的加密强度如何? 最佳答案 最终,这取决于你所说的强是什么意思。例如,从加密的角度来看,即利用攻击者在不访问 key 的情况下解密您的密文的能力,它应
在 Delphi 项目中,CTRL + Space 不起作用。在“使用”之前,当我按 CTRL + Space 时,会出现代码完成,但在“使用”之后不会出现。我使用德尔福 2009。我安装了 Fast
我写了一个小的 java 脚本代码,它用有效的 url 替换 anchor 标记的 url。我的听众的代码 document.addEventListener("click", function (e
我是一名优秀的程序员,十分优秀!