//Encoding ecd; public TripleDESCipher(String textStr, byte[] textByte, DESKey desKey, bool flag) //加密&解密构造器 { if (flag == true) //加密 { doCipher(textByte, desKey); } else//解密 { textByte = new byte[textStr.Length / 8]; int[] textInt = new int[textStr.Length / 8]; char[] textChar = new char[textStr.Length / 8]; for (int i = 0; i < textByte.Length; i++) { textInt[i] = Convert.ToInt32(textStr.Substring(i * 8, 8), 2); textByte[i] = (byte)textInt[i]; } seq = 15; doCipher(textByte, desKey); for (int i = 0; i < textInt.Length; i++) { textInt[i] = Convert.ToInt32(outStr.ToString().Substring(i * 8, 8), 2); textByte[i] = (byte)textInt[i]; } outBytes = textByte; outStr.Remove(0, outStr.Length); outStr.Append(Encoding.Unicode.GetString(textByte)); } }
private void doCipher(byte[] textByte, DESKey desKey) { int loop = 0; int pos = 0; while (loop < textByte.Length / 8) { int[] text64 = new int[64]; int[] lr = new int[64]; int n = 0; for (int i = pos; i < pos + 8; i++) { for (int j = 7; j >= 0; j--) { text64[n++] = (textByte[i] >> j & 1); } } for (int i = 0; i < 32; i++)//生成l0 { l[i] = text64[IP[i] - 1]; } for (int i = 0; i < 32; i++) { r[i] = text64[IP[i + 32] - 1]; //生成r0 } dolr(desKey); //得到16次加密之后最终的l和r for (int i = 0; i < 32; i++) { lr[i] = r[i]; } for (int i = 0; i < 32; i++) { lr[i + 32] = l[i]; } for (int i = 0; i < 64; i++) { text64[i] = lr[IP_1[i] - 1]; outStr.Append(text64[i]); } pos += 8; loop++; } }
public static string Encrypt(string Source, string[] Key) { if (String.IsNullOrEmpty(Source)) { throw new Exception("没有输入明文!"); } if (Key.Length < 3) { throw new Exception("密匙个数不足(需3个密匙)!"); } else { int kl1 = Encoding.Default.GetBytes(Key[0]).Length; int kl2 = Encoding.Default.GetBytes(Key[1]).Length; int kl3 = Encoding.Default.GetBytes(Key[2]).Length; if (kl1 != 8 || kl2 != 8 || kl3 != 8) { throw new Exception("每个密钥必须均为8个字符或4个汉字!"); } } DESKey desKey1 = new DESKey(Key[0]); DESKey desKey2 = new DESKey(Key[1]); DESKey desKey3 = new DESKey(Key[2]); int space = (8 - (Encoding.Unicode.GetBytes(Source)).Length % 8) % 8; for (int i = 0; i < space; i++) { Source = Source + " "; } byte[] textByte = Encoding.Unicode.GetBytes(Source); TripleDESCipher cipher = new TripleDESCipher(null, textByte, desKey1, true); TripleDESCipher decipher = new TripleDESCipher(cipher.getOutStr(), null, desKey2, false); cipher = new TripleDESCipher(null, decipher.getOutBytes(), desKey3, true); return(cipher.getOutStr()); }
public static string Decrypt(string Source, string[] Key) { if (String.IsNullOrEmpty(Source)) { throw new Exception("没有输入明文!"); } if (Key.Length < 3) { throw new Exception("密匙个数不足(需3个密匙)!"); } else { int kl1 = Encoding.Default.GetBytes(Key[0]).Length; int kl2 = Encoding.Default.GetBytes(Key[1]).Length; int kl3 = Encoding.Default.GetBytes(Key[2]).Length; if (kl1 != 8 || kl2 != 8 || kl3 != 8) { throw new Exception("每个密钥必须均为8个字符或4个汉字!"); } } for (int i = 0; i < Source.Length; i++) { if ((!Source[i].ToString().Equals("1")) && (!Source[i].ToString().Equals("0"))) { throw new Exception("密文内容有误!(密文内容仅含有0或1)"); } } DESKey desKey1 = new DESKey(Key[0]); DESKey desKey2 = new DESKey(Key[1]); DESKey desKey3 = new DESKey(Key[2]); TripleDESCipher decipher = new TripleDESCipher(Source, null, desKey3, false); TripleDESCipher cipher = new TripleDESCipher(null, decipher.getOutBytes(), desKey2, true); decipher = new TripleDESCipher(cipher.getOutStr(), null, desKey1, false); return(Encoding.Unicode.GetString(decipher.getOutBytes())); }
private void dolr(DESKey desKey) { int[] r48 = new int[48]; int[] K; int[] r32 = this.r; for (int i = 0; i < 16; i++) { K = desKey.getK(Math.Abs(i - seq)); for (int j = 0; j < 48; j++) { r48[j] = (r32[E[j] - 1] + K[j]) % 2; } r32 = doSBox(r48);//进盒子 for (int j = 0; j < 32; j++) { r32[j] = (r32[j] + this.l[j]) % 2; } this.l = this.r; this.r = r32; } }