标题: [原创代码] 德国的enigma加密技术例子 - JavaScript [打印本页]
作者: yu2n 时间: 2013-2-4 04:10 标题: 德国的enigma加密技术例子 - JavaScript
本帖最后由 yu2n 于 2013-2-7 05:59 编辑
作者:yu2n【原创】
EnigmaEncrypt 函数说明:
strSRC 待加密字符串
strKEY 密钥
strMatrix 矩阵字符串
主程序代码:- function EnigmaEncrypt(strSRC, strKEY, strMatrix)
- {
- // 默认按ANSI码表(33~126)生成矩阵字符串,忽略空格 String.fromCharCode(32)
- if (strMatrix == "")
- {
- for (var i=33; i <= "~".charCodeAt(0); i++)
- {
- strMatrix = strMatrix + String.fromCharCode(i);
- };
- };
-
- // 根据矩阵字符串生成依次缩进1位的矩阵数组
- var LenMatrix = strMatrix.length;
- var arrCryptMatrix = new Array();
- arrCryptMatrix[0] = strMatrix;
- for (var i=0, j=1; i<LenMatrix-1; i++, j++)
- {
- var mov1 = arrCryptMatrix[i].substr(1, LenMatrix-1)
- var mov2 = arrCryptMatrix[i].charAt(0);
- arrCryptMatrix[j] = mov1 + mov2;
- }
-
- // 根据加密密钥查找矩阵数组,找到输入字符对应的字符
- var LenSRC = strSRC.length;
- var LenKEY = strKEY.length;
- var EncryptedString = "";
- for (var i=0, j=0; i<LenSRC; i++)
- {
- // 按密钥长度循环
- if (j >= LenKEY){j=0;};
-
- // 在矩阵中找出对应字符
- var C2E = strSRC.charAt(i);
- var MP = strMatrix.indexOf(C2E,0);
- var CWL = strKEY.charAt(j);
-
- if (MP == -1){
- // 跳过矩阵表中不存在的字符,并重置起始编码
- EncryptedString += C2E; j=0;
- } else {
- // 密钥字符串,右移一位
- j++;
- // 在矩阵中查找对应字符
- for (var m=0; m<LenMatrix; m++)
- {
- if (arrCryptMatrix[m].charAt(MP) == CWL){
- EncryptedString += arrCryptMatrix[m].charAt(0); break;
- }
- };
- }
- }
-
- return EncryptedString;
- }
复制代码
加密实例:
1. 加密前:- // 按ANSI码表(33~126)生成矩阵字符串
- var Matrix = "";
- for (var i=33; i <= "~".charCodeAt(0); i++)
- {
- Matrix = Matrix + String.fromCharCode(i);
- }
复制代码
2. 加密后:- kg 按YH^F码表rc~oid{f生成矩阵字符串
- $5? M5={1| ] xtv
- 4'? r~P{ 1Y~\_ 1 ^Y xv1a7.P{W'M*Y"+_q[ 1k(f
- }
- M5={1| ] M5={1| o G"?&,/%)('DL25?L+2Lg1mv
- {
复制代码
作者: yu2n 时间: 2013-2-4 04:13
另发一个VBS版本,来至网络上的VB代码- ' ====================================================================================================
- '德国的enigma加密类
- ' Download by http://www.codefans.net,yu2n 改写为VBS
- ' 使用:
- ' Msgbox GetEnigmaEncrypt("123456", "KATHER")
- Function GetEnigmaEncrypt( byVal EncryptStr, byVal KeyStr )
- Set MyEncrypt = New EnigmaEncrypt
- MyEncrypt.KeyString = ( KeyStr ) ' 初始密钥(6个字母,如"KATHER")
- GetEnigmaEncrypt = MyEncrypt.Encrypt( EncryptStr )
- End Function
- Class EnigmaEncrypt
- Private LCW 'As Integer 'Length of CodeWord
- Private LS2E 'As Integer 'Length of String to be Encrypted
- Private LAM 'As Integer 'Length of Array Matrix
- Private MP 'As Integer 'Matrix Position
- Private Matrix 'As String 'Starting Matrix
- Private mov1 'As String 'First Part of Replacement String
- Private mov2 'As String 'Second Part of Replacement String
- Private CodeWord 'As String 'CodeWord
- Private CWL 'As String 'CodeWord Letter
- Private EncryptedString 'As String 'String to Return for Encrypt or String to UnEncrypt for UnEncrypt
- Private EncryptedLetter 'As String 'Storage Variable for Character just Encrypted
- Private strCryptMatrix() 'As String 'yu2n 2013-1-28 生成动态数组 strCryptMatrix(LAM)
- 'Private strCryptMatrix(97) 'As String 'Matrix Array
-
- Public Property Let KeyString(sKeyString) 'As String)
- CodeWord = sKeyString
- End Property
-
- Public Function Encrypt(mstext) 'As String) As String
- Dim X 'As Integer ' Loop Counter
- Dim Y 'As Integer 'Loop Counter
- Dim Z 'As Integer 'Loop Counter
- Dim C2E 'As String 'Character to Encrypt
- Dim Str2Encrypt 'As String 'Text from TextBox
-
- Str2Encrypt = mstext
- LS2E = Len(mstext)
- LCW = Len(CodeWord)
- EncryptedLetter = ""
- EncryptedString = ""
-
- Y = 1
- For X = 1 To LS2E
- C2E = Mid(Str2Encrypt, X, 1)
- MP = InStr(1, Matrix, C2E, 0)
- CWL = Mid(CodeWord, Y, 1)
- For Z = 1 To LAM
- If Mid(strCryptMatrix(Z), MP, 1) = CWL Then
- EncryptedLetter = Left(strCryptMatrix(Z), 1)
- EncryptedString = EncryptedString + EncryptedLetter
- Exit For
- End If
- Next 'Z
- Y = Y + 1
- If Y > LCW Then Y = 1
- Next 'X
- Encrypt = EncryptedString
-
- End Function
-
- Private Sub Class_Initialize()
-
- Dim W 'As Integer 'Loop Counter to set up Matrix
- Dim X 'As Integer 'Loop through Matrix
- Dim I 'As Integer ' 生成ANSCII码
-
- 'yu2n 2013-1-11 按ANSI码表(32~126)生成母字符串
- For I = Asc(Chr(32)) To Asc(Chr(126))
- Matrix = Matrix + Chr(I)
- Next
- 'yu2n 2013-1-28 写入 .ini 文件时去掉空格与[]符号
- 'Matrix = Replace(Matrix, "[", "", 1, -1, 1) : Matrix = Replace(Matrix, "]", "", 1, -1, 1)
-
- 'Matrix = "8x3p5BeabcdfghijklmnoqrstuvwyzACDEFGHIJKLMNOPQRSTUVWXYZ 1246790-.#/\!@$<>&*()[]{}';:,?=+~`^|%_"
- 'Matrix = Matrix + Chr(13) 'Add Carriage Return to Matrix
- 'Matrix = Matrix + Chr(10) 'Add Line Feed to Matrix
- 'Matrix = Matrix + Chr(34) 'Add "
- ' Unique String used to make Matrix - 8x3p5Be
- ' Unique String can be any combination that has a character only ONCE.
- ' EACH Letter in the Matrix is Input ONLY once.
-
- W = 1
- 'yu2n 2013-1-28 生成动态数组 strCryptMatrix(LAM)
- LAM = Len(Matrix) : ReDim Preserve strCryptMatrix(LAM) : strCryptMatrix(1) = Matrix
-
- For X = 2 To LAM ' LAM = Length of Array Matrix
- mov1 = Left(strCryptMatrix(W), 1) 'First Character of strCryptMatrix
- mov2 = Right(strCryptMatrix(W), (LAM - 1)) 'All but First Character of strCryptMatrix
- strCryptMatrix(X) = mov2 & mov1 'Makes up each row of the Array
- W = W + 1
- Next 'X
-
- End Sub
- End Class
复制代码
作者: BAT-VBS 时间: 2013-2-4 13:14
感谢分享
作者: yu2n 时间: 2013-3-23 12:00
本帖最后由 yu2n 于 2013-3-23 12:12 编辑
在线体验:http://yu2n.tk/app/format/EnigmaEncoding.html
另存为HTA,有惊喜。- // 德国 Enigma Encrypt 加密技术
- function EnigmaEncrypt(strSRC, strMatrix, strKEY)
- {
- // 检测输入的参数,必须全部为字符串
- if (!(strSRC.constructor == String && strMatrix.constructor == String
- && strKEY.constructor == String)){return('args error!');};
-
- // 默认密钥字符串 Key
- if (strKEY == ''){strKEY = 'Fishing';};
-
- // 密钥加盐更安全 salt
- var salt = 'WoYeBuxiangJiayanA!ShangyeGongsiTaiKengdie!!!';
- strKEY += salt;
-
- // 默认的矩阵字符串 Matrix
- if (strMatrix.length == 0){
- // 按ANSI码表(33~126)生成矩阵字符串,忽略空格 String.fromCharCode(32)
- for (var i=33; i <= '~'.charCodeAt(0); i++)
- {
- strMatrix += String.fromCharCode(i);
- }
- }
-
- // 根据矩阵字符串生成依次缩进1位的矩阵数组
- var LenMatrix = strMatrix.length;
- var arrMatrix = new Array();
- arrMatrix[0] = strMatrix;
- for (var i=0, j=1; i<LenMatrix-1; i++, j++)
- {
- var mov1 = arrMatrix[i].substr(1, LenMatrix-1) // 读取‘当前数组元素’,截取除‘第一个字符’以外的所有字符
- var mov2 = arrMatrix[i].charAt(0); // 截取第一个字符,然后拼接至末尾,保存到‘下一个数组元素’
- arrMatrix[j] = mov1 + mov2;
- }
-
- // 根据加密密钥查找矩阵数组,找到输入字符对应的字符
- var LenSRC = strSRC.length; // 原字符串长度
- var LenKEY = strKEY.length; // 密钥长度
- var EncryptedString = ''; // 加密后的字符串
- for (var i=0, j=0; i<LenSRC; i++)
- {
- // 按密钥长度循环,超出长度重新开始
- if (j >= LenKEY){j=0;};
-
- // 换行重新开始
- if (strSRC.charAt(i)=='\r'){j=0;};
- if (strSRC.charAt(i)=='\n'){j=0;};
-
- // 在矩阵中找出对应字符
- var charKEY = strKEY.charAt(j); // 当前‘密钥截取字符’
- var charSRC = strSRC.charAt(i); // 当前‘原字符串截取字符’
- var MatrixPos = strMatrix.indexOf(charSRC,0); // 当前‘原字符串截取字符’在‘矩阵字符串’中的位置
-
- // 跳过矩阵表中不存在(位置返回-1)的字符
- if (MatrixPos == -1){
- EncryptedString += charSRC;
- } else {
- // 密钥字符串,右移一位
- j++;
- // 在矩阵中查找对用字符
- for (var m=0; m<LenMatrix; m++)
- {
- if (arrMatrix[m].charAt(MatrixPos) == charKEY){
- EncryptedString += arrMatrix[m].charAt(0); break;
- }
- };
- }
- }
- return(EncryptedString);
- }
复制代码
题外话,汉字有多少个?- // 生成汉字码表 从 \u4e00 至 \u0x9fa5
- var strMatrix='';
- for(var i=parseInt(0x4e00,10); i<parseInt(0x9fa5,10); i++){
- strMatrix += String.fromCharCode(i);
- }
复制代码
欢迎光临 批处理之家 (http://www.bathome.net/) |
Powered by Discuz! 7.2 |