Rust UTF-8 error, thread 'main' panicked at 'called `Result::unwrap()










-3















I'm working on cryptopals challenge set 1 challenge 3:




The hex encoded string:



1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736


... has been XOR'd against a single character. Find the key, decrypt the message.



You can do this by hand. But don't: write code to do it for you.



How? Devise some method for "scoring" a piece of English plaintext. Character frequency is a good metric. Evaluate each output and choose the one with the best score.




I've programmed this:



extern crate num_traits;

use num_traits::pow;
use std::io;

fn main()
let mut hex_string = String::new();
let mut bin_string = String::new();
let mut vec_bin: Vec<String> = Vec::new();
let mut vec_plain: Vec<String> = Vec::new();
println!("Please enter the string: ");
io::stdin()
.read_line(&mut hex_string)
.expect("Failed to read line");
bin_string = string_to_hex(hex_string.trim());
for i in 0..256
let mut tmp = bin_string.clone();
vec_bin.push(xor(tmp, i));

for s in vec_bin
let mut vec_utf8: Vec<u8> = Vec::new();
for i in 0..(s.len() / 8)
vec_utf8.push(bits_to_int(s[(i * 8)..((i * 8) + 8)].to_string()));

vec_plain.push(String::from_utf8(vec_utf8).unwrap());

for s in vec_plain
println!("", s);



fn string_to_hex(text: &str) -> String
let mut hex_string = String::new();
for c in text.chars()
hex_string.push_str(&hex_to_bits(c));

hex_string


fn hex_to_bits(c: char) -> String
let mut result = String::new();
let x = c.to_digit(16).unwrap();
let mut tmp = x;
while tmp != 0
result = (tmp % 2).to_string() + &result;
tmp = tmp / 2;

while result.len() < 4
result = "0".to_string() + &result;

result


fn xor(s: String, u: u16) -> String
let mut result = String::new();
let bin = dec_to_bin(u);
for i in 0..(s.len() / 8)
let mut tmp = bin.clone();
result = result + &single_byte_xor(s[(i * 8)..((i * 8) + 8)].to_string(), tmp);

result


fn dec_to_bin(u: u16) -> String
let mut result = String::new();
let mut tmp = u;
while tmp != 0
result = (tmp % 2).to_string() + &result;
tmp = tmp / 2;

while result.len() < 8
result = "0".to_string() + &result;

result


fn single_byte_xor(s: String, u: String) -> String
let mut result = String::new();
for i in 0..s.len()
result


fn bits_to_int(s: String) -> u8
let mut result: u8 = 0;
let mut counter = 0;
for c in s.chars().rev()
let x = c.to_digit(10).unwrap();
if x == 1
result += pow(2u8, counter);

counter += 1;

result



When I run the program, I get this error:



Please enter the string: 

1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: FromUtf8Error bytes: [155, 183, 183, 179, 177, 182, 191, 248, 149, 155, 255, 171, 248, 180, 177, 179, 189, 248, 185, 248, 168, 183, 173, 182, 188, 248, 183, 190, 248, 186, 185, 187, 183, 182], error: Utf8Error valid_up_to: 0, error_len: Some(1) ', /checkout/src/libcore/result.rs:916:5
stack backtrace:
0: std::sys::unix::backtrace::tracing::imp::unwind_backtrace
at /checkout/src/libstd/sys/unix/backtrace/tracing/gcc_s.rs:49
1: std::sys_common::backtrace::print
at /checkout/src/libstd/sys_common/backtrace.rs:68
at /checkout/src/libstd/sys_common/backtrace.rs:57
2: std::panicking::default_hook::closure
at /checkout/src/libstd/panicking.rs:381
3: std::panicking::default_hook
at /checkout/src/libstd/panicking.rs:397
4: std::panicking::rust_panic_with_hook
at /checkout/src/libstd/panicking.rs:577
5: std::panicking::begin_panic
at /checkout/src/libstd/panicking.rs:538
6: std::panicking::begin_panic_fmt
at /checkout/src/libstd/panicking.rs:522
7: rust_begin_unwind
at /checkout/src/libstd/panicking.rs:498
8: core::panicking::panic_fmt
at /checkout/src/libcore/panicking.rs:71
9: core::result::unwrap_failed
at /checkout/src/libcore/macros.rs:23
10: <core::result::Result<T, E>>::unwrap
at /checkout/src/libcore/result.rs:782
11: nr3::main
at src/main.rs:24
12: std::rt::lang_start::closure
at /checkout/src/libstd/rt.rs:74
13: std::panicking::try::do_call
at /checkout/src/libstd/rt.rs:59
at /checkout/src/libstd/panicking.rs:480
14: __rust_maybe_catch_panic
at /checkout/src/libpanic_unwind/lib.rs:101
15: std::rt::lang_start_internal
at /checkout/src/libstd/panicking.rs:459
at /checkout/src/libstd/panic.rs:365
at /checkout/src/libstd/rt.rs:58
16: std::rt::lang_start
at /checkout/src/libstd/rt.rs:74
17: main
18: __libc_start_main
19: _start


I can not figure out what the problem is.










share|improve this question



















  • 4





    Well, that byte string isn't UTF-8.

    – Josh Lee
    Nov 15 '18 at 18:16






  • 2





    One thing to try first when getting an error is to search the web for information. Your error is a FromUtf8Error, so I suggest you google that and click the first hit. In general, you may want to add more context to the search terms, e.g. "Rust", but in this case just "FromUtf8Error" does the trick. The documentation of the error should be self-explanatory. As a solution, I suggest you work with byte vectors instead of strings, or simply discard everything with character values above 127.

    – Sven Marnach
    Nov 15 '18 at 18:58






  • 1





    Looking at the data, the easiest solution seems to be to change for i in 0..256 to for i in 0..128.

    – Sven Marnach
    Nov 15 '18 at 19:02















-3















I'm working on cryptopals challenge set 1 challenge 3:




The hex encoded string:



1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736


... has been XOR'd against a single character. Find the key, decrypt the message.



You can do this by hand. But don't: write code to do it for you.



How? Devise some method for "scoring" a piece of English plaintext. Character frequency is a good metric. Evaluate each output and choose the one with the best score.




I've programmed this:



extern crate num_traits;

use num_traits::pow;
use std::io;

fn main()
let mut hex_string = String::new();
let mut bin_string = String::new();
let mut vec_bin: Vec<String> = Vec::new();
let mut vec_plain: Vec<String> = Vec::new();
println!("Please enter the string: ");
io::stdin()
.read_line(&mut hex_string)
.expect("Failed to read line");
bin_string = string_to_hex(hex_string.trim());
for i in 0..256
let mut tmp = bin_string.clone();
vec_bin.push(xor(tmp, i));

for s in vec_bin
let mut vec_utf8: Vec<u8> = Vec::new();
for i in 0..(s.len() / 8)
vec_utf8.push(bits_to_int(s[(i * 8)..((i * 8) + 8)].to_string()));

vec_plain.push(String::from_utf8(vec_utf8).unwrap());

for s in vec_plain
println!("", s);



fn string_to_hex(text: &str) -> String
let mut hex_string = String::new();
for c in text.chars()
hex_string.push_str(&hex_to_bits(c));

hex_string


fn hex_to_bits(c: char) -> String
let mut result = String::new();
let x = c.to_digit(16).unwrap();
let mut tmp = x;
while tmp != 0
result = (tmp % 2).to_string() + &result;
tmp = tmp / 2;

while result.len() < 4
result = "0".to_string() + &result;

result


fn xor(s: String, u: u16) -> String
let mut result = String::new();
let bin = dec_to_bin(u);
for i in 0..(s.len() / 8)
let mut tmp = bin.clone();
result = result + &single_byte_xor(s[(i * 8)..((i * 8) + 8)].to_string(), tmp);

result


fn dec_to_bin(u: u16) -> String
let mut result = String::new();
let mut tmp = u;
while tmp != 0
result = (tmp % 2).to_string() + &result;
tmp = tmp / 2;

while result.len() < 8
result = "0".to_string() + &result;

result


fn single_byte_xor(s: String, u: String) -> String
let mut result = String::new();
for i in 0..s.len()
result


fn bits_to_int(s: String) -> u8
let mut result: u8 = 0;
let mut counter = 0;
for c in s.chars().rev()
let x = c.to_digit(10).unwrap();
if x == 1
result += pow(2u8, counter);

counter += 1;

result



When I run the program, I get this error:



Please enter the string: 

1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: FromUtf8Error bytes: [155, 183, 183, 179, 177, 182, 191, 248, 149, 155, 255, 171, 248, 180, 177, 179, 189, 248, 185, 248, 168, 183, 173, 182, 188, 248, 183, 190, 248, 186, 185, 187, 183, 182], error: Utf8Error valid_up_to: 0, error_len: Some(1) ', /checkout/src/libcore/result.rs:916:5
stack backtrace:
0: std::sys::unix::backtrace::tracing::imp::unwind_backtrace
at /checkout/src/libstd/sys/unix/backtrace/tracing/gcc_s.rs:49
1: std::sys_common::backtrace::print
at /checkout/src/libstd/sys_common/backtrace.rs:68
at /checkout/src/libstd/sys_common/backtrace.rs:57
2: std::panicking::default_hook::closure
at /checkout/src/libstd/panicking.rs:381
3: std::panicking::default_hook
at /checkout/src/libstd/panicking.rs:397
4: std::panicking::rust_panic_with_hook
at /checkout/src/libstd/panicking.rs:577
5: std::panicking::begin_panic
at /checkout/src/libstd/panicking.rs:538
6: std::panicking::begin_panic_fmt
at /checkout/src/libstd/panicking.rs:522
7: rust_begin_unwind
at /checkout/src/libstd/panicking.rs:498
8: core::panicking::panic_fmt
at /checkout/src/libcore/panicking.rs:71
9: core::result::unwrap_failed
at /checkout/src/libcore/macros.rs:23
10: <core::result::Result<T, E>>::unwrap
at /checkout/src/libcore/result.rs:782
11: nr3::main
at src/main.rs:24
12: std::rt::lang_start::closure
at /checkout/src/libstd/rt.rs:74
13: std::panicking::try::do_call
at /checkout/src/libstd/rt.rs:59
at /checkout/src/libstd/panicking.rs:480
14: __rust_maybe_catch_panic
at /checkout/src/libpanic_unwind/lib.rs:101
15: std::rt::lang_start_internal
at /checkout/src/libstd/panicking.rs:459
at /checkout/src/libstd/panic.rs:365
at /checkout/src/libstd/rt.rs:58
16: std::rt::lang_start
at /checkout/src/libstd/rt.rs:74
17: main
18: __libc_start_main
19: _start


I can not figure out what the problem is.










share|improve this question



















  • 4





    Well, that byte string isn't UTF-8.

    – Josh Lee
    Nov 15 '18 at 18:16






  • 2





    One thing to try first when getting an error is to search the web for information. Your error is a FromUtf8Error, so I suggest you google that and click the first hit. In general, you may want to add more context to the search terms, e.g. "Rust", but in this case just "FromUtf8Error" does the trick. The documentation of the error should be self-explanatory. As a solution, I suggest you work with byte vectors instead of strings, or simply discard everything with character values above 127.

    – Sven Marnach
    Nov 15 '18 at 18:58






  • 1





    Looking at the data, the easiest solution seems to be to change for i in 0..256 to for i in 0..128.

    – Sven Marnach
    Nov 15 '18 at 19:02













-3












-3








-3








I'm working on cryptopals challenge set 1 challenge 3:




The hex encoded string:



1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736


... has been XOR'd against a single character. Find the key, decrypt the message.



You can do this by hand. But don't: write code to do it for you.



How? Devise some method for "scoring" a piece of English plaintext. Character frequency is a good metric. Evaluate each output and choose the one with the best score.




I've programmed this:



extern crate num_traits;

use num_traits::pow;
use std::io;

fn main()
let mut hex_string = String::new();
let mut bin_string = String::new();
let mut vec_bin: Vec<String> = Vec::new();
let mut vec_plain: Vec<String> = Vec::new();
println!("Please enter the string: ");
io::stdin()
.read_line(&mut hex_string)
.expect("Failed to read line");
bin_string = string_to_hex(hex_string.trim());
for i in 0..256
let mut tmp = bin_string.clone();
vec_bin.push(xor(tmp, i));

for s in vec_bin
let mut vec_utf8: Vec<u8> = Vec::new();
for i in 0..(s.len() / 8)
vec_utf8.push(bits_to_int(s[(i * 8)..((i * 8) + 8)].to_string()));

vec_plain.push(String::from_utf8(vec_utf8).unwrap());

for s in vec_plain
println!("", s);



fn string_to_hex(text: &str) -> String
let mut hex_string = String::new();
for c in text.chars()
hex_string.push_str(&hex_to_bits(c));

hex_string


fn hex_to_bits(c: char) -> String
let mut result = String::new();
let x = c.to_digit(16).unwrap();
let mut tmp = x;
while tmp != 0
result = (tmp % 2).to_string() + &result;
tmp = tmp / 2;

while result.len() < 4
result = "0".to_string() + &result;

result


fn xor(s: String, u: u16) -> String
let mut result = String::new();
let bin = dec_to_bin(u);
for i in 0..(s.len() / 8)
let mut tmp = bin.clone();
result = result + &single_byte_xor(s[(i * 8)..((i * 8) + 8)].to_string(), tmp);

result


fn dec_to_bin(u: u16) -> String
let mut result = String::new();
let mut tmp = u;
while tmp != 0
result = (tmp % 2).to_string() + &result;
tmp = tmp / 2;

while result.len() < 8
result = "0".to_string() + &result;

result


fn single_byte_xor(s: String, u: String) -> String
let mut result = String::new();
for i in 0..s.len()
result


fn bits_to_int(s: String) -> u8
let mut result: u8 = 0;
let mut counter = 0;
for c in s.chars().rev()
let x = c.to_digit(10).unwrap();
if x == 1
result += pow(2u8, counter);

counter += 1;

result



When I run the program, I get this error:



Please enter the string: 

1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: FromUtf8Error bytes: [155, 183, 183, 179, 177, 182, 191, 248, 149, 155, 255, 171, 248, 180, 177, 179, 189, 248, 185, 248, 168, 183, 173, 182, 188, 248, 183, 190, 248, 186, 185, 187, 183, 182], error: Utf8Error valid_up_to: 0, error_len: Some(1) ', /checkout/src/libcore/result.rs:916:5
stack backtrace:
0: std::sys::unix::backtrace::tracing::imp::unwind_backtrace
at /checkout/src/libstd/sys/unix/backtrace/tracing/gcc_s.rs:49
1: std::sys_common::backtrace::print
at /checkout/src/libstd/sys_common/backtrace.rs:68
at /checkout/src/libstd/sys_common/backtrace.rs:57
2: std::panicking::default_hook::closure
at /checkout/src/libstd/panicking.rs:381
3: std::panicking::default_hook
at /checkout/src/libstd/panicking.rs:397
4: std::panicking::rust_panic_with_hook
at /checkout/src/libstd/panicking.rs:577
5: std::panicking::begin_panic
at /checkout/src/libstd/panicking.rs:538
6: std::panicking::begin_panic_fmt
at /checkout/src/libstd/panicking.rs:522
7: rust_begin_unwind
at /checkout/src/libstd/panicking.rs:498
8: core::panicking::panic_fmt
at /checkout/src/libcore/panicking.rs:71
9: core::result::unwrap_failed
at /checkout/src/libcore/macros.rs:23
10: <core::result::Result<T, E>>::unwrap
at /checkout/src/libcore/result.rs:782
11: nr3::main
at src/main.rs:24
12: std::rt::lang_start::closure
at /checkout/src/libstd/rt.rs:74
13: std::panicking::try::do_call
at /checkout/src/libstd/rt.rs:59
at /checkout/src/libstd/panicking.rs:480
14: __rust_maybe_catch_panic
at /checkout/src/libpanic_unwind/lib.rs:101
15: std::rt::lang_start_internal
at /checkout/src/libstd/panicking.rs:459
at /checkout/src/libstd/panic.rs:365
at /checkout/src/libstd/rt.rs:58
16: std::rt::lang_start
at /checkout/src/libstd/rt.rs:74
17: main
18: __libc_start_main
19: _start


I can not figure out what the problem is.










share|improve this question
















I'm working on cryptopals challenge set 1 challenge 3:




The hex encoded string:



1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736


... has been XOR'd against a single character. Find the key, decrypt the message.



You can do this by hand. But don't: write code to do it for you.



How? Devise some method for "scoring" a piece of English plaintext. Character frequency is a good metric. Evaluate each output and choose the one with the best score.




I've programmed this:



extern crate num_traits;

use num_traits::pow;
use std::io;

fn main()
let mut hex_string = String::new();
let mut bin_string = String::new();
let mut vec_bin: Vec<String> = Vec::new();
let mut vec_plain: Vec<String> = Vec::new();
println!("Please enter the string: ");
io::stdin()
.read_line(&mut hex_string)
.expect("Failed to read line");
bin_string = string_to_hex(hex_string.trim());
for i in 0..256
let mut tmp = bin_string.clone();
vec_bin.push(xor(tmp, i));

for s in vec_bin
let mut vec_utf8: Vec<u8> = Vec::new();
for i in 0..(s.len() / 8)
vec_utf8.push(bits_to_int(s[(i * 8)..((i * 8) + 8)].to_string()));

vec_plain.push(String::from_utf8(vec_utf8).unwrap());

for s in vec_plain
println!("", s);



fn string_to_hex(text: &str) -> String
let mut hex_string = String::new();
for c in text.chars()
hex_string.push_str(&hex_to_bits(c));

hex_string


fn hex_to_bits(c: char) -> String
let mut result = String::new();
let x = c.to_digit(16).unwrap();
let mut tmp = x;
while tmp != 0
result = (tmp % 2).to_string() + &result;
tmp = tmp / 2;

while result.len() < 4
result = "0".to_string() + &result;

result


fn xor(s: String, u: u16) -> String
let mut result = String::new();
let bin = dec_to_bin(u);
for i in 0..(s.len() / 8)
let mut tmp = bin.clone();
result = result + &single_byte_xor(s[(i * 8)..((i * 8) + 8)].to_string(), tmp);

result


fn dec_to_bin(u: u16) -> String
let mut result = String::new();
let mut tmp = u;
while tmp != 0
result = (tmp % 2).to_string() + &result;
tmp = tmp / 2;

while result.len() < 8
result = "0".to_string() + &result;

result


fn single_byte_xor(s: String, u: String) -> String
let mut result = String::new();
for i in 0..s.len()
result


fn bits_to_int(s: String) -> u8
let mut result: u8 = 0;
let mut counter = 0;
for c in s.chars().rev()
let x = c.to_digit(10).unwrap();
if x == 1
result += pow(2u8, counter);

counter += 1;

result



When I run the program, I get this error:



Please enter the string: 

1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: FromUtf8Error bytes: [155, 183, 183, 179, 177, 182, 191, 248, 149, 155, 255, 171, 248, 180, 177, 179, 189, 248, 185, 248, 168, 183, 173, 182, 188, 248, 183, 190, 248, 186, 185, 187, 183, 182], error: Utf8Error valid_up_to: 0, error_len: Some(1) ', /checkout/src/libcore/result.rs:916:5
stack backtrace:
0: std::sys::unix::backtrace::tracing::imp::unwind_backtrace
at /checkout/src/libstd/sys/unix/backtrace/tracing/gcc_s.rs:49
1: std::sys_common::backtrace::print
at /checkout/src/libstd/sys_common/backtrace.rs:68
at /checkout/src/libstd/sys_common/backtrace.rs:57
2: std::panicking::default_hook::closure
at /checkout/src/libstd/panicking.rs:381
3: std::panicking::default_hook
at /checkout/src/libstd/panicking.rs:397
4: std::panicking::rust_panic_with_hook
at /checkout/src/libstd/panicking.rs:577
5: std::panicking::begin_panic
at /checkout/src/libstd/panicking.rs:538
6: std::panicking::begin_panic_fmt
at /checkout/src/libstd/panicking.rs:522
7: rust_begin_unwind
at /checkout/src/libstd/panicking.rs:498
8: core::panicking::panic_fmt
at /checkout/src/libcore/panicking.rs:71
9: core::result::unwrap_failed
at /checkout/src/libcore/macros.rs:23
10: <core::result::Result<T, E>>::unwrap
at /checkout/src/libcore/result.rs:782
11: nr3::main
at src/main.rs:24
12: std::rt::lang_start::closure
at /checkout/src/libstd/rt.rs:74
13: std::panicking::try::do_call
at /checkout/src/libstd/rt.rs:59
at /checkout/src/libstd/panicking.rs:480
14: __rust_maybe_catch_panic
at /checkout/src/libpanic_unwind/lib.rs:101
15: std::rt::lang_start_internal
at /checkout/src/libstd/panicking.rs:459
at /checkout/src/libstd/panic.rs:365
at /checkout/src/libstd/rt.rs:58
16: std::rt::lang_start
at /checkout/src/libstd/rt.rs:74
17: main
18: __libc_start_main
19: _start


I can not figure out what the problem is.







utf-8 rust






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 1:54









Shepmaster

159k16327471




159k16327471










asked Nov 15 '18 at 18:11









jupperjupper

267




267







  • 4





    Well, that byte string isn't UTF-8.

    – Josh Lee
    Nov 15 '18 at 18:16






  • 2





    One thing to try first when getting an error is to search the web for information. Your error is a FromUtf8Error, so I suggest you google that and click the first hit. In general, you may want to add more context to the search terms, e.g. "Rust", but in this case just "FromUtf8Error" does the trick. The documentation of the error should be self-explanatory. As a solution, I suggest you work with byte vectors instead of strings, or simply discard everything with character values above 127.

    – Sven Marnach
    Nov 15 '18 at 18:58






  • 1





    Looking at the data, the easiest solution seems to be to change for i in 0..256 to for i in 0..128.

    – Sven Marnach
    Nov 15 '18 at 19:02












  • 4





    Well, that byte string isn't UTF-8.

    – Josh Lee
    Nov 15 '18 at 18:16






  • 2





    One thing to try first when getting an error is to search the web for information. Your error is a FromUtf8Error, so I suggest you google that and click the first hit. In general, you may want to add more context to the search terms, e.g. "Rust", but in this case just "FromUtf8Error" does the trick. The documentation of the error should be self-explanatory. As a solution, I suggest you work with byte vectors instead of strings, or simply discard everything with character values above 127.

    – Sven Marnach
    Nov 15 '18 at 18:58






  • 1





    Looking at the data, the easiest solution seems to be to change for i in 0..256 to for i in 0..128.

    – Sven Marnach
    Nov 15 '18 at 19:02







4




4





Well, that byte string isn't UTF-8.

– Josh Lee
Nov 15 '18 at 18:16





Well, that byte string isn't UTF-8.

– Josh Lee
Nov 15 '18 at 18:16




2




2





One thing to try first when getting an error is to search the web for information. Your error is a FromUtf8Error, so I suggest you google that and click the first hit. In general, you may want to add more context to the search terms, e.g. "Rust", but in this case just "FromUtf8Error" does the trick. The documentation of the error should be self-explanatory. As a solution, I suggest you work with byte vectors instead of strings, or simply discard everything with character values above 127.

– Sven Marnach
Nov 15 '18 at 18:58





One thing to try first when getting an error is to search the web for information. Your error is a FromUtf8Error, so I suggest you google that and click the first hit. In general, you may want to add more context to the search terms, e.g. "Rust", but in this case just "FromUtf8Error" does the trick. The documentation of the error should be self-explanatory. As a solution, I suggest you work with byte vectors instead of strings, or simply discard everything with character values above 127.

– Sven Marnach
Nov 15 '18 at 18:58




1




1





Looking at the data, the easiest solution seems to be to change for i in 0..256 to for i in 0..128.

– Sven Marnach
Nov 15 '18 at 19:02





Looking at the data, the easiest solution seems to be to change for i in 0..256 to for i in 0..128.

– Sven Marnach
Nov 15 '18 at 19:02












2 Answers
2






active

oldest

votes


















0














I solved it by changing this



for s in vec_bin 
let mut vec_utf8: Vec<u8> = Vec::new();
for i in 0..(s.len()/8)
vec_utf8.push(bits_to_int(s[(i*8)..((i*8)+8)].to_string()));

vec_plain.push(String::from_utf8(vec_utf8).unwrap());



to this



for s in vec_bin 
let mut vec_utf8: Vec<u8> = Vec::new();
for i in 0..(s.len()/8)
vec_utf8.push(bits_to_int(s[(i*8)..((i*8)+8)].to_string()));

let mut tmp = String::new();
tmp.push_str(&String::from_utf8_lossy(&vec_utf8));
vec_plain.push(tmp);







share|improve this answer























  • It doesn't really make sense to interpret the essentially random strings as UTF-8 data, since they just are not. The input data is all 7-bit ASCII, and it is a reasonable assumption that the output data is also 7-bit ASCII, so if you restrict the range of keys to seven bits, the result is guaranteed to be valid 7-bit ASCII (which implies valid UTF-8).

    – Sven Marnach
    Nov 15 '18 at 19:52


















0














You should be checking if the string you've got is a valid UTF-8 string. String::from_utf8 will check this for you and returns either Ok(s) or Err(_).
You should then match on the return value and either print the decoded string, or report a failure. Something like this does the trick.



use std::io; 

fn main()
let mut hex_string = "1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736";
let raw:Vec<u8> = hex_string_to_raw(hex_string);
for i in 0..255
// Get XOR'd version
let mut vec_utf8:Vec<u8> = raw.iter().map(


fn hex_string_to_raw(hex: &str) -> Vec<u8>
let chars:Vec<char> = hex.chars().collect();
let mut raw:Vec<u8> = vec!;
for cs in chars.chunks(2)
let c0:char = cs[0];
let c1:char = cs[1];
raw.push( (c0.to_digit(16).unwrap()*16 + c1.to_digit(16).unwrap()) as u8);

raw






share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53325547%2frust-utf-8-error-thread-main-panicked-at-called-resultunwrap%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    I solved it by changing this



    for s in vec_bin 
    let mut vec_utf8: Vec<u8> = Vec::new();
    for i in 0..(s.len()/8)
    vec_utf8.push(bits_to_int(s[(i*8)..((i*8)+8)].to_string()));

    vec_plain.push(String::from_utf8(vec_utf8).unwrap());



    to this



    for s in vec_bin 
    let mut vec_utf8: Vec<u8> = Vec::new();
    for i in 0..(s.len()/8)
    vec_utf8.push(bits_to_int(s[(i*8)..((i*8)+8)].to_string()));

    let mut tmp = String::new();
    tmp.push_str(&String::from_utf8_lossy(&vec_utf8));
    vec_plain.push(tmp);







    share|improve this answer























    • It doesn't really make sense to interpret the essentially random strings as UTF-8 data, since they just are not. The input data is all 7-bit ASCII, and it is a reasonable assumption that the output data is also 7-bit ASCII, so if you restrict the range of keys to seven bits, the result is guaranteed to be valid 7-bit ASCII (which implies valid UTF-8).

      – Sven Marnach
      Nov 15 '18 at 19:52















    0














    I solved it by changing this



    for s in vec_bin 
    let mut vec_utf8: Vec<u8> = Vec::new();
    for i in 0..(s.len()/8)
    vec_utf8.push(bits_to_int(s[(i*8)..((i*8)+8)].to_string()));

    vec_plain.push(String::from_utf8(vec_utf8).unwrap());



    to this



    for s in vec_bin 
    let mut vec_utf8: Vec<u8> = Vec::new();
    for i in 0..(s.len()/8)
    vec_utf8.push(bits_to_int(s[(i*8)..((i*8)+8)].to_string()));

    let mut tmp = String::new();
    tmp.push_str(&String::from_utf8_lossy(&vec_utf8));
    vec_plain.push(tmp);







    share|improve this answer























    • It doesn't really make sense to interpret the essentially random strings as UTF-8 data, since they just are not. The input data is all 7-bit ASCII, and it is a reasonable assumption that the output data is also 7-bit ASCII, so if you restrict the range of keys to seven bits, the result is guaranteed to be valid 7-bit ASCII (which implies valid UTF-8).

      – Sven Marnach
      Nov 15 '18 at 19:52













    0












    0








    0







    I solved it by changing this



    for s in vec_bin 
    let mut vec_utf8: Vec<u8> = Vec::new();
    for i in 0..(s.len()/8)
    vec_utf8.push(bits_to_int(s[(i*8)..((i*8)+8)].to_string()));

    vec_plain.push(String::from_utf8(vec_utf8).unwrap());



    to this



    for s in vec_bin 
    let mut vec_utf8: Vec<u8> = Vec::new();
    for i in 0..(s.len()/8)
    vec_utf8.push(bits_to_int(s[(i*8)..((i*8)+8)].to_string()));

    let mut tmp = String::new();
    tmp.push_str(&String::from_utf8_lossy(&vec_utf8));
    vec_plain.push(tmp);







    share|improve this answer













    I solved it by changing this



    for s in vec_bin 
    let mut vec_utf8: Vec<u8> = Vec::new();
    for i in 0..(s.len()/8)
    vec_utf8.push(bits_to_int(s[(i*8)..((i*8)+8)].to_string()));

    vec_plain.push(String::from_utf8(vec_utf8).unwrap());



    to this



    for s in vec_bin 
    let mut vec_utf8: Vec<u8> = Vec::new();
    for i in 0..(s.len()/8)
    vec_utf8.push(bits_to_int(s[(i*8)..((i*8)+8)].to_string()));

    let mut tmp = String::new();
    tmp.push_str(&String::from_utf8_lossy(&vec_utf8));
    vec_plain.push(tmp);








    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 15 '18 at 19:09









    jupperjupper

    267




    267












    • It doesn't really make sense to interpret the essentially random strings as UTF-8 data, since they just are not. The input data is all 7-bit ASCII, and it is a reasonable assumption that the output data is also 7-bit ASCII, so if you restrict the range of keys to seven bits, the result is guaranteed to be valid 7-bit ASCII (which implies valid UTF-8).

      – Sven Marnach
      Nov 15 '18 at 19:52

















    • It doesn't really make sense to interpret the essentially random strings as UTF-8 data, since they just are not. The input data is all 7-bit ASCII, and it is a reasonable assumption that the output data is also 7-bit ASCII, so if you restrict the range of keys to seven bits, the result is guaranteed to be valid 7-bit ASCII (which implies valid UTF-8).

      – Sven Marnach
      Nov 15 '18 at 19:52
















    It doesn't really make sense to interpret the essentially random strings as UTF-8 data, since they just are not. The input data is all 7-bit ASCII, and it is a reasonable assumption that the output data is also 7-bit ASCII, so if you restrict the range of keys to seven bits, the result is guaranteed to be valid 7-bit ASCII (which implies valid UTF-8).

    – Sven Marnach
    Nov 15 '18 at 19:52





    It doesn't really make sense to interpret the essentially random strings as UTF-8 data, since they just are not. The input data is all 7-bit ASCII, and it is a reasonable assumption that the output data is also 7-bit ASCII, so if you restrict the range of keys to seven bits, the result is guaranteed to be valid 7-bit ASCII (which implies valid UTF-8).

    – Sven Marnach
    Nov 15 '18 at 19:52













    0














    You should be checking if the string you've got is a valid UTF-8 string. String::from_utf8 will check this for you and returns either Ok(s) or Err(_).
    You should then match on the return value and either print the decoded string, or report a failure. Something like this does the trick.



    use std::io; 

    fn main()
    let mut hex_string = "1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736";
    let raw:Vec<u8> = hex_string_to_raw(hex_string);
    for i in 0..255
    // Get XOR'd version
    let mut vec_utf8:Vec<u8> = raw.iter().map(


    fn hex_string_to_raw(hex: &str) -> Vec<u8>
    let chars:Vec<char> = hex.chars().collect();
    let mut raw:Vec<u8> = vec!;
    for cs in chars.chunks(2)
    let c0:char = cs[0];
    let c1:char = cs[1];
    raw.push( (c0.to_digit(16).unwrap()*16 + c1.to_digit(16).unwrap()) as u8);

    raw






    share|improve this answer



























      0














      You should be checking if the string you've got is a valid UTF-8 string. String::from_utf8 will check this for you and returns either Ok(s) or Err(_).
      You should then match on the return value and either print the decoded string, or report a failure. Something like this does the trick.



      use std::io; 

      fn main()
      let mut hex_string = "1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736";
      let raw:Vec<u8> = hex_string_to_raw(hex_string);
      for i in 0..255
      // Get XOR'd version
      let mut vec_utf8:Vec<u8> = raw.iter().map(


      fn hex_string_to_raw(hex: &str) -> Vec<u8>
      let chars:Vec<char> = hex.chars().collect();
      let mut raw:Vec<u8> = vec!;
      for cs in chars.chunks(2)
      let c0:char = cs[0];
      let c1:char = cs[1];
      raw.push( (c0.to_digit(16).unwrap()*16 + c1.to_digit(16).unwrap()) as u8);

      raw






      share|improve this answer

























        0












        0








        0







        You should be checking if the string you've got is a valid UTF-8 string. String::from_utf8 will check this for you and returns either Ok(s) or Err(_).
        You should then match on the return value and either print the decoded string, or report a failure. Something like this does the trick.



        use std::io; 

        fn main()
        let mut hex_string = "1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736";
        let raw:Vec<u8> = hex_string_to_raw(hex_string);
        for i in 0..255
        // Get XOR'd version
        let mut vec_utf8:Vec<u8> = raw.iter().map(


        fn hex_string_to_raw(hex: &str) -> Vec<u8>
        let chars:Vec<char> = hex.chars().collect();
        let mut raw:Vec<u8> = vec!;
        for cs in chars.chunks(2)
        let c0:char = cs[0];
        let c1:char = cs[1];
        raw.push( (c0.to_digit(16).unwrap()*16 + c1.to_digit(16).unwrap()) as u8);

        raw






        share|improve this answer













        You should be checking if the string you've got is a valid UTF-8 string. String::from_utf8 will check this for you and returns either Ok(s) or Err(_).
        You should then match on the return value and either print the decoded string, or report a failure. Something like this does the trick.



        use std::io; 

        fn main()
        let mut hex_string = "1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736";
        let raw:Vec<u8> = hex_string_to_raw(hex_string);
        for i in 0..255
        // Get XOR'd version
        let mut vec_utf8:Vec<u8> = raw.iter().map(


        fn hex_string_to_raw(hex: &str) -> Vec<u8>
        let chars:Vec<char> = hex.chars().collect();
        let mut raw:Vec<u8> = vec!;
        for cs in chars.chunks(2)
        let c0:char = cs[0];
        let c1:char = cs[1];
        raw.push( (c0.to_digit(16).unwrap()*16 + c1.to_digit(16).unwrap()) as u8);

        raw







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 16 '18 at 1:18









        Michael AndersonMichael Anderson

        46.3k699150




        46.3k699150



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53325547%2frust-utf-8-error-thread-main-panicked-at-called-resultunwrap%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

            Museum of Modern and Contemporary Art of Trento and Rovereto