Practice of RC4 Encryption algorithm

Source: Internet
Author: User
Tags asymmetric encryption

The encryption algorithm simply says

?? Encryption is not so mysterious, after trying, encryption application is also very simple, although they do not think of the classic encryption algorithm out.

?? Types of cryptographic algorithms:

    • Symmetric encryption: The encryption decryption key is the same;
    • Asymmetric encryption: Encryption and decryption using different keys;

?? Common cryptographic algorithms:

    • RC4: symmetric algorithm, variable length key, encryption of large amounts of data, fast speed, clear text length;
    • DEC: symmetric algorithm, fast speed, a large number of data encryption;
    • Idea: symmetric algorithm, 128-bit key;
    • RSA: Asymmetric algorithm, public key + key;
Practice of RC4 Encryption algorithm

?? For the RC4 encryption algorithm, when the key length reaches 128 bits, the brute force hack is basically difficult to crack. The following is the implementation of C + +:

encryptdecode.h
#ifndef __encryptdecode_h__#define __ENCRYPTDECODE_H__#include <stdio.h>#include <string.h>classencryptdecode{Private:voidEncrypt_decode_init (unsigned Char*s,Char*key,unsigned intKeylen);voidEncrypt_decode (unsigned Char*s,unsigned Char*data,unsigned LongTextlen);};#endif
encryptdecode.cpp
#include "EncryptDecode.h"voidEncryptdecode::encrypt_decode_init (unsigned Char*s,Char*key,unsigned intKeylen) {intI=0, j=0;Chark[ the]={0};unsigned Chartmp=0; for(i=0;i< the; i++) {s[i]=i;    K[i]=key[i%keylen]; } for(i=0;i< the; i++) {j= (j+s[i]+k[i])% the;        Tmp=s[i];        S[I]=S[J];    s[j]=tmp; }}voidEncryptdecode::encrypt_decode (unsigned Char*s,unsigned Char*data,unsigned LongTextlen) {intI=0, j=0, t=0;unsigned Longk=0;unsigned Chartmp for(k=0; k<textlen;k++) {i= (i+1)% the; J= (J+s[i])% the;        Tmp=s[i];        S[I]=S[J];                s[j]=tmp; T= (S[i]+s[j])% the;    DATA[K] ^= s[t]; }}
main.cpp
#include "encryptdecode.h"#include <stdio.h>intMainintargcChar*argv[]) {unsigned Chars_box[ the] ="";unsigned Charbuf[ +] ="This is a RC4 encryption decryption algorithm test";Charkey[ the] ="";intKeylen =strlen(key);//EncryptionEncrypt_decode_init (S_box,key,keylen); Encrypt_decode (S_box,buf,strlen(BUF));printf("encrypt:%s\n", buf);//decryption (S_box changed in encryption, need to regain S_box)Encrypt_decode_init (S_box,key,keylen); Encrypt_decode (S_box,buf,strlen(BUF));printf("decode:%s\n", buf);return 0;}
Description
    • For different lengths of plaintext encryption, the text is decrypted, it is necessary to obtain the length of the plaintext, otherwise the decryption does not come out;
    • S_box encryption is no longer the original content, before encryption can save the contents of S_box, or re-initialized once, decryption can be decrypted successfully;
    • You can know that the factors that affect the success of decryption are the length of the plaintext, such as a file encryption, the file is divided into three paragraphs, the length of each paragraph is different, then the decryption needs to know the length of the three pieces of clear text, and the length and content of the key will affect the decryption;

Copyright NOTICE: This article for Bo Master [original] article, without BO Master permission can be reproduced, annotated blog Source: [Http://blog.csdn.net/FreeApe]

Practice of RC4 Encryption algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.