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已經不太安全,只能用於一般加密,不能用於金融等緊要場合。