imho.ws |
![]() |
![]() |
![]() |
# 4 |
Full Member
Регистрация: 20.01.2004
Адрес: Таллинн
Пол: Male
Сообщения: 623
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
Я пробовал на C#. Использовал стандартные функции из .NET
Код такой: MemoryStream memoryStream = new MemoryStream(); byte[] cr = { 155, 156, 135, 170, 210, 232, 108, 198 }; byte[] field = { 105, 25, 131, 203, 171, 234, 78, 8 }; DES alg = DES.Create(); alg.Key = cr; alg.IV = field; alg.Mode = CipherMode.ECB; CryptoStream cs = new CryptoStream(memoryStream, alg.CreateEncryptor(), CryptoStreamMode.Write); cs.Write(field, 0, 8); cs.Close(); byte[] resp_t1 = memoryStream.ToArray(); После строчки cs.Write его длинна 8. После cs.Close уже 16 и в resp_t1 записывается 16. Может есть какие-то другие библиотеки или на другом языке маленькую библиотеку оформить? В .NET я пол дня копался и ничего не нашёл... |
![]() |
![]() |
# 5 |
Junior Member
Регистрация: 11.10.2005
Сообщения: 63
![]() |
Поменяй последние две строчки местами.
. Закрывает поток и освобождает используемые ресурсы. я так понимаю что сбрасывается буфер. Или же смотри http://www.aspnetmania.com/Code/Code/30.html Там точно работает. Примеров много, стоит только поискать. Тем более ты будешь шифровать поток. |
![]() |
![]() |
# 6 | ||
Full Member
Регистрация: 20.01.2004
Адрес: Таллинн
Пол: Male
Сообщения: 623
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
Цитата:
Цитата:
Наверно нужна другая библиотека для шифрования... |
||
![]() |
![]() |
# 7 |
Junior Member
Регистрация: 11.10.2005
Сообщения: 63
![]() |
UnicodeEncoding unicodeEncoding = new UnicodeEncoding();
byte[] inputByteArray = unicodeEncoding.GetBytes(inputString); byte[] outputByteArray; MemoryStream outputMemoryStream = new MemoryStream(); DES des = new DESCryptoServiceProvider(); CryptoStream cryptoStream = new CryptoStream(outputMemoryStream, des.CreateEncryptor(Cryptographer.Key, Cryptographer.IV), CryptoStreamMode.Write); cryptoStream.Write(inputByteArray, 0, inputByteArray.Length); outputMemoryStream.Position = 0; outputByteArray=outputMemoryStream.ToArray(); cryptoStream.Close(); outputMemoryStream.Close(); return unicodeEncoding.GetString(outputByteArray); Всё отрабатывает и даже потом расшифровывается: UnicodeEncoding unicodeEncoding = new UnicodeEncoding(); byte[] inputByteArray = unicodeEncoding.GetBytes(inputString); byte[] outputByteArray; MemoryStream outputMemoryStream = new MemoryStream(); DES des = new DESCryptoServiceProvider(); CryptoStream cryptoStream = new CryptoStream(outputMemoryStream, des.CreateDecryptor(Cryptographer.Key, Cryptographer.IV), CryptoStreamMode.Write); cryptoStream.Write(inputByteArray, 0, inputByteArray.Length); outputMemoryStream.Position = 0; outputByteArray=outputMemoryStream.ToArray(); outputMemoryStream.Close(); return unicodeEncoding.GetString(outputByteArray); Вначале естественно была инициализация: static Cryptographer() { //загрузить определённые ключи byte[] cr = { 155, 156, 135, 170, 210, 232, 108, 198 }; byte[] field = { 105, 25, 131, 203, 171, 234, 78, 8 }; DES alg = DES.Create(); alg.Key = cr; alg.IV = field; alg.Mode = CipherMode.ECB; Cryptographer.IV = alg.IV; Cryptographer.Key = alg.Key; /* или же можно сгенерить DES des = new DESCryptoServiceProvider(); des.GenerateIV(); des.GenerateKey(); Cryptographer.IV = des.IV; Cryptographer.Key = des.Key; */ } |
![]() |