Guest User

Untitled

a guest
Feb 15th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. #[allow(dead_code)]
  2. fn rc4(n: Vec<u8>) -> Vec<u8> {
  3. let (mut s,mut j, mut t,l) = ((0..256).collect::<Vec<usize>>(),0,0,n[0] as usize);
  4. let (key,msg) = n[1..].split_at(l);
  5. (0..256).fold(0,|a,i| {
  6. j = (a + s[i] + key[i % l] as usize) % 256;
  7. t=s[i];s[i]=s[j];s[j]=t;j
  8. });
  9. j=0;
  10. (1..).zip(msg.iter()).map(|(i,b)| {
  11. j = (j + s[i]) % 256;
  12. t=s[i];s[i]=s[j];s[j]=t;
  13. s[(s[i] + s[j]) % 256] as u8 ^ b
  14. }).collect::<Vec<u8>>()
  15. }
  16.  
  17. #[cfg(test)] // cfg -> section will only compiled during 'cargo test'
  18. mod tests {
  19. // namespace helper
  20. use super::*; // bring in our functions above
  21. #[test] // next function will be a single test
  22. fn test_rc4() {
  23. assert!(
  24. rc4(b"\x0Dthis is a keythis is some data to encrypt".to_vec())
  25. == b"\xb5\xdb?i\x1f\x92\x96\x96e!\xf3\xae(!\xf3\xeaC\xd4\x9fS\xbd?d\x82\x84{\xcdN"
  26. .to_vec()
  27. );
  28. assert!(rc4(b"\x01\x00\x00\x00\x00\x00\x00".to_vec()) == b"\xde\x18\x89\x41\xa3".to_vec());
  29. assert!( rc4( b"Sthis is a rather long key because the value of S is 83 so the key length must matchand this is the data to be encrypted".to_vec())
  30. ==b"\x96\x1f,\x8f\xa3%\x9b\xa3f[mk\xdf\xbc\xac\x8b\x8e\xfa\xfe\x96B=!\xfc;\x13`c\x16q\x04\x11\xd8\x86\xee\x07".to_vec() );
  31. assert!( rc4( b"\x0dthis is a key\xb5\xdb?i\x1f\x92\x96\x96e!\xf3\xae(!\xf3\xeaC\xd4\x9fS\xbd?d\x82\x84{\xcdN".to_vec() )==
  32. b"\x74\x68\x69\x73\x20\x69\x73\x20\x73\x6f\x6d\x65\x20\x64\x61\x74\x61\x20\x74\x6f\x20\x65\x6e\x63\x72\x79\x70\x74".to_vec() );
  33. } // test f() by itself (unit)
  34. }
Add Comment
Please, Sign In to add comment