rc4加密流程
A. 在易语言里,加密算法只需要作为参数,求rc4算法易语言源码,不是用加密数据命令,而是
.版本2
.子程序加密为rc4,文本型,公开,用RC4算法对文本进行加密
.参数加密的数据,文本型,,欲加密的文本
.参数加密密钥,文本型,可空,用作加密的密码
.局部变量临时,字节集
.局部变量s,字节型,,"256"
.局部变量k,字节型,,"256"
.局部变量i,整数型
.局部变量j,整数型
.局部变量y,整数型
.局部变量outstr,文本型
.局部变量temp,字节型
.局部变量x,整数型
.局部变量t,整数型
.局部变量aaa,文本型
临时=到字节集(文本)
.计次循环首(256,i)
s[i]=i-1
.计次循环尾()
j=1
.计次循环首(256,i)
.如果真(j>取文本长度(密码))
j=1
.如果真结束
k[i]=取代码(取文本中间(密码,j,1),)
j=j+1
.计次循环尾()
j=0
.计次循环首(256,i)
j=(j+s[i]+k[i])%256+1
temp=s[i]
s[i]=s[j]
s[j]=temp
.计次循环尾()
i=0
j=0
.计次循环首(取字节集长度(临时),x)
i=(i+1)%256+1
j=(j+s[i])%256+1
temp=s[i]
s[i]=s[j]
s[j]=temp
t=(s[i]+s[j]%256)%256+1
y=s[t]
aaa=到十六进制文本(位异或(临时[x],y))
outstr=outstr+选择(取文本长度(aaa)>1,“”,“0”)+aaa+“,”
.计次循环尾()
返回(outstr)
.子程序到十六进制文本,文本型
.参数数值,整数型,,
返回(多项选择(右移(数值,4)+1,“0”,“1”,“2”,“3”,“4”,“5”,“6”,“7”,“8”,“9”,“A”,“B”,“C”,“D”,“E”,“F”)+多项选择(位与(数值,15)+1,“0”,“1”,“2”,“3”,“4”,“5”,“6”,“7”,“8”,“9”,“A”,“B”,“C”,“D”,“E”,“F”))
B. 可以给我一个用RC4加密明文的例子吗 谢谢啊
运行环境:Microsoft Visual Studio 6.0
语言:C++
#include<stdio.h>
#include<string>
void decrypt(char cipher[]);///////////////////////////////解密过程函数,根据输入密钥再一次生成密钥流
void main()
{
printf("**************************RC4加解密程**************************\n");
char choose1,choose2;
do{
int s[256],t[256];
char k[256];/////////////////用户输入的密钥
char plaintext[1024],ciphertext[1024];
printf("输入密钥:\n");
gets(k);
for(int i=0;i<256;i++)//////////////给字节状态矢量和可变长的密钥数组赋值
{
s[i]=i;
t[i]=k[i%strlen(k)];
}
int j=0;
for(i=0;i<256;i++) //////使用可变长的密钥数组初始化字节状态矢量数组s
{
int temp;
j=(j+s[i]+t[i])%256;
temp=s[i];
s[i]=s[j];
s[j]=temp;
}
printf("\n输入要加密的字符串:\n");
gets(plaintext);
int m,n,key[256],q;
m=n=0;
printf("\n得到密文:\n");
for(i=0;i<strlen(plaintext);i++)/////////////由字节状态矢量数组变换生成密钥流并对明文字符进行加密
{
int temp;
m=(m+1)% 256;
n=(n+s[n])% 256;
temp=s[m];
s[m]=s[n];
s[n]=temp;
q=(s[m]+s[n])%256;
key[i]=s[q];
ciphertext[i]=plaintext[i]^key[i];
printf("%c",ciphertext[i]);
}
ciphertext[i]='\0';
//解密
printf("\n是否对上面的密文进行解密?(y/n)\n");
scanf("%c",&choose2);
getchar();
while(choose2=='y'||choose2=='Y')
{
decrypt(ciphertext);///////////////////////////////解密过程函数
choose2='n';
}
printf("\n是否希望继续使用程序?(y/n)\n");
scanf("%c",&choose1);
getchar();
}
while(choose1=='y'||choose1=='Y');
printf("\n****************************程序结束*****************************");
system("pause");
}
//解密函数,密钥流的生成与加密相同
void decrypt(char cipher[])
{
int s[256],t[256];
int i;
char k[256];/////////////////用户输入的密钥
char plaintext[1024];
printf("\n输入密钥:\n");
gets(k);
for(i=0;i<256;i++)//////////////给字节状态矢量和可变长的密钥数组赋值
{
s[i]=i;
t[i]=k[i%strlen(k)];
}
int j=0;
for(i=0;i<256;i++) //////使用可变长的密钥数组初始化字节状态矢量数组s
{
int temp;
j=(j+s[i]+t[i])%256;
temp=s[i];
s[i]=s[j];
s[j]=temp;
}
int m,n,key[256],q;
m=n=0;
printf("\n解密后所得到明文是:\n");
for(i=0;i<strlen(cipher);i++)/////////////由字节状态矢量数组变换生成密钥流并对密文字符进行解密
{
int temp;
m=(m+1)% 256;
n=(n+s[n])% 256;
temp=s[m];
s[m]=s[n];
s[n]=temp;
q=(s[m]+s[n])%256;
key[i]=s[q];
plaintext[i]=cipher[i]^key[i];
printf("%c",plaintext[i]);
}
printf("\n");
}
C. VC++ RC4,加密解密, 使用问题
#include "rc4.h"
void main()
{
char key[]="abcd";
RC4_KEY stKey;
BYTE d1[4]={0x11,0x22,0x33,0x44};
//加密
RC4Init(key,strlen(key),&stKey);
RC4Works(d1,4,&stKey);
//解密
RC4Init(key,strlen(key),&stKey);
RC4Works(d1,4,&stKey);
}
D. RC4算法的详细介绍
RC4加密算法
之所以称其为簇,是由于其核心部分的S-box长度可为任意,但一般为256字节。该算法的速度可以达到DES加密的10倍左右。
RC4算法的原理很简单,包括初始化算法和伪随机子密码生成算法两大部分。假设S-box长度和密钥长度均为n。先来看看算法的初始化部分(用类C伪代码表示):
for (i=0; i<n; i++){
s[i]=i;
}
j=0;
for (i=0; i<n; i++)
{
j=(j+s[i]+k[i])%n;
swap(s[i], s[j]);
}
在初始化的过程中,密钥的主要功能是将S-box搅乱,i确保S-box的每个元素都得到处理,j保证S-box的搅乱是随机的。而不同的S-box在经过伪随机子密码生成算法的处理后可以得到不同的子密钥序列,并且,该序列是随机的:
i=j=0;
while (明文未结束)
{
++i%=n;
j=(j+s)%n;
swap(s, s[j]);
sub_k=s((s+s[j])%n);
}
得到的子密码sub_k用以和明文进行xor运算,得到密文,解密过程也完全相同。
由于RC4算法加密是采用的xor,所以,一旦子密钥序列出现了重复,密文就有可能被破解。关于如何破解xor加密,请参看Bruce Schneier的Applied Cryptography一书的1.4节Simple XOR,在此我就不细说了。那么,RC4算法生成的子密钥序列是否会出现重复呢?经过我的测试,存在部分弱密钥,使得子密钥序列在不到100万字节内就发生了完全的重复,如果是部分重复,则可能在不到10万字节内就能发生重复,因此,推荐在使用RC4算法时,必须对加密密钥进行测试,判断其是否为弱密钥。
但在2001年就有以色列科学家指出RC4加密算法存在着漏洞,这可能对无线通信网络的安全构成威胁。
以色列魏茨曼研究所和美国思科公司的研究者发现,在使用“有线等效保密规则”(WEP)的无线网络中,在特定情况下,人们可以逆转RC4算法的加密过程,获取密钥,从而将已加密的信息解密。实现这一过程并不复杂,只需要使用一台个人电脑对加密的数据进行分析,经过几个小时的时间就可以破译出信息的全部内容。
专家说,这并不表示所有使用RC4算法的软件都容易泄密,但它意味着RC4算法并不像人们原先认为的那样安全。这一发现可能促使人们重新设计无线通信网络,并且使用新的加密算法。
E. 怎样把电脑上的应用软件加密
1、首先选择鼠标打开电脑并在键盘上输入gpedit.msc。
F. 如何在voip中使用rc4对udp收发函数进行加密
qutecom 一个开源的voip客户端
asterisk 开源的ippbx
rc4加密算法简单,快速,据说是比DES算法快10倍。sip 信令本身就属于明文方式传输的,之所以要加密,是为了防止运营商的干扰,使用一个弱的加密算法,是要能防止串改就满足要求了。
rc4 算法可以google原来,用密钥来生成一个256长度的box, 然后box与明文异或操作得到密文,密文再次异或就恢复明文。
下面实现了 qutecom 到asterisk 信令的当向加密,反向的目前还没弄完,等完工了在来补充。
rc4.h
/*
*RC4 functions for HTMLDOC.
*
* Original code by Rob Earhart
* Copyright 1999 by Carnegie Mellon University, All Rights Reserved
*
* Permission to use, , modify, and distribute this software and its
* documentation for any purpose and without fee is hereby granted,
* provided that the above right notice appear in all copies and that
* both that right notice and this permission notice appear in
* supporting documentation, and that the name of Carnegie Mellon
* University not be used in advertising or publicity pertaining to
* distribution of the software without specific, written prior
* permission.
*
* CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
* THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE FOR
* ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
* OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#ifndef _RC4_H_
# define _RC4_H_
# ifdef __cplusplus
extern "C" {
# endif /* __cplusplus */
/*
* RC4 context...
*/
typedef struct
{
unsigned char sbox[256]; /* S boxes for encryption */
int i, j; /* Current indices into S boxes */
} rc4_context_t;
/*
* Prototypes...
*/
extern void rc4_init(rc4_context_t *context, const unsigned char *key,
unsigned keylen);
extern void rc4_encrypt(rc4_context_t *context, const unsigned char *input,
unsigned char *output, unsigned len);
# ifdef __cplusplus
}
# endif /* __cplusplus */
#endif /* !_RC4_H_ */
rc4.c
/*
* RC4 functions for HTMLDOC.
*
* Original code by Tim Martin
* Copyright 1999 by Carnegie Mellon University, All Rights Reserved
*
* Permission to use, , modify, and distribute this software and its
* documentation for any purpose and without fee is hereby granted,
* provided that the above right notice appear in all copies and that
* both that right notice and this permission notice appear in
* supporting documentation, and that the name of Carnegie Mellon
* University not be used in advertising or publicity pertaining to
* distribution of the software without specific, written prior
* permission.
*
* CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
* THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE FOR
* ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
* OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* Contents:
*
* rc4_init() - Initialize an RC4 context with the specified key.
* rc4_encrypt() - Encrypt the given buffer.
*/
#include "rc4.h"
/*
* 'rc4_init()' - Initialize an RC4 context with the specified key.
*/
void
rc4_init(rc4_context_t *text, /* IO - Context */
const unsigned char *key, /* I - Key */
unsigned keylen) /* I - Length of key */
{
int i, j; /* Looping vars */
unsigned char tmp; /* Temporary variable */
/*
* Fill in linearly s0=0, s1=1, ...
*/
for (i = 0; i < 256; i ++)
text->sbox[i] = i;
for (i = 0, j = 0; i < 256; i ++)
{
/*
* j = (j + Si + Ki) mod 256
*/
j = (j + text->sbox[i] + key[i % keylen]) & 255;
/*
* Swap Si and Sj...
*/
tmp = text->sbox[i];
text->sbox[i] = text->sbox[j];
text->sbox[j] = tmp;
}
/*
* Initialized counters to 0 and return...
*/
text->i = 0;
text->j = 0;
}
/*
* 'rc4_encrypt()' - Encrypt the given buffer.
*/
void
rc4_encrypt(rc4_context_t *text, /* I - Context */
const unsigned char *input, /* I - Input buffer */
unsigned char *output, /* O - Output buffer */
unsigned len) /* I - Size of buffers */
{
unsigned char tmp; /* Swap variable */
int i, j; /* Looping vars */
int t; /* Current S box */
/*
* Loop through the entire buffer...
*/
i = text->i;
j = text->j;
while (len > 0)
{
/*
* Get the next S box indices...
*/
i = (i + 1) & 255;
j = (j + text->sbox[i]) & 255;
/*
* Swap Si and Sj...
*/
tmp = text->sbox[i];
text->sbox[i] = text->sbox[j];
text->sbox[j] = tmp;
/*
* Get the S box index for this byte...
*/
t = (text->sbox[i] + text->sbox[j]) & 255;
/*
* Encrypt using the S box...
*/
*output++ = *input++ ^ text->sbox[t];
len --;
}
/*
* Copy current S box indices back to context...
*/
text->i = i;
text->j = j;
}
修改exosip项目中的 jcallback.c 在函数cb_udp_snd_message 中修改,加入rc4加密部分
....
if( 1 )
{
rc4_context_t context;
char * key = "*****";
unsigned char * out = NULL;
int i=0;
out = osip_malloc (length);
if (out == NULL)
return -1;
rc4_init(&context,key,16);
rc4_encrypt(&context,message,out,length);
rc4_message = osip_malloc(length+4);
if(rc4_message != NULL)
{
rc4_message[0] = 'R';
rc4_message[1] = 'C';
rc4_message[2] = '4';
rc4_message[3] = ':';
for(i=0;i<length;i++)
{
rc4_message[i+4] = out[i];
}
}
osip_free(out);
}
// Really send the packet over network
if(rc4_message == NULL)
{
i = owsip_send (account, (const void*) message, length, 0, address, OWSL_ADDRESS_SIZE);
}
else
{
i = owsip_send (account, (const void*) rc4_message, length+4, 0, address, OWSL_ADDRESS_SIZE);
osip_free(rc4_message);
}
....
在asterisk 中的chan_sip.c 修改函数 sipsock_read, 添加 接受信令rc4解密代码
.....
if(res>4 && req.data[0]=='R' && req.data[1]=='C' && req.data[2]=='4' && req.data[3]==':')
{
rc4_context_t context;
char * key = "********";
unsigned char * out = NULL;
int i=0;
out = malloc(res-4);
rc4_init(&context,key,16);
rc4_encrypt(&context,req.data+4,out,res-4);
for(i=0;i<res-4;i++)
{
req.data[i] = out[i];
}
free(out);
req.data[res-4] = '/0';
res = res-4;
req.len = res;
}
.....
G. 如何用rc4 加密算法对excel vba进行加密
我就是专门做破解工作的,没有说普通的加密很容易破解,你有密码里加入大小写,特殊符号以及空格,或者在加上几个其它国家的语言文子(比如日文)10位以上,这样就很难破解了,
如果这样不行的话,你用最新版本的RAR(压缩包加密)位数多一点,再加上大小写,特殊符号以及空格或者在加上几个其它国家的语言文字(比如日文)10位以上,目前基本无法破解,破解软件对这种远算只能达到一秒几次。
namespace CryptoRC4
{
using System;
using System.Text;
public class clsRC4Engine
{
private static long m_nBoxLen = 255;
protected clsRC4Engine()
{
}
private static void GetKeyBytes( string Key, out byte[] m_nBox )
{
long index2 = 0;
m_nBox = new byte[m_nBoxLen];
Encoding ascii = Encoding.ASCII;
Encoding unicode = Encoding.Unicode;
byte[] asciiBytes = Encoding.Convert(unicode,ascii, unicode.GetBytes( Key ));
char[] asciiChars = new char[ascii.GetCharCount(asciiBytes,0,asciiBytes.Length)];
ascii.GetChars(asciiBytes,0,asciiBytes.Length,asciiChars,0);
long KeyLen = Key.Length;
for ( long count = 0; count < m_nBoxLen ; count ++ )
{
m_nBox[count] = (byte)count;
}
for ( long count = 0; count < m_nBoxLen ; count ++ )
{
index2 = (index2 + m_nBox[count] + asciiChars[ count % KeyLen ]) % m_nBoxLen;
byte temp = m_nBox[count];
m_nBox[count] = m_nBox[index2];
m_nBox[index2] = temp;
}
}
private static bool GetEncryptBytes( string sData, byte[] m_nBox,out byte[] EncryptedBytes )
{
EncryptedBytes = null;
bool toRet = true;
try
{
long i=0;
long j=0;
Encoding enc_default = Encoding.Unicode;
byte[] input = enc_default.GetBytes( sData );
EncryptedBytes = new byte[input.Length];
byte[] n_LocBox = new byte[m_nBoxLen];
m_nBox.CopyTo(n_LocBox,0);
long ChipherLen = input.Length + 1;
for ( long offset = 0; offset < input.Length ; offset++ )
{
i = ( i + 1 ) % m_nBoxLen;
j = ( j + n_LocBox[i] ) % m_nBoxLen;
byte temp = n_LocBox[i];
n_LocBox[i] = n_LocBox[j];
n_LocBox[j] = temp;
byte a = input[offset];
byte b = n_LocBox[(n_LocBox[i]+n_LocBox[j])% m_nBoxLen];
EncryptedBytes[offset] = (byte)((int)a^(int)b);
}
}
catch
{
EncryptedBytes = null;
toRet = false;
}
return toRet;
}
public static bool Encrypt( string sData, string Key, out string EncryptedString )
{
EncryptedString = null;
if( sData == null || Key == null ) return false;
byte[] m_nBox;
GetKeyBytes( Key, out m_nBox );
byte[] output;
if( GetEncryptBytes( sData, m_nBox, out output ) )
{
// Convert data to hex-data
EncryptedString = "";
for( int i = 0; i < output.Length; i++ )
EncryptedString += output[i].ToString( "X2" );
return true;
}
else
return false;
}
/// <summary>
/// Decrypt data using specific key
/// </summary>
/// <param name="EncryptedString"></param>
/// <param name="Key"></param>
/// <param name="sData"></param>
/// <returns></returns>
public static bool Decrypt( string EncryptedString, string Key, out string sData )
{
sData = null;
if( EncryptedString == null || Key == null ) return false;
else if( EncryptedString.Length % 2 != 0 ) return false;
byte[] m_nBox;
GetKeyBytes( Key, out m_nBox );
// Convert data from hex-data to string
byte[] bData = new byte[EncryptedString.Length / 2];
for( int i = 0; i < bData.Length; i++ )
bData[i] = Convert.ToByte( EncryptedString.Substring( i * 2, 2 ), 16 );
EncryptedString = Encoding.Unicode.GetString( bData );
byte[] output;
if( GetEncryptBytes( EncryptedString, m_nBox, out output ) )
{
sData = Encoding.Unicode.GetString( output );
return true;
}
else
return false;
}
}
}
调用:
//Encrypt data
string strEncryptedString;
if( clsRC4Engine.Encrypt( strValue, strKey, out strEncryptedString ) )
MessageBox.Show( strEncryptedString );
//Decrypt data
string strDecryptedString;
if( clsRC4Engine.Decrypt( strValue, strKey, out strDecryptedString ) )
MessageBox.Show( strDecryptedString );
另外一种
public static string encrypt_str( string str )
{
string s = "";
int i_Encrypt = ClsSetConst.m_Set_Encrypt;
char[] s_array = str.ToCharArray();
for(int i = 0; i < s_array.Length; i++)
{
int x = ((int)s_array[i]) + i_Encrypt;
s += (char)(x);
}
return s;
}
public void decript_str(string str)
{
string s = "";
int i_Encrypt = ClsSetConst.m_Set_Encrypt;
char[] s_array = str.ToCharArray();
for(int i = 0; i < s_array.Length; i++)
{
int x = ((int)s_array[i]) - i_Encrypt;
s += (char)x;
}
自己看看有没有输错的地方吧
H. 求大神用java实现RC4的加密,解密功能,高分悬赏.
importjavax.crypto.Cipher;
importjavax.crypto.spec.SecretKeySpec;
importjavax.xml.bind.DatatypeConverter;
publicclassTest{
publicstaticvoidmain(String[]args)throwsException{
Ciphercipher=Cipher.getInstance("RC4");
Stringpwd="123456";
Stringptext="HelloWorld你好";
SecretKeySpeckey=newSecretKeySpec(pwd.getBytes("UTF-8"),"RC4");
cipher.init(Cipher.ENCRYPT_MODE,key);
byte[]cdata=cipher.update(ptext.getBytes("UTF-8"));
//解密
cipher.init(Cipher.DECRYPT_MODE,key);
byte[]ddata=cipher.update(cdata);
System.out.println("密码:"+pwd);
System.out.println("明文:"晌隐+ptext);
System.out.println("密文:"+DatatypeConverter.printHexBinary(cdata));
System.out.println("解密文:"+newString(ddata,"UTF-8"));
}
}
密码:123456
明文:HelloWorld你镇戚好
密文:
解密文御谨陵:HelloWorld你好
RC4已经不太安全,只能用于一般加密,不能用于金融等紧要场合。