维吉尼亚密码JAVA代码实现完整版
- 格式:pdf
- 大小:85.46 KB
- 文档页数:2
一、实验目的1. 理解维吉尼亚密码算法的原理和加密解密过程。
2. 掌握维吉尼亚密码算法的编程实现。
3. 通过实验,提高对密码学基础知识的理解和应用能力。
二、实验原理维吉尼亚密码算法是一种多表密码,由法国密码学家布莱斯·德·维吉尼亚(Blaise de Vigenère)于16世纪提出。
该算法利用一个密钥来生成一系列凯撒密码,通过将这些凯撒密码依次应用于明文,实现对明文的加密。
加密过程如下:1. 编号:将A-Z以编号0-25编号;2. 选取密钥:举例:wangyuhang;3. 明文处理:去掉所有空格;4. 密钥处理:将密钥重复排列;5. 加密:将明文每个字母和密钥对应位置的字母相加(mod 26),得到新的字母;6. 输出密文。
解密过程如下:1. 编号:将A-Z以编号0-25编号;2. 获取密钥:举例:wangyuhang;3. 密文处理:去掉所有空格;4. 密钥处理:将密钥重复排列;5. 解密:将密文每个字母和密钥对应位置的字母相减(mod 26),得到原始明文;6. 输出明文。
三、实验环境1. 操作系统:Windows 102. 编程语言:C++3. 开发工具:Visual Studio 2019四、实验步骤1. 创建一个名为“VigenereCipher”的C++项目。
2. 在项目中创建一个名为“VigenereCipher.h”的头文件,用于声明加密和解密函数。
3. 在项目中创建一个名为“VigenereCipher.cpp”的源文件,用于实现加密和解密函数。
4. 在项目中创建一个名为“main.cpp”的主程序文件,用于调用加密和解密函数,并进行测试。
5. 编写代码实现以下功能:(1)将明文和密钥转换为数字编码;(2)将数字编码相加(加密)或相减(解密)(mod 26);(3)将数字编码转换回字母编码;(4)输出加密后的密文和解密后的明文。
五、实验结果与分析1. 加密过程输入明文:we are discovered save yourself密钥:wangyuhang输出密文:senxadvyyoikneqywvrekueyals2. 解密过程输入密文:senxadvyyoikneqywvrekueyals密钥:wangyuhang输出明文:wearediscoveredsaveyourself通过实验,验证了维吉尼亚密码算法的正确性和实用性。
维吉尼亚(vigenere)密码,是一个基于多表替换的移位密码!现代的维吉尼亚(vigenere)密码的替换表一般为这样:A B C D E F G H I J K L M N O P Q R S T U V W X Y ZA ABCDEFGHIJKLMNOPQRSTUVWXY ZB BCDEFGHIJKLMNOPQRSTUVWXYZ AC CDEFGHIJKLMNOPQRSTUVWXYZ A BD DEFGHIJKLMNOPQRSTUVWXYZ A B CE EFGHIJKLMNOPQRSTUVWXYZ A B C DF FGHIJKLMNOPQRSTUVWXYZ A B C D EG G H I J K L M N O P Q R S T U V W X Y Z A B C D E FH H I J K L M N O P Q R S T U V W X Y Z A B C D E F GI I J K L M N O P Q R S T U V W X Y Z A B C D E F G HJ J K L M N O P Q R S T U V W X Y Z A B C D E F G H IK K L M N O P Q R S T U V W X Y Z A B C D E F G H I JL L M N O P Q R S T U V W X Y Z A B C D E F G H I J KM M N O P Q R S T U V W X Y Z A B C D E F G H I J K LN N O P Q R S T U V W X Y Z A B C D E F G H I J K L MO O P Q R S T U V W X Y Z A B C D E F G H I J K L M NP P Q R S T U V W X Y Z A B C D E F G H I J K L M N OQ Q R S T U V W X Y Z A B C D E F G H I J K L M N O PR R S T U V W X Y Z A B C D E F G H I J K L M N O P QS S T U V W X Y Z A B C D E F G H I J K L M N O P Q RT T U V W X Y Z A B C D E F G H I J K L M N O P Q R SU U V W X Y Z A B C D E F G H I J K L M N O P Q R S TV V W X Y Z A B C D E F G H I J K L M N O P Q R S T UW W X Y Z A B C D E F G H I J K L M N O P Q R S T U VX X Y Z A B C D E F G H I J K L M N O P Q R S T U V WY Y Z A B C D E F G H I J K L M N O P Q R S T U V W XZ Z A B C D E F G H I J K L M N O P Q R S T U V W X Y行和列分别用待加密的字符和加密当前字符的密钥来决定,然后横竖的交集,就是当前字符加密后的结果!比起传统的凯撒(caesar)密码,维吉尼亚(vigenere)在移位密码带来的字符出现频率上,具有更多迷惑性!多表替换,意味着不同字母移位替换时,遇到不同的密钥(偏移量),可能得到相同的密文;当然,相同的明文经过加密可能获得不同的密文,下面是一个用dorainm 当作密钥来加密blood has not been cold yet的例子明文: bloodhasbotbeencoldyet密钥: dorainmdorainmdorainmd密文: EZFOLUMVBFTJRQQQFLLLQW对于维吉尼亚(vigenere)密码的介绍就到这里,下面讲述如何破解!虽然从全文角度,我们没法像凯撒(caesar)密码一样通过获取某个字母出现的概率来推算密钥(关于如何破解凯撒密码,看/u1/41420/showart_322311.html),但是维吉尼亚(vigenere) 密码的密钥长度有限,并且根据密钥长度,隔相同次数又重新使用了相同的密钥,比如上例中的密钥dorainm,隔7个字符,又出现了相同的密钥(偏移量),那么结果,不就是一个凯撒密码了吗?现在有个思路了,首先,我们隔1个,2个...MAXLEN个字符取,按照破解传统凯撒密码的方式,统计里面最高概率的2个字符的差值,是否等于'e'和't'之间的差值(因为在英文单词中,e出现的概率是12.702%,第一高,t出现的概率是9.056%,第二高)如果某个长度能够满足这个条件,我们就可以初步推测,这就是密钥的长度!既然推测出密钥的长度了,那么我们根据长度和起始位置,就像破解凯撒密码一样,挨组破解,那么结果就出来了!vigenere-cracker的源码包中,有篇纽约时报(New York Times)网站上转来的新闻,我们用维吉尼亚(vigenere)加密它$ tar xzf dvigenere-0.1.tar.gz$ tar xzf dvigenere-cracker-0.1.tar.gz$ cd dvigenere-0.1$ makegcc -O3 -c -o dvigenere.o dvigenere.cgcc -O3 -o dvigenere main.c dvigenere.o$./dvigenere -e -k dorainm ../dvigenere-cracker-0.1/new-york-times.txt../dvigenere-cracker-0.1/ciphertext.txt我们来看看新闻的内容和加密后的内容Fed Cuts Key Interest Rate by a Half PointWASHINGTON, Sept. 18 — The Federal Reserve today lowered its benchmark interest rate by a half point,a forceful policy shift intended to limit the damage to the economy from the recent disorder in the housing and credit markets.Skip to next paragraphRelatedFederal Reserve StatementThe New York TimesWhile an interest rate cut was widely expected, there had been profound uncertainty about whether the Fed would choose a more cautious quarter-point reduction. But the bolder action and an accompanying statement, both approved by a unanimous vote of the central bank's policy-setting committee, made it clear the Fed decided the risks of a recession were too big to ignore."Developments in financial markets since the committee's last regular meeting have increased the uncertainty surrounding the economic outlook," the central bank said. Signaling that it might cut rates more if necessary in months ahead, it said it would "continue to assess" the economic outlook and "act as needed to foster price stability and sustainable economic growth."The decision, which reset the overnight lending rate between banks to 4.75 percent, was the Fed's first rate cut in four years.Stocks immediately soared. The Dow Jones industrial average had been up about 75 points shortly before the announcement at 2:15 p.m., and within seconds it jumped another 100 points. Shortly after 3:30 p.m., it was showing a gain of 300 points on the day, or more than 2 percent.For consumers, the Fed's move could mean lower borrowing costs on for mortgages and automobile loans. But the impact may be muted, because investors remain deeply anxious about the credit quality of mortgages and other long-term loans. The main problem in the past month has not been high rates so much as the availability of capital to complete deals.In a separate move to bolster the banking system, the Fed also said today that it had cut its discount lending rate, which applies to short-term emergency loans to banks, to 5.25 percent —also a half-point cut.This was the Federal Reserve's most abrupt reversal of course since January 2001, when it suddenly slashed rates at an unscheduled emergency meeting because of signs that the economy was slipping into a recession. The last half-point cut in the federal funds rate came in November 2002.Economists said that the Fed's move today was similarly pre-emptive. "Monetary policy makers are worried about growth being seriously compromised and are prepared to take whatever prudent steps they can to avoid a deep slump," said Joshua Shapiro, chief United States economist for MFR.Some aspects of today's Fed's move could fuel inflation fears. Gold, a traditional investment safe haven in times of inflation, soared immediately after the Fed's decision was announced. As United States interest rates became less attractive for investment, the value of the dollar against the euro touched a new low before recovering slightly, and oil prices continued to climb even further above $80 a barrel.In the stock market, financial stocks posted the biggest gains, reflecting the fact that banks now will face lower borrowing costs, which should help drive profits higher."Shock therapy," was the assessment of Ethan Harris, chief economist at Lehman Brothers.But Mr. Harris cautioned that the Fed stopped short of signaling a firm commitment to more rate reductions. While it dropped its previous statement that inflation was still the "predominant concern," which would argue against using lower rates to stimulate the economy, the Fed said that "inflation risks remain" and that it would "monitor inflation developments carefully ... ...加密后tVD pGWG kML lBKEZREW iABR EM A uMOT pWVZWwiftlbxtwa, gVPB. 18 — wVV nRPHFRL eQVSIVM FRRRY YAZSIEL UWG BMAOKARRS UQHVRMFF FRTM NB R PNXI GOQAF, R NBDFSWUT BRZZCG EKWWT VZWSEDMQ WC LQZUW KHM PDARGM FR KHM QFCEOUL IFFM GTH IEKRZW UIABDGSI QA WVV PBGVWEG NZG TRMQUW DAZXQWG.fWLD TW ZHLK XNDDUIAXUuSCABRPtVDMEMO iEARDYS sBNFHAVNBfKS nMJ bCIK gUPSJjTLZV IA LBKEZREW IABR FIK ENE KZDMYK SOPMPFHR,BUQUS HIQ ESVN CDRTFUVQ XBTEZGMLBKY NNRIK EUQWVVR GTH wEL IRICD PTRCJE N PCIE PMXHZOCF TIRRBRD-DFIVG USUUKGURB.jHF HYE OAORVR NOWWFN NZG RN NOFCDPIAKLBX AGMWSDEVG,PFTP MSDIODRP PP I GQOEIUBGV MOBR RT TPR FSETZNX PRNS'E DFLQPK-GVTBVZJ TOUZUWHVE, YDRV QG FZVAZ FKS fMQ GSTILRP HYE EUVYJ WS D IEKREVWFN JQUS TWB EWX BB LUEOZR."lRHHZFPURZWG IV RLBRNKVMO DAZXQWG SQAOH KHM ORADIBGQH'J TNEW IEOHXDF MMRFLBX PNHH ZNKEQDGVD GTH LNKRDWOZNBL VIIRWHZGWEG GTH VCWAAPWT WHFOCFK," WVV KRZWFRL OMQY SIVP. jIOAMOWEG GTDH IB YLUYT PGW IABRE AFRM UI EEKREVOIY VZ AFNBUE OYEIQ, WK ANUG ZT JAXZU "PAQHZNCR WC AAFQVG" BUQ STOVBYLQ OCGXRCB IAP "RCB MV EEMQQG KO SAVHVR CDLQV AGMEWCIBL DBU AHEWOZNIOXH VCWAAPWT OEAZHY."hYE QQFWJIWA, KYIKU USJEB FKS ODRDQWXHB XHBUIVT UOKE OQWKVEV NDBBS GA 4.75 BHFTEVG, KRS GTH wEL'E TZRAG UOKE PGW ZN SAXF YMNDV.aGAFYJ QZYHRZABRXB JOIEQG. tPR gCN rBZHG IVQGVHIIIY DJVRITQ VRD OQHB UX MECLT 75 DFIVGE GYOZGXB SENBDH KHM MQBFUVPQPSET NF 2:15 B.A., NZG NIBUUQ JEKBZGG IB VXAGEL MQCKHME 100 XBUQHJ. fTRFKLG MIHVR 3:30 P.Z., ZT JMV JHWJUQU A TMLB ON 300 PWVZWG OV FKS DIL, CI UBDH KHIA 2 GEZPQQH.sAU TOVFGPSIS, FKS fMQ'V DODR FCLLL YHOE TBIHF BWEDRKZNO ORGKS BZ TFR ZAUHXAORE OED NGWCDOJVXH COIAE. sUB FKS IUCMFH MIL ES MCGQG, BMPMXGV QAHHGKOZF USDAQA GSVPTL DBOIWHE OSOCG WVV KEQGWK YHMOWKY BR AFRBTMJSJ IAP CKHME OCEG-GQUA LWNZV. tPR POZN CDRPCEU UQ KHM BDGK UBZWV HIF QCK JRQQ YIOU UOKEA ER DUKU DG TPR DJRITNNLZZTG AI TAXVFDZ TW ORAGLMGQ RVATF.iV M GVPIEMWS MWIQ HF JBXVHVR GTH SAVXUQU SGFFHA, BUQ tVD NXVC SIVP HFDIL WVRT VF VRD PGW ZTA PLGTOCAF ZVNLVZJ IABR, KYIKU DDGLQRE HF AUAUH-TMEY SDEZTQQQP TBMQG TW NDBBS, FR 5.25 SSICMAF — NXVC A UMOT-PWVZW TUB.kHQF ZOJ BUQ tVDMEMO iEARDYS'S ZAVH AJEGSH RMIQUGRL BR QFUZFQ GZNKR mOEUIEK 2001, ZVVN VF GLDLRZOM STNEKSU ZNFHG AB MQ LNAPTHRLLMQ HAVRORZFM MMRFLBX JRODIJE BR GZGVF WVRT GTH VCWAAPM WIF VZZPXVZJ ZNBB D IEKREVWFN. fKS LIFF VRLN-BRWET PGW ZN GTH WELRDDZ FCAPV IABR FODE VZ bFVMZNHF 2002.mPAQCDIAGE GRIL FKOK BUQ tVD'F PCME GAGOP ENE GZMQYMUZP XEQ-SDPBVHH. "uBZHHRRG BRZZCG YDYVRA MUS WWEDLSU IOAXH GZBIWV BMVZJ JEZVAXGCY PAPDIOUVEHR AVQ DFV XEQSOIEL FR KASR ZVRTMIQU GRCQQQH SBRBV KHML FOE BB DJFIL M RVEX EOIDP," VOZD wAVVLA fTDDZRW, FVZEN gQWKEL eWOKEA QFCEOUVEW WOZ yif.eRAV IFBHQKS BR HFDIL'V wEL'E AFVM ORICD SGHZ IVSXDHZOV RHOIS. sRZU, N WFRDQGURBRL VZYSJTURZW JANR KOMEV UQ KIURE CW QAROOKIWA, GFAZRP WDMMQUDHVLG MIHVR GTH wEL'E RVCQFURB WIF DBEOCAOHR. iF xBZTMQ vHRTMF LBKEZREW IABRE PVCIZQ ZVSA MWHIAKGUYS FWE LBMEAGYHBK, GTH MATHQ CW BUQ RFLTND OXAQAEW KHM QXFF BBGFVVD N QSN TBI PVFWEQ FVCWIQUWEG FXLUYTTL, OED BUO GRQPQV TOVGUQIVD GA QCIUO HJVN SGUHYEZ MECME $80 R JNDUSC.lB TPR VHFCS YDFBEB, IWEAVPUDZ SBBONG PWFFHR TPR EWXGMFF URIVF, FVFTROWWEG GTH WAKG WVRT OMQYJ VBI KZLT RDQV TBIHF BWEDRKZNO ORGKS, IKWTH FTRICD UQOD DZVHH GRWSUWG HQTTHF."eKCTK GTHFRPG," KRS GTH RSAREVAVNB AI vTPNZ vRRZVE, THQRR STOVBYLGK IG oSYMIA eFFTPRDV.oGW dR. tDFIIA ODIKIWAQG KHIG WVV nRP GKOXCQG JHWEF CW AVSQOCIVT D WIZZ FCDMQGYHBK BB PCIE EMWS RMQGFHZOVF. kYITR LH DZBBSSU QGE DIEDVAXG SBNFHAVNB FKOK QAROOKIWA ZOJ AGUOZ TPR "DIELBYLBRNB ORBTEZA," NHQPT KFUTQ DFXUM MJOZNAG XGZNO XRKVR EMWSJ BB VHZMCYMWS TPR HQFNWZK, KHM rHR SIVP HYAB "LBWLIGURB RQFWV IEUNUQ" AVQ WVRT VF KFUTQ "AFNQGAU ZNNYMWWFN QQYSCOXZQQHJ KNDHTLLTL ... ...然后我们尝试破解$ cd ../dvigenere-creacker-0.1$ makegcc -O3 -c -o dvigenere.o dvigenere.cgcc -O3 -c -o dcaesar.o dcaesar.cgcc -O3 -o dvigenere-cracker main.c dvigenere.o dcaesar.o$./dvigenere-cracker ciphertext.txt plaintext.txtthe length of the key : 7the key : dorainm看下解密出来的明文Fed Cuts Key Interest Rate by a Half PointWASHINGTON, Sept. 18 — The Federal Reserve today lowered its benchmark interest rate by a half point,a forceful policy shift intended to limit the damage to the economy from the recent disorder in the housing and credit markets.Skip to next paragraphRelatedFederal Reserve StatementThe New York TimesWhile an interest rate cut was widely expected, there had been profound uncertainty about whether the Fed would choose a more cautious quarter-point reduction. But the bolder action and an accompanying statement, both approved by a unanimous vote of the central bank's policy-setting committee, made it clear the Fed decided the risks of a recession were too big to ignore."Developments in financial markets since the committee's last regular meeting have increased the uncertainty surrounding the economic outlook," the central bank said. Signaling that it might cut rates more if necessary in months ahead, it said it would "continue to assess" the economic outlook and "act as needed to foster price stability and sustainable economic growth."The decision, which reset the overnight lending rate between banks to 4.75 percent, was the Fed's first rate cut in four years.Stocks immediately soared. The Dow Jones industrial average had been up about 75 points shortly before the announcement at 2:15 p.m., and within seconds it jumped another 100 points. Shortly after 3:30 p.m., it was showing a gain of 300 points on the day, or more than 2 percent.For consumers, the Fed's move could mean lower borrowing costs on for mortgages and automobile loans. But the impact may be muted, because investors remain deeply anxious about the credit quality of mortgages and other long-term loans. The main problem in the past month has not been high rates so much as the availability of capital to complete deals.In a separate move to bolster the banking system, the Fed also said today that it had cut its discount lending rate, which applies to short-term emergency loans to banks, to 5.25 percent —also a half-point cut.This was the Federal Reserve's most abrupt reversal of course since January 2001, when it suddenly slashed rates at an unscheduled emergency meeting because of signs that the economy was slipping into a recession. The last half-point cut in the federal funds rate came in November 2002.Economists said that the Fed's move today was similarly pre-emptive. "Monetary policy makers are worried about growth being seriously compromised and are prepared to take whatever prudent steps they can to avoid a deep slump," said Joshua Shapiro, chief United States economist for MFR.Some aspects of today's Fed's move could fuel inflation fears. Gold, a traditional investment safe haven in times of inflation, soared immediately after the Fed's decision was announced. As United States interest rates became less attractive for investment, the value of the dollar against the euro touched a new low before recovering slightly, and oil prices continued to climb even further above $80 a barrel.In the stock market, financial stocks posted the biggest gains, reflecting the fact that banks now will face lower borrowing costs, which should help drive profits higher."Shock therapy," was the assessment of Ethan Harris, chief economist at Lehman Brothers.But Mr. Harris cautioned that the Fed stopped short of signaling a firm commitment to more rate reductions. While it dropped its previous statement that inflation was still the "predominant concern," which would argue against using lower rates to stimulate the economy, the Fed said that "inflation risks remain" and that it would "monitor inflation developments carefully ... ...果然在不知道密钥的情况下,破解了维吉尼亚(vigenere)密码这是一个成功的例子,用这种方式破解维吉尼亚(vigenere)会存在比较多的弊端,比如密钥中对称出现了相同的字符,密钥doradinm, 会被破解程序认为,当前加密使用的密钥长度是4!再比如,如果待解密的字符过少,或者运气不佳,相隔密钥长度获取的字母中,根本没有形成'e','t'位居前列的形式(统计出现概率最高的多个字母,然后排列组合那些字母来分析'e','t'的差值,可以稍微缓解这种倒霉运气),那么也无从谈破解了...时间仓促,就此结束!希望各位能有更好的方法或者建议!。
维吉尼亚(Vigenere)密码算法(Javascript实现加密与解密) 传统加密技术对于当今的⽹络安全发挥不了⼤作⽤,但每⼀本讲述密码学的书的开头都会率先介绍它们,因为它们是密码学的基础,是密码学的历史。
Vigenere密码就是⼀种传统加密技术,它是多表代换密码,能够有效改进单表代换密码的词频分布特征问题。
详细介绍请参考密码学相关书籍。
⼏乎每⼀本密码学的书在讲述Vigenere密码的章节都会有这么⼀个《Vigenere代换表》⽤户讲解Vigenere密码机制:A B C D E F G H I J K L M N O P Q R S T U V W X Y ZB C D E F G H I J K L M N O P Q R S T U V W X Y Z AC D E F G H I J K L M N O P Q R S T U V W X Y Z A BD E F G H I J K L M N O P Q R S T U V W X Y Z A B CE F G H I J K L M N O P Q R S T U V W X Y Z A B C DF G H I J K L M N O P Q R S T U V W X Y Z A B C D EG H I J K L M N O P Q R S T U V W X Y Z A B C D E FH I J K L M N O P Q R S T U V W X Y Z A B C D E F GI J K L M N O P Q R S T U V W X Y Z A B C D E F G HJ K L M N O P Q R S T U V W X Y Z A B C D E F G H IK L M N O P Q R S T U V W X Y Z A B C D E F G H I JL M N O P Q R S T U V W X Y Z A B C D E F G H I J KM N O P Q R S T U V W X Y Z A B C D E F G H I J K LN O P Q R S T U V W X Y Z A B C D E F G H I J K L MO P Q R S T U V W X Y Z A B C D E F G H I J K L M NP Q R S T U V W X Y Z A B C D E F G H I J K L M N OQ R S T U V W X Y Z A B C D E F G H I J K L M N O PR S T U V W X Y Z A B C D E F G H I J K L M N O P QS T U V W X Y Z A B C D E F G H I J K L M N O P Q RT U V W X Y Z A B C D E F G H I J K L M N O P Q R SU V W X Y Z A B C D E F G H I J K L M N O P Q R S TV W X Y Z A B C D E F G H I J K L M N O P Q R S T UW X Y Z A B C D E F G H I J K L M N O P Q R S T U VX Y Z A B C D E F G H I J K L M N O P Q R S T U V WY Z A B C D E F G H I J K L M N O P Q R S T U V W XZ A B C D E F G H I J K L M N O P Q R S T U V W X Y 加密过程很简单,就是给定密钥字母x和明⽂字母y,密⽂字母是位于x⾏和y列的那个字母。
破解维吉尼亚密码题⽬密⽂为:KCCPKBGUFDPHQTYAVINRRTMVGRKDNBVFDETDGILTXRGUD DKOTFMBPVGEGLTGCKQRACQCWDNAWCRXIZAKFTLEWRPTYC QKYVXCHKFTPONCQQRHJVAJUWETMCMSPKQDYHJVDAHCTRL SVSKCGCZQQDZXGSFRLSWCW 1、Kasiski 测试法⾸先我们⽤Kasiski 测试法确定密钥字的长度。
密⽂串KC 在密⽂中的三处出现,起始位置在1,139,260,这三个数互素,所以未得到有效的信息2、计算重合指数确定密钥长度对该问题,使⽤如下代码计算其重合指数:cipher = 'KCCPKBGUFDPHQTYAVINRRTMVGRKDNBVFDETDGILTXRGUD\DKOTFMBPVGEGLTGCKQRACQCWDNAWCRXIZAKFTLEWRPTYC\QKYVXCHKFTPONCQQRHJVAJUWETMCMSPKQDYHJVDAHCTRL\SVSKCGCZQQDZXGSFRLSWCWSJTBHAFSIASPRJAHKJRJUMV\GKMITZHFPDISPZLVLGWTFPLKKEBDPGCEBSHCTJRWXBAFS\PEZQNRWXCVYCGAONWDDKACKAWBBIKFTIOVKCGGHJVLNHI\FFSQESVYCLACNVRWBBIREPBBVFEXOSCDYGZWPFDTKFQIY\CWHJVLNHIQIBTKHJVNPIST'# print(len(cipher))n = len(cipher)def getNum(code):#统计各字符出现频率count={}length = len(code)for i in range(len(code)):count[code[i]] = 0for i in range(len(code)):count[code[i]] += 1#print(count)#计算sum = 0for i in count:sum += count[i]*(count[i]-1)I = sum / (length*(length-1))# print(sum)# print((length*(length-1)))return I# print(getNum(cipher))def searchResult(code, k):print(k, "的结果:")step = int(n/k)sp = []for i in range(k):c = []for j in range(0, n, k):if j+i < n:c.append(code[j+i])c = ''.join(c)sp.append(c)for s in sp:if len(s) > 5:I = getNum(s)print(I)if I > 0.06:print('****************************************************')for i in range(1, 10):searchResult(cipher, i)输出结果为:1 的结果:0.0408718383495831552 的结果:0.0384615384615384640.047120045623039633 的结果:0.0559418457648546140.0481016731016731050.048262548262548264 的结果:0.037254901960784310.042742398164084910.0375788869764773350.049053356282271955 的结果:0.042581211589113260.043020193151887620.0325644504748982340.0352781546811397550.042966983265490736 的结果:0.06265664160401002****************************************************0.08376623376623377****************************************************0.049350649350649350.06493506493506493****************************************************0.042857142857142860.07337662337662337****************************************************7 的结果:0.0306122448979591830.0443262411347517750.043439716312056740.0407801418439716350.0443262411347517750.0443262411347517750.0407801418439716358 的结果:0.033222591362126250.040650406504065040.033681765389082460.040650406504065040.039488966318234610.045296167247386760.040650406504065040.05458768873403029 的结果:0.0512091038406827860.042674253200568990.06401137980085349****************************************************0.07539118065433854****************************************************0.040540540540540540.034534534534534530.043543543543543540.048048048048048050.042042042042042045由此可以初步判断出密钥长度为6。
package cn.longxuzi;import org.junit.Test;public class Chi_SquareUtils {private static final ICUtils icUtils = new ICUtils();/*** @author信息111李继华(LiJiHua)* @param String* [][]ciphertext* @return null* @Date 2013-11-21 PM 19:23* @copyright李继华所有*/@Testpublic void chi_square() {String[][] ciphertext = icUtils.devideCipherText();// 获取密文划分以后的每一小组的密文数组String[] freList = { "A=8.167", "B=1.492", "C=2.782", "D=4.253","E=12.702", "F=2.228", "G=2.015", "H=6.049", "I=6.996","J=0.153", "K=0.772", "L=4.025", "M=2.406", "N=6.749","O=7.507", "P=1.929", "Q=0.095", "R=5.987", "S=6.327","T=9.056", "U=2.758", "V=0.978", "W=2.360", "X=0.150","Y=1.974", "Z=0.074" };// 频率分布表构成的字符串数组int count = 0;// 计数器(计算每一个英文字母出现的次数)double index = 0;// 临时存储每一组密文的X^2得到的值double[] x = new double[ciphertext.length];// 创建存储每一组密文X值的double型数组容器int[][] sum1 = new int[ciphertext.length][freList.length];// 创建用来存储每一组每个英文字母出现的次数的整形数组容器double[][] sum2 = new double[ciphertext.length][freList.length];// 创建存储每一组每个密文字母的(C-E)/E 值for (int i = 0; i < ciphertext.length; i++) {// 第一层循环遍历根据密钥长度分成的每一小组密文for (int j = 0; j < freList.length; j++) {// 第二层循环遍历频率分布表中每个字母在每一小组密文中是否出现以及它们的频率String flag = freList[j].substring(0, 1);// 临时存储取到的第J个英文字母double E = Double.parseDouble(freList[j].substring(2,freList[j].length()));// 临时存储取到的第J个字母对应的频率值for (int k = 0; k < ciphertext[0].length; k++) {// 第三层循环遍历每一个密文小组中每一个密文字母if (flag.equals(ciphertext[i][k])) {// 根据字母表顺序依次判断每一个英文字母是否在密文小组中出现了count++;// 如果出现,开始计数每一个英文字母在小组密文字母表中总共出现的次数}}sum1[i][j] = count;// 赋值给用来存储字母出现次数的数组容器count = 0;// 计数器清零double dis = (double) (sum1[i][j] - E);sum2[i][j] = (dis * dis) / E;}}for (int i = 0; i < ciphertext.length; i++) {for (int j = 0; j < freList.length; j++) {index += sum2[i][j];}x[i] = index;index = 0;System.out.println("第" + (i + 1) + "小组的X值为" + Math.sqrt(x[i]));}}// 以下是计算每一个密文小组X值的测试代码public static void main(String[] args) {while (true) {Chi_SquareUtils ch = new Chi_SquareUtils();ch.chi_square();}}}。
维吉尼亚密码c语言源代码#include<stdio.h> // 头文件<stdio.h> , 声明标准输入输出函数#include<string.h> // 头文件<string.h> ,声明字符串操作函数char vigenere_table[26][26]; //定义维吉尼亚表void set_vigenere_table(){int i,j;int key; //行指针for (i = 0; i < 26; i++){key=i;for(j=0;j<26;j++){vigenere_table[i][j]='A'+(key%26);key++;}}printf("初始化维吉尼亚表完毕:\n\n");}void vigenere_encrypt(char* source,char* des) //定义加密函数{int source_len=strlen(source);int key_len=strlen(des);int i;for(i=0;i<source_len;i++) //对每个字母进行加密{int source_index= source[i]-'A'; //密文指针int key_index=des[i%key_len]-'A'; //维吉尼亚表指针int des_index=vigenere_table[key_index][source_index]-'A';des[i]=des_index+'A';}des[source_len]='\0';}void vigenere_decrypt(char* source,char* des) //定义解密函数{#define MAX_LEN 26int source_len=strlen(source);int key_len=strlen(des);int i;for(i=0;i<source_len;i++) //对每个字母进行解密{int key_index=des[i%key_len]-'A'; //维吉尼亚表指针int j;for(j=0;j<MAX_LEN;j++) //从维吉尼亚表中查找密文指针{if(vigenere_table[key_index][j]==source[i]){break;}}des[i]=j+'A';}des[source_len]='\0';}int main(){char plain[]="VIGENERECIPHER";char key[]="ABCD";char result[20];char result1[20];set_vigenere_table();vigenere_encrypt(plain,result);printf("明文: %s\n密钥: %s\n密文: %s\n",plain,key,result);vigenere_decrypt(result,result1);printf("解密后: %s\n",result1);return 0;}。
java实现维吉尼亚加密解密算法加密算法程序:public class mtoc{//输⼊明⽂和密钥,⽤输⼊的密钥对明⽂进⾏加密public static void main(String[] args){int i;char[] c=new char[100];char[] k1=new char[100];//输⼊System.out.print("enter a mingwen string:");String m=MyInput.readString();System.out.print("enter a key string:");String k=MyInput.readString();//构造密钥对照表for(i=0;i<k.length();i++){if(k.charAt(i)>='a'&&k.charAt(i)<='z')k1[i]=(char)(k.charAt(i)-97);if(k.charAt(i)>='A'&&k.charAt(i)<='Z')k1[i]=(char)(k.charAt(i)-65);}//加密for(i=0;i<m.length();i++){if(m.charAt(i)>='a'&&m.charAt(i)<='z')c[i]=(char)((m.charAt(i)-97+k1[i%k.length()])%26+97);if(m.charAt(i)>='A'&&m.charAt(i)<='Z')c[i]=(char)((m.charAt(i)-65+k1[i%k.length()])%26+65);}//输出密⽂for(i=0;i<c.length;i++)System.out.print(c[i]);}}解密算法程序:public class ctom{//输⼊密⽂和密钥,⽤密钥对密⽂解密public static void main(String[] args){int i;char[] m=new char[100];char[] k1=new char[100];//输⼊System.out.print("enter the miwen string:");String c=MyInput.readString();System.out.print("enter the key string:");String k=MyInput.readString();//构造密钥对照表for(i=0;i<k.length();i++){if(k.charAt(i)>='a'&&k.charAt(i)<='z')k1[i]=(char)(k.charAt(i)-97);if(k.charAt(i)>='A'&&k.charAt(i)<='Z')k1[i]=(char)(k.charAt(i)-65);}//解密for(i=0;i<c.length();i++){if(c.charAt(i)>='a'&&c.charAt(i)<='z')m[i]=(char)((c.charAt(i)-97-k1[i%k.length()]+26)%26+97);if(c.charAt(i)>='A'&&c.charAt(i)<='Z')m[i]=(char)((c.charAt(i)-65-k1[i%k.length()]+26)%26+65);}//输出明⽂for(i=0;i<m.length;i++)System.out.print(m[i]);}}需要⽤到的MyInput.java类:// MyInput.java: Contain the methods for reading int, double, and// string values from the keyboardimport java.io.*;public class MyInput{// Read a string from the keyboardpublic static String readString(){BufferedReader br= new BufferedReader(new InputStreamReader(System.in), 1);// Declare and initialize the stringString string = "";// Get the string from the keyboardtry{string = br.readLine();}catch (IOException ex){System.out.println(ex);}// Return the string obtained from the keyboardreturn string;}// Read an int value from the keyboardpublic static int readInt(){return Integer.parseInt(readString());}// Read a double value from the keyboardpublic static double readDouble(){return Double.parseDouble(readString());}// Read a byte value from the keyboardpublic static byte readByte(){return Byte.parseByte(readString());}// Read a short value from the keyboardpublic static short readShort(){return Short.parseShort(readString());}// Read a long value from the keyboardpublic static long readLong(){return Long.parseLong(readString());}// Read a float value from the keyboardpublic static float readFloat(){return Float.parseFloat(readString());}}加密算法程序:public class mtoc{//输⼊明⽂和密钥,⽤输⼊的密钥对明⽂进⾏加密public static void main(String[] args){int i;char[] c=new char[100];char[] k1=new char[100];//输⼊System.out.print("enter a mingwen string:");String m=MyInput.readString();System.out.print("enter a key string:");String k=MyInput.readString();//构造密钥对照表for(i=0;i<k.length();i++){if(k.charAt(i)>='a'&&k.charAt(i)<='z')k1[i]=(char)(k.charAt(i)-97);if(k.charAt(i)>='A'&&k.charAt(i)<='Z')k1[i]=(char)(k.charAt(i)-65);}//加密for(i=0;i<m.length();i++){if(m.charAt(i)>='a'&&m.charAt(i)<='z')c[i]=(char)((m.charAt(i)-97+k1[i%k.length()])%26+97);if(m.charAt(i)>='A'&&m.charAt(i)<='Z')c[i]=(char)((m.charAt(i)-65+k1[i%k.length()])%26+65);}//输出密⽂for(i=0;i<c.length;i++)System.out.print(c[i]);}}解密算法程序:public class ctom{//输⼊密⽂和密钥,⽤密钥对密⽂解密public static void main(String[] args){int i;char[] m=new char[100];char[] k1=new char[100];//输⼊System.out.print("enter the miwen string:");String c=MyInput.readString();System.out.print("enter the key string:");String k=MyInput.readString();//构造密钥对照表for(i=0;i<k.length();i++){if(k.charAt(i)>='a'&&k.charAt(i)<='z')k1[i]=(char)(k.charAt(i)-97);if(k.charAt(i)>='A'&&k.charAt(i)<='Z')k1[i]=(char)(k.charAt(i)-65);}//解密for(i=0;i<c.length();i++){if(c.charAt(i)>='a'&&c.charAt(i)<='z')m[i]=(char)((c.charAt(i)-97-k1[i%k.length()]+26)%26+97);if(c.charAt(i)>='A'&&c.charAt(i)<='Z')m[i]=(char)((c.charAt(i)-65-k1[i%k.length()]+26)%26+65);}//输出明⽂for(i=0;i<m.length;i++)System.out.print(m[i]);}}需要⽤到的MyInput.java类:// MyInput.java: Contain the methods for reading int, double, and // string values from the keyboardimport java.io.*;public class MyInput{// Read a string from the keyboardpublic static String readString(){BufferedReader br= new BufferedReader(new InputStreamReader(System.in), 1);// Declare and initialize the stringString string = "";// Get the string from the keyboardtry{string = br.readLine();}catch (IOException ex){System.out.println(ex);}// Return the string obtained from the keyboardreturn string;}// Read an int value from the keyboardpublic static int readInt(){return Integer.parseInt(readString());}// Read a double value from the keyboardpublic static double readDouble(){return Double.parseDouble(readString()); }// Read a byte value from the keyboard public static byte readByte(){return Byte.parseByte(readString());}// Read a short value from the keyboard public static short readShort(){return Short.parseShort(readString());}// Read a long value from the keyboard public static long readLong(){return Long.parseLong(readString());}// Read a float value from the keyboard public static float readFloat(){return Float.parseFloat(readString());}}。
《网络与信息安全》课程设计报告目录一、密码分析及密码系统设计原则 (2)二、维吉尼亚密码 (3)(一)维吉尼亚密码的介绍 (3)(二)维吉尼亚密码的原理 (4)(三)维吉尼亚密码-特点 (5)(四)维吉尼亚密码的演变 (7)(五)维吉尼亚密码下的扩展 (7)三、界面的设计 (15)(一)软件启动封面设计- (16)(二)软件框架设计 (16)(三)软件按钮设计 (16)(四)菜单设计 (16)(五)标签设计 (17)(六)图标设计 (17)(七)滚动条及状态栏设计 (17)(八)安装过程设计 (17)(九)包装及商品化 (17)四、加密的意义 (18)总结 (18)致谢词 (19)参考文献 (19)摘要使用维吉尼亚密码加密只是众多密码加密方法中的一个,本设计是基于维吉尼亚密码的一种扩展,能起到更强的加密,完成所有属于ACSII的加密,本人主要负责设计的规划,程序设计,界面设计,报告的完成等工作,整个程序是Java平台下实现的。
本次设计方法的原理以及方法仍然以维吉尼亚通用的方法来介绍。
关键词:维吉尼亚加密,密钥,加密,解密。
一、密码分析及密码系统设计原则对通信双方而言,通信的一方将信息加密后发送给另一方,是为了使攻击者即使得到密文也无法读懂。
对于攻击者来说,在不知道密钥的情况下,要想读懂密文,就要根据他的知识以及掌握的情报来进行密码分析。
密码分析是研究密钥未知的情况下恢复明文的科学。
通常假设攻击者知道正在使用的密码体制,当然,如果分析者不知道正在使用的密码体制,分析起来将更加困难。
成功的密码分析可能直接恢复明文或密钥,也可能找出保密系统的弱点来恢复明文或密钥。
根据攻击者具有的知识和掌握的情报,可以将密码分析分为以下几种类型:(1)只有密文的攻击。
攻击者有一些密文,它们是使用同一加密算法和同一密钥加密的。
(2)已知明文的攻击。
攻击者不仅得到一些密文,而且能得到这些密文对应的明文。
(3)选择明文的攻击。