RC4加密 解密

2021-10-06 18:12:20 字數 3432 閱讀 9528

建立rc4crypto類

using system;

using system.collections.generic;

using system.linq;

using system.text;

using system.web;

namespace rc.web.utility

///

/// 有參構造器

///

/// 密碼

public

rc4crypto

(string key)

#endregion

#region 屬性

///

/// 金鑰

///

public

string key

///

/// 編碼

///

public

encoding encoding

///

/// 編碼模式

///

public

encodermode encodermode

///

/// 例項方法

///

public

static

rc4crypto rc4

}#endregion

#region 方法

///

/// 編碼模式

///

public

enum encodermode

///

/// 加密

///

/// 明文

///

public

string

encrypt

(string data)

///

/// 帶編碼模式的字串加密

///

/// 要加密的資料

/// 密碼

/// 編碼模式

/// 加密後經過編碼的字串

public

string

encrypt

(string data,

string key,

encodermode em = encodermode.base64encoder)

///

/// 解密

///

/// 密文

///

public

string

decrypt

(string data)

///

/// 帶編碼模式的字串解密

///

/// 要解密的資料

/// 密碼

/// 編碼模式

/// 明文

public

string

decrypt

(string data,

string key,

encodermode em)

///

/// 加密

///

/// 要加密的資料

/// 密碼

/// 加密後經過預設編碼的字串

public

string

encrypt

(string data,

string key)

///

/// 解密

///

/// 要解密的經過編碼的資料

/// 密碼

/// 明文

public

string

decrypt

(string data,

string key)

///

/// 16進製制轉換位元組

///

/// 字串

///

private byte[

]hextobyte

(string hex)

int32 dwcount = ilen /2;

uint32 tmp1, tmp2;

byte[

] pbbuffer =

newbyte

[dwcount]

;for

(int32 i =

0; i < dwcount; i++

)return pbbuffer;

}///

/// 位元組轉換16進製制

///

/// 位元組

///

private

string

bytetohex

(byte[

] bytes)

return sbr.

tostring()

;}///

/// 加密位元組

///

/// 節字

/// key

///

private byte[

]encrypt

(byte[

] data,

string key)

return output;

}///

/// 解密位元組

///

/// 位元組

/// key

///

private byte[

]decrypt

(byte[

] data,

string key)

///

/// 打亂密碼

///

/// 密碼

/// 密碼箱長度

/// 打亂後的密碼

private byte[

]getkey

(byte[

] pass,

int32 klen)

int64 j =0;

for(

int64 i =

0; i < klen; i++

)return mbox;

}#endregion

}///

/// rc4 擴充套件方法

///

public

static

class

rc4extend

}}

下面是呼叫部分

using rc.web.utility;

using system;

using system.collections.generic;

using system.linq;

using system.runtime.compilerservices;

using system.text;

using system.threading.tasks;

namespace rc4

}}

RC4加密解密演算法

介紹 rc4 來自rivest cipher 4的縮寫 是一種流加密演算法,金鑰長度可變。它加解密使用相同的金鑰,因此也屬於對稱加密演算法。rc4是有線等效加密 wep 中採用的加密演算法,也曾經是tls可採用的演算法之一。rc4演算法特點 1 演算法簡潔易於軟體實現,加密速度快,安全性比較高 2 ...

RC4加密解密實現

首先引用下別人的圖吧很清晰的流程圖,加密解密都是同乙個金鑰流實現的。1 第一步是生成s盒 初始化s和t 開始時,s中元素的值被置為按公升序從0到255,即s 0 0,s 1 1,s 255 255。同時建立乙個臨時向量t 長度與s相同 如果金鑰k的長度為256位元組,則將k賦給t k的長度為可能小於...

RC4加密隨筆

1.原理 1.1 初始化金鑰 根據輸入的金鑰key,使用金鑰排程演算法ksa生成乙個256位元組的sbox。1.2 再通過偽隨機數生成演算法 prga 得到金鑰流 keystream 1.3 加密 金鑰流與明文進行異或運算得到密文 1.4 解密 密文與金鑰流進行異或運算得到明文 2.金鑰排程演算法k...