- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我想在我的平台上使用 Speck 分组密码进行加密。https://en.wikipedia.org/wiki/Speck_(cipher) 上提供了一些 C 语言代码,但它们不提供解密。
目前,我修改了代码以实现 Speck 64/128(64 位 block 和 128 位 key )并获得(假设)成功加密,现在我想再次解密它。 key 是硬编码的,这只是为了进行基准测试。
#include <stdint.h>
#define ROR(x, r) ((x >> r) | (x << (32 - r)))
#define ROL(x, r) ((x << r) | (x >> (32 - r)))
#define R(x, y, k) (x = ROR(x, 8), x += y, x ^= k, y = ROL(y, 3), y ^= x)
#define ROUNDS 27
void speckEncrypt(uint32_t pt[2], uint32_t ct[2], uint32_t K[2]) {
uint32_t y = pt[0], x = pt[1], b = K[0], a = K[1];
R(x, y, b);
for (int i = 0; i < ROUNDS - 1; i++) {
R(a, b, i);
R(x, y, b);
}
ct[0] = y;
ct[1] = x;
}
void speckDecrypt(uint32_t pt[2], uint32_t ct[2], uint32_t K[2]) {
}
static void speck(uint32_t pt[2]){
uint32_t ct[2];
uint32_t K[4] = {123456789,123456789,123456789,123456789};
printf("Plaintext x: %lu", pt[0]);
printf(", Plaintext y: %lu \n", pt[1]);
printf("Get key schedule \n");
speckEncrypt(pt, ct, K);
printf("Encrypted encr_x: %lu ",ct[0]);
printf(", Encrypted encr_y: %lu \n", ct[1]);
// speckDecrypt(pt, ct, K);
// printf("Decrypted x: %lu", pt[0]);
// printf(", Decrypted y: %lu \n", pt[1]);
}
我没有成功实现 speckDecrypt。我尝试四处寻找其他解决方案,但始终失败(例如 https://www.multos.com/forums/viewthread/97 )。我对加密没有经验,所以有人可以帮助我吗?
编辑:
我也对使用键扩展的 Java 实现做了同样的事情。这个也可以加上吗?据我所知,Java 版本有效。
public void speck(int subm_x,int subm_y){
byte n = 32; // Word size
byte m = 4; // # of key words
byte T = 27; // Number of rounds
int[] l; // Used in the key generation
int[] k; // Stores subkeys
int x; // Encrypted x
int y; // Encrypted y
byte alpha = 8; // Number of shifts, function of n
byte beta = 3; // Number of shifts, function of n
k = new int[T];
l = new int[2*T];
k[0] = 123456789; //faux random number. Max Int is 2,147,483,647
k[1] = 123456789; //faux random number. Max Int is 2,147,483,647
k[2] = 123456789; //faux random number. Max Int is 2,147,483,647
k[3] = 123456789; //faux random number. Max Int is 2,147,483,647
l[m-4] = 1123456789;
l[m-3] = 1113456789;
l[m-2] = 1111456789;
x = subm_x;
y = subm_y;
/* *************** KEY EXTENSTION ***************** */
for(int i = 0; i < T-1; i++) {
l[i+m-1] = (k[i] + rotateRight(l[i], alpha)) ^ i;
k[i+1] = rotateLeft(k[i], beta) ^ l[i+m-1];
}
/* *************** ENCRYPTION ********************* */
for(int i = 0; i < T; i++) {
x = (rotateRight(x, alpha) + y) ^ k[i];
y = rotateLeft(y, beta) ^ x;
}
/* *************** DECRYPTION ********************* */
for(int i = T-1; i >= 0; i--) {
y = rotateRight(x ^ y, beta);
x = rotateLeft((x ^ k[i]) - y, alpha);
}
}
目的是将它们相互作为基准。
最佳答案
我在 https://github.com/madmo/speck/blob/master/speck.c 上找到了可靠的实现并使用非组合版本(不确定差异)。
这是有效的,希望可以帮助其他人!
根据要求,我的实现(作为 Contiki 进程):
#include <stdint.h>
#define SPECK_TYPE uint32_t
#define SPECK_ROUNDS 27
#define SPECK_KEY_LEN 4
#define ROR(x, r) ((x >> r) | (x << ((sizeof(SPECK_TYPE) * 8) - r)))
#define ROL(x, r) ((x << r) | (x >> ((sizeof(SPECK_TYPE) * 8) - r)))
#ifdef SPECK_32_64
#define R(x, y, k) (x = ROR(x, 7), x += y, x ^= k, y = ROL(y, 2), y ^= x)
#define RR(x, y, k) (y ^= x, y = ROR(y, 2), x ^= k, x -= y, x = ROL(x, 7))
#else
#define R(x, y, k) (x = ROR(x, 8), x += y, x ^= k, y = ROL(y, 3), y ^= x)
#define RR(x, y, k) (y ^= x, y = ROR(y, 3), x ^= k, x -= y, x = ROL(x, 8))
#endif
void speck_expand(SPECK_TYPE const K[static SPECK_KEY_LEN], SPECK_TYPE S[static SPECK_ROUNDS])
{
SPECK_TYPE i, b = K[0];
SPECK_TYPE a[SPECK_KEY_LEN - 1];
for (i = 0; i < (SPECK_KEY_LEN - 1); i++)
{
a[i] = K[i + 1];
}
S[0] = b;
for (i = 0; i < SPECK_ROUNDS - 1; i++) {
R(a[i % (SPECK_KEY_LEN - 1)], b, i);
S[i + 1] = b;
}
}
void speck_encrypt(SPECK_TYPE const pt[static 2], SPECK_TYPE ct[static 2], SPECK_TYPE const K[static SPECK_ROUNDS])
{
SPECK_TYPE i;
ct[0]=pt[0]; ct[1]=pt[1];
for(i = 0; i < SPECK_ROUNDS; i++){
R(ct[1], ct[0], K[i]);
}
}
void speck_decrypt(SPECK_TYPE const ct[static 2], SPECK_TYPE pt[static 2], SPECK_TYPE const K[static SPECK_ROUNDS])
{
SPECK_TYPE i;
pt[0]=ct[0]; pt[1]=ct[1];
for(i = 0; i < SPECK_ROUNDS; i++){
RR(pt[1], pt[0], K[(SPECK_ROUNDS - 1) - i]);
}
}
PROCESS_THREAD(eval_crypto_process, ev, data)
{
PROCESS_BEGIN();
printf("eval_crypto_process\n");
while(1){
printf("Waiting.\n");
static struct etimer timer;
etimer_set(&timer, 5*CLOCK_CONF_SECOND);
PROCESS_WAIT_UNTIL(etimer_expired(&timer));
printf("Starting crypto.\n");
PORTB ^= _BV(PB5);
PORTB ^= _BV(PB6);
uint32_t plain[2] = {987654321,987654321};
uint32_t key[4] = {123456789, 123456789, 123456789, 123456789};
SPECK_TYPE buffer[2] = {0};
SPECK_TYPE enc[2] = {0};
SPECK_TYPE exp[SPECK_ROUNDS];
speck_expand(key, exp);
// printf("Plaintext x: %lu", plain[0]);
// printf(", Plaintext y: %lu \n", plain[1]);
speck_encrypt(plain, enc, exp);
// printf("Encrypted encr_x: %lu ",enc[0]);
// printf(", Encrypted encr_y: %lu \n", enc[1]);
speck_decrypt(enc, buffer, exp);
// printf("Decrypted x: %lu", buffer[0]);
// printf(", Decrypted y: %lu \n", buffer[1]);
PORTB ^= _BV(PB5);
PORTB ^= _BV(PB6);
printf("finished crypto.\n");
}
PROCESS_END();
}
关于c - 停留在 C 中的 Speck 解密算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37701601/
我有一个 C# 应用程序调用 Java 网络服务来验证用户密码。我想让 C# 应用程序加密密码,然后让 Java Web 服务解密密码。我已经完成了 Java 端的代码(解密代码),但我无法找出 C#
我正在使用以下代码在使用 openssl 的 Windows 中使用 C 加密和解密二进制数据。如您所见,在这两个函数中,我都知道纯文本的大小。有什么方法可以在不知道纯文本大小的情况下解密消息? #i
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我有一个非常恼人的问题,Java中使用RSA算法对字符串进行不可靠的加密和解密。它似乎只能在大约 35% 的时间内工作,而且我不明白为什么它有时能工作,有时却不能。这是我写的一些测试代码,试图验证加密
我已经设法编写了用于文件加密/解密的函数。但它非常慢,尤其是随着文件大小的增加。例如几MB长的音频/视频文件 我几乎浏览了所有帖子来改进它,并尝试更改算法。如果有任何更改可以帮助我提高性能,请帮助我。
我正在尝试让我的转置密码发挥作用。 每当我将加密方法得到的密文输入解密方法时,我应该得到原始的明文......但事实并非如此...... 我做错了什么? 感谢您的帮助! public String E
我正在使用密码来加密和解密消息: public String encrypt(String string) throws InvalidKeyException, IllegalBlockSizeEx
我有一个在 MySQL 中存储数据的 spring-mvc 堆栈。其中一些数据需要保护,所以我想我应该加密它。由于我以后可能需要使用这些数据(信用卡、SSN 等),所以我需要对其进行解密。我认为这排除
作为一名SEOER,都想了解百度算法,通过算法原理来找到捷径的优化方案,那么今天我把研究多年的百度算法原理解密给大家,可能不是最好的,但是我可以给大家保证,这些都是非常实际的。希望给SEOER带来一
我试图找到一种技术来加密和解密程序中的文件,而无需将密码硬编码到程序中,也无需向用户询问密码。 如果我也可以从我正在编写的另一个程序中解密文件,那就太好了。 到目前为止,我还没有多少运气找到一种看起来
有没有一种方法可以使用作为字符串参数传递给程序的私钥而不是使用存储在机器上的证书来解密 PowerShell 中的 RSA?欢迎任何帮助,我的代码如下。 Function Decrypt-Asymme
通过问题Is it possible to use the Grails Jasypt plugin outside the GORM layer for simple String encrypti
我需要解密/加密我的域类中的几列,并且正在寻找有关如何做的信息。我已经找到了jasypt加密插件,但不幸的是它似乎与Grails 2.4不兼容。 我可能可以将一些东西拼凑在一起,但是想要确保Im遵循最
我需要有关声音文件加密/解密的帮助。我想在存储这个声音文件时加密一个声音文件,并在播放这个文件时解密它。我阅读了有关 java 中的加密/解密以及 java 中可用于此的大量示例代码。但这些程序不适用
我很感兴趣是否可以使用 Excel Visual Basic 和某些加密服务提供程序进行字符串加密/解密。 我找到了一个演练 Encrypting and Decrypting Strings in
我们正在使用加密/解密和UIIMAGE。如果我们在不保存到iphone画廊的情况下进行加密和解密以及UIIMAge,则可以正常工作,但是,如果我们进行加密,保存到画廊,将(加密的图像)加载到应用程序中
我正在做一个像这样的简单程序: package rsaexample; import java.io.*; import java.math.BigInteger; import java.secur
我发现这段代码返回给定字符串的校验和。 public static String getChecksum(String md5) { int counter = 0; while (c
我在 Java SE 和 Android 项目上使用相同的代码。在 Java 和 Android 中运行的应用程序连接到相同的 MQTT 代理并交换消息。消息使用 AES 进行加密/解密。我对 Jav
我想在 openssl/libcrypto 中使用 RSA 加密/解密一个长文件(我知道 AES 更好,但这只是为了比较)。我将输入文件分成大小为 numBlocks = inputFileLengt
我是一名优秀的程序员,十分优秀!