blowfish的C源码.docx
- 文档编号:1921735
- 上传时间:2022-10-25
- 格式:DOCX
- 页数:15
- 大小:23.50KB
blowfish的C源码.docx
《blowfish的C源码.docx》由会员分享,可在线阅读,更多相关《blowfish的C源码.docx(15页珍藏版)》请在冰豆网上搜索。
blowfish的C源码
blowfish的C源码
//包含三个文件:
blowfish.h,blowfish.c,blowfish_test.c以====为分割线,可运行
/*
blowfish.h:
Headerfileforblowfish.c
Copyright(C)1997byPaulKocher
Thislibraryisfreesoftware;youcanredistributeitand/ormodifyitunderthetermsoftheGNULesserGeneralPublicLicenseaspublishedbytheFreeSoftwareFoundation;eitherversion2.1oftheLicense,or(atyouroption)anylaterversion.Thislibraryisdistributedinthehopethatitwillbeuseful,butWITHOUTANYWARRANTY;withouteventheimpliedwarrantyofMERCHANTABILITYorFITNESSFORAPARTICULARPURPOSE.SeetheGNULesserGeneralPublicLicenseformoredetails.
YoushouldhavereceivedacopyoftheGNULesserGeneralPublicLicensealongwiththislibrary;ifnot,writetotheFreeSoftwareFoundation,Inc.,59TemplePlace,Suite330,Boston,MA02111-1307USA
Seeblowfish.cformoreinformationaboutthisfile.
*/
typedefstruct{
unsignedlongP[16+2];
unsignedlongS[4][256];
}BLOWFISH_CTX;
voidBlowfish_Init(BLOWFISH_CTX*ctx,unsignedchar*key,intkeyLen);voidBlowfish_Encrypt(BLOWFISH_CTX*ctx,unsignedlong*xl,unsignedlong*xr);voidBlowfish_Decrypt(BLOWFISH_CTX*ctx,unsignedlong*xl,unsignedlong*xr);
==========================
/*
blowfish.c:
CimplementationoftheBlowfishalgorithm.
Copyright(C)1997byPaulKocher
Thislibraryisfreesoftware;youcanredistributeitand/ormodifyitunderthetermsoftheGNULesserGeneralPublicLicenseaspublishedbytheFreeSoftwareFoundation;eitherversion2.1oftheLicense,or(atyouroption)anylaterversion.
Thislibraryisdistributedinthehopethatitwillbeuseful,butWITHOUTANYWARRANTY;withouteventheimpliedwarrantyofMERCHANTABILITYorFITNESSFORAPARTICULARPURPOSE.SeetheGNULesserGeneralPublicLicenseformoredetails.
YoushouldhavereceivedacopyoftheGNULesserGeneralPublicLicensealongwiththislibrary;ifnot,writetotheFreeSoftwareFoundation,Inc.,59TemplePlace,Suite330,Boston,MA02111-1307USA
COMMENTSONUSINGTHISCODE:
Normalusageisasfollows:
[1]AllocateaBLOWFISH_CTX.(Itmaybetoobigforthestack.)
[2]CallBlowfish_InitwithapointertoyourBLOWFISH_CTX,apointerto
thekey,andthenumberofbytesinthekey.
[3]Toencrypta64-bitblock,callBlowfish_Encryptwithapointerto
BLOWFISH_CTX,apointertothe32-bitlefthalfoftheplaintext
andapointertothe32-bitrighthalf.Theplaintextwillbe
overwrittenwiththeciphertext.
[4]Decryptionisthesameasencryptionexceptthattheplaintextand
ciphertextarereversed.
Warning#1:
Thecodedoesnotcheckkeylengths.(Caveatencryptor.)Warning#2:
BewarethatBlowfishkeysrepeatsuchthat"ab"="abab".Warning#3:
ItisnormallyagoodideatozeroizetheBLOWFISH_CTXbefore
freeingit.
Warning#4:
Endiannessconversionsaretheresponsibilityofthecaller.
(Toencryptbytesonalittle-endianplatforms,you'llprobablywant
toswapbytesaroundinsteadofjustcasting.)
Warning#5:
Makesuretouseareasonablemodeofoperationforyour
application.(Ifyoudon'tknowwhatCBCmodeis,seeWarning#7.)Warning#6:
Thiscodeissusceptibletotimingattacks.
Warning#7:
Securityengineeringisriskyandnon-intuitive.Havesomeone
checkyourwork.Ifyoudon'tknowwhatyouaredoing,gethelp.
Thisiscodeisfastenoughformostapplications,butisnotoptimizedforspeed.
IfyourequirethiscodeunderalicenseotherthanLGPL,pleaseask.(Icanbelocatedusingyourfavoritesearchengine.)Unfortunately,Idonothavetimetoprovideunpaidsupportforeveryonewhousesthiscode.
--PaulKocher*/
#include"blowfish.h"
#defineN16
staticconstunsignedlongORIG_P[16+2]={
0x243F6A88L,0x85A308D3L,0x13198A2EL,0x03707344L,
0xA4093822L,0x299F31D0L,0x082EFA98L,0xEC4E6C89L,
0x452821E6L,0x38D01377L,0xBE5466CFL,0x34E90C6CL,
0xC0AC29B7L,0xC97C50DDL,0x3F84D5B5L,0xB5470917L,
0x9216D5D9L,0x8979FB1BL
};
staticconstunsignedlongORIG_S[4][256]={
{0xD1310BA6L,0x98DFB5ACL,0x2FFD72DBL,0xD01ADFB7L,
0xB8E1AFEDL,0x6A267E96L,0xBA7C9045L,0xF12C7F99L,
0x24A19947L,0xB3916CF7L,0x0801F2E2L,0x858EFC16L,
0x636920D8L,0x71574E69L,0xA458FEA3L,0xF4933D7EL,
0x0D95748FL,0x728EB658L,0x718BCD58L,0x82154AEEL,
0x7B54A41DL,0xC25A59B5L,0x9C30D539L,0x2AF26013L,
0xC5D1B023L,0x286085F0L,0xCA417918L,0xB8DB38EFL,
0x8E79DCB0L,0x603A180EL,0x6C9E0E8BL,0xB01E8A3EL,
0xD71577C1L,0xBD314B27L,0x78AF2FDAL,0x55605C60L,
0xE65525F3L,0xAA55AB94L,0x57489862L,0x63E81440L,
0x55CA396AL,0x2AAB10B6L,0xB4CC5C34L,0x1141E8CEL,
0xA15486AFL,0x7C72E993L,0xB3EE1411L,0x636FBC2AL,
0x2BA9C55DL,0x741831F6L,0xCE5C3E16L,0x9B87931EL,
0xAFD6BA33L,0x6C24CF5CL,0x7A325381L,0x28958677L,
0x3B8F4898L,0x6B4BB9AFL,0xC4BFE81BL,0x66282193L,
0x61D809CCL,0xFB21A991L,0x487CAC60L,0x5DEC80
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- blowfish 源码