Polzovatel | Дата: Понедельник, 2012-09-24, 3:33 PM | Сообщение # 61 |
Лейтенант
Группа: Пользователь
Сообщений: 50
Статус: Offline
| matlab Code clear all; clc; load digits % #1 % N=300; % x=digits.four1; % Px=stpower(x,N); % plot(Px/max(Px),'r'); hold on; % plot(x/max(x));
% #2 % N=500;%10000gz*0.001ms*window % x = digits.four1; % lenx = 1:length(x); % Px=stpower(x,N); % plot(lenx/100,Px/max(Px),'r'); hold on; % plot(lenx/100,x/max(x));
% #3 help hamming N=100; x=1:N; y=hamming(N); plot(x,y); Добавлено (2012-09-24, 3:33 PM) --------------------------------------------- Passwords
Code #include <stdio.h> #include <pthread.h> #include <stdlib.h> #include <cstdlib> #include <time.h> #include <openssl/md5.h> #include <iostream> #include <vector> #include <string> #define N 10 using namespace std;
double myrand(int n) { return (double)(((double)rand()/(double)RAND_MAX)*n); }
float x2(float x) { return x*x; }
void* pass_on_threads_thread(void* args) { unsigned char md5[MD5_DIGEST_LENGTH]; unsigned char pass[5] = {'0','1','2','3','4'}; MD5(pass,5,md5); for(int i=0; i<MD5_DIGEST_LENGTH; i++) printf("%02x",md5[i]); // int counter=0; // pthread_exit((void*) counter); }
vector<string> buff_of_pass; char x[36]; void init_x() { x[0]='a'; for(int i=1; i<27; i++) x[i]=x[i-1]+1; x[26] = '0'; for(int i=27; i<37; i++) x[i]=x[i-1]+1; x[36] = '\0'; //cout<<'\n'<<x<<'\n'; }
void func(int count) { if (count == 0) return;
for(int i=0; i<buff_of_pass.size(); i++) { buff_of_pass[i].push_back(x[0]); for(int j=0; j<35; j++) { buff_of_pass.push_back(buff_of_pass[i]); buff_of_pass[i].operator[](count-2) = x[j]; } } func(count-1); }
int main() { init_x(); printf("Hellow World\n"); /* unsigned char md5[MD5_DIGEST_LENGTH]; unsigned char pass[5] = {'0','1','2','3','4'}; cout<<pass<<"\n"; MD5(pass,5,md5); for(int i=0; i<MD5_DIGEST_LENGTH; i++) printf("%02x",md5[i]); pthread_t thread; pthread_create(&thread,NULL,pass_on_threads_thread,NULL); */ for(int i=0; i<36; i++) { string s; s.push_back(x[i]); buff_of_pass.push_back(s); } func(2); for(int i=0; i<10; i++) cout<<buff_of_pass[i]<<endl; //MD5(pass,1,md5); // printf("%02x\n",md5); //cout<<md5; return 0; }
////////////////////////////////////////////////////// ////////////////////////////////////////////////////// ////////////////////////////////////////////////////// ////////////////INTEGRAL ON THREADS ////////////////////////////////////////////////////// ////////////////////////////////////////////////////// struct interval { int a,b,h; };
void* integral_on_threads_thread(void* args) { interval* input = (interval*) args; int x = input->a + myrand(input->b-input->a); int y = myrand(input->h); int counter=0; if(y<x2(x)) { counter++; } pthread_exit((void*) counter); }
void integral_on_threads_main() { printf("Input a,b\n"); int a,b,nt; scanf("%i%i",&a,&b); pthread_t tid[N]; interval thread_input; thread_input.a = a; thread_input.b = b; thread_input.h = x2(b); srand(time(NULL)); //printf("%f\n",myrand(4)); for(int i=0; i<N; i++) pthread_create(&tid[i],NULL,integral_on_threads_thread,&thread_input); int countout, totalcount=0; for(int i=0; i<N; i++) { pthread_join(tid[i], (void**)countout); printf("%d\n",countout); totalcount += countout; } }
|
|
| |
Polzovatel | Дата: Воскресенье, 2012-10-07, 7:05 PM | Сообщение # 63 |
Лейтенант
Группа: Пользователь
Сообщений: 50
Статус: Offline
| RaysForm VS2012
Добавлено (2012-10-07, 6:33 PM) --------------------------------------------- Integral on threads (рандом тупит) Code // Semenov.cpp : Defines the entry point for the console application. //
#include "stdafx.h" #include <iostream>
#include <stdlib.h> #include <stdio.h> #include <pthread.h> #include <stdlib.h> #include <cstdlib> #include <time.h> #include <md5.h> #include <iostream> #include <vector> #include <string> #define N 10 using namespace std;
void integral_on_threads_main(); double myrand(int n); double myrand(int n, void* r);
void* pass_on_threads_thread(void* args) { //unsigned char md5[MD5_DIGEST_LENGTH]; char pass[5] = {'0','1','2','3','4'}; //MD5(pass,5,md5); // for(int i=0; i<MD5_DIGEST_LENGTH; i++) //printf("%02x",md5[i]); // cout<<md5(string(pass)); // int counter=0; // pthread_exit((void*) counter); return (void*)1; }
vector<string> buff_of_pass; char x[36]; void init_x() { x[0]='a'; for(int i=1; i<27; i++) x[i]=x[i-1]+1; x[26] = '0'; for(int i=27; i<37; i++) x[i]=x[i-1]+1; x[36] = '\0'; //cout<<'\n'<<x<<'\n'; }
void func(int count) { if (count == 0) return;
for(int i=0; i<buff_of_pass.size(); i++) { buff_of_pass[i].push_back(x[0]); for(int j=0; j<35; j++) { buff_of_pass.push_back(buff_of_pass[i]); buff_of_pass[i].operator[](count-2) = x[j]; } } func(count-1); }
int main() { //init_x(); printf("Hellow World\n"); integral_on_threads_main(); int i; cin >> i; /* unsigned char md5[MD5_DIGEST_LENGTH]; unsigned char pass[5] = {'0','1','2','3','4'}; cout<<pass<<"\n"; MD5(pass,5,md5); for(int i=0; i<MD5_DIGEST_LENGTH; i++) printf("%02x",md5[i]); pthread_t thread; pthread_create(&thread,NULL,pass_on_threads_thread,NULL); */ /*for(int i=0; i<36; i++) { string s; s.push_back(x[i]); buff_of_pass.push_back(s); }*/ /* func(2); for(int i=0; i<10; i++) cout<<buff_of_pass[i]<<endl;*/ //MD5(pass,1,md5); // printf("%02x\n",md5); //cout<<md5; return 0; }
////////////////////////////////////////////////////// ////////////////////////////////////////////////////// ////////////////////////////////////////////////////// ////////////////INTEGRAL ON THREADS ////////////////////////////////////////////////////// ////////////////////////////////////////////////////// struct interval { int a,b,h; };
double x2(double x) { return pow(x,2); }
void* integral_on_threads_thread(void* args) { interval* input = (interval*) args; int x; int y; int counter=0, k=0; while(k<1000) { x = input->a + myrand(input->b-input->a); y = myrand(input->h); //,pthread_self().p if(y<x2(x)) { counter++; } k++; } //counter = y; pthread_exit((void*) counter); return (void*)counter; }
void integral_on_threads_main() { printf("Input a,b\n"); int a,b,nt; scanf_s("%i%i",&a,&b); pthread_t tid[N]; interval thread_input; thread_input.a = a; thread_input.b = b; thread_input.h = x2(b); srand(time(NULL)); //printf("%f\n",myrand(4)); for(int i=0; i<N; i++) pthread_create(&tid[i],NULL,integral_on_threads_thread,&thread_input); int countout=0, totalcount=0; for(int i=0; i<N; i++) { pthread_join(tid[i], (void **)&countout); printf("%d\n",countout); totalcount += countout; } cout<<(b-a)*x2(b) *totalcount/(N*1000); }
double myrand(int n) { //srand(clock()); return (double)(((double)rand()/(double)RAND_MAX)*n); } double myrand(int n, void* r) { srand((int)r); return (double)(((double)rand()/(double)RAND_MAX)*n); }
Добавлено (2012-10-07, 7:05 PM) --------------------------------------------- srand(int(time(NULL)) * (int)pthread_self().p);
Сообщение отредактировал Polzovatel - Воскресенье, 2012-10-07, 6:35 PM |
|
| |
Polzovatel | Дата: Суббота, 2013-10-26, 3:21 PM | Сообщение # 67 |
Лейтенант
Группа: Пользователь
Сообщений: 50
Статус: Offline
| Shifrovanie on python
Код ################################## #Class Switch for Python ################################## class switch(object): value = None def __new__(class_, value): class_.value = value return True
def case(*args): return any((arg == switch.value for arg in args)) ################################## ##################################
def AES(chiper, message, key, mode): translated = '' message = message.upper() while switch(chiper): if case('caesar'): LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' # run the encryption/decryption code on each symbol in the message string for symbol in message: if symbol in LETTERS: # get the encrypted (or decrypted) number for this symbol num = LETTERS.find(symbol) # get the number of the symbol if mode == 'encrypt': num = num + key elif mode == 'decrypt': num = num - key
# handle the wrap-around if num is larger than the length of # LETTERS or less than 0 #f num >= len(LETTERS): # num = num %len(LETTERS) # elif num < 0: num = num %len(LETTERS)
# add encrypted/decrypted number's symbol at the end of translated translated = translated + LETTERS
[num]else: # just add the symbol without encrypting/decrypting translated = translated + symbol return translated break print ("Does not know algorythm") break
# Caesar Cipher def main(): # the string to be encrypted/decrypted #message = 'This is my secret message.' message = 'You are missing some details2323' crypted = 'LBH NER ZVFFVAT FBZR QRGNVYF2323' key = 13 translated = AES('caesar', message, key, 'encrypt') translated = AES('caesar', crypted, key, 'decrypt')
print(message) print(translated) # If transpositionTest.py is run (instead of imported as a module) call # the main() function. if __name__ == '__main__': main()
Добавлено (2013-10-26, 2:52 PM) --------------------------------------------- Simplified DES 10 вариант 3 раунда шифр 0000001010
Код # # Simplified DES # # Author: Dan Shumow, 2009 #
# # The Expansions/Permutations are stored as lists of bit positions #
xrange = range P10_data = [3, 5, 2, 7, 4, 10, 1, 9, 8, 6];
P8_data = [6, 3, 7, 4, 8, 5, 10, 9];
LS1_data = [2, 3, 4, 5, 1];
LS2_data = [3, 4, 5, 1, 2];
LS3_data = [4, 5, 1, 2, 3];
IP_data = [2, 6, 3, 1, 4, 8, 5, 7];
IPinv_data = [4, 1, 3, 5, 7, 2, 8, 6];
EP_data = [4, 1, 2, 3, 2, 3, 4, 1];
P4_data = [2, 4, 3, 1];
SW_data = [5, 6, 7, 8, 1, 2, 3, 4];
# # SDES lookup tables #
S0_data = [[1, 0, 3, 2], [3, 2, 1, 0], [0, 2, 1, 3], [3, 1, 3, 2]];
S1_data = [[0, 1, 2, 3], [2, 0, 1, 3], [3, 0, 1, 0], [2, 1, 0, 3]];
def ApplyPermutation(X, permutation): #r""" #This function takes a permutation list (list of bit positions.) #And outputs a bit list with the bits taken from X. #""" # permute the list X l = len(permutation); return [X[permutation[j]-1] for j in xrange(l)];
def ApplySBox(X, SBox): #r""" #This function Applies the SDES Sbox (by table look up #""" r = 2*X[0] + X[3]; c = 2*X[1] + X[2]; o = SBox[r][c]; #return [o & 2, o & 1]; o=[int(x) for x in bin(o)[2:]] l=2-len(o) i=0 o.reverse() while i<l: o.append(0) i=i+1 o.reverse() return o
# # Each of these functions uses ApplyPermutation # and a permutation list to perform an SDES # Expansion/Permutation #
def P10(X): return ApplyPermutation(X, P10_data);
def P8(X): return ApplyPermutation(X, P8_data);
def IP(X): return ApplyPermutation(X, IP_data);
def IPinv(X): return ApplyPermutation(X, IPinv_data);
def EP(X): return ApplyPermutation(X, EP_data);
def P4(X): return ApplyPermutation(X, P4_data);
def SW(X): return ApplyPermutation(X, SW_data);
def LS1(X): return ApplyPermutation(X, LS1_data);
def LS2(X): return ApplyPermutation(X, LS2_data);
def LS3(X): return ApplyPermutation(X, LS3_data);
# # These two functions peform the SBox substitutions # def S0(X): return ApplySBox(X, S0_data);
def S1(X): return ApplySBox(X, S1_data);
def concatenate(left, right): #r""" #Joins to bit lists together. #""" ret = [left [j]for j in xrange(len(left))]; ret.extend(right); return ret;
def LeftHalfBits(block): #r""" #Returns the left half bits from block. #""" l = len(block); return [block [j]for j in xrange(int(l/2))];
def RightHalfBits(block): #r""" #Returns the right half bits from block. #""" l = len(block); return [block [j]for j in xrange(int(l/2), int(l))];
def XorBlock(block1, block2): #r""" #Xors two blocks together. #""" l = len(block1); if (l != len(block2)): print("XorBlock arguments must be same length") #raise ValueError, "XorBlock arguments must be same length" return [(block1[j]+block2[j]) % 2 for j in xrange(l)];
def SDESKeySchedule(K): #r""" #Expands an SDES Key (bit list) into the two round keys. #""" temp_K = P10(K);
left_temp_K = LeftHalfBits(temp_K); right_temp_K = RightHalfBits(temp_K);
K1left = LS1(left_temp_K); K1right = LS1(right_temp_K);
K1temp = concatenate(K1left, K1right); K1 = P8(K1temp);
K2left = LS2(K1left); K2right = LS2(K1right);
K2temp = concatenate(K2left, K2right);
K2 = P8(K2temp);
K3left = LS3(K2left); K3right = LS3(K2right);
K3temp = concatenate(K3left, K3right);
K3 = P8(K3temp);
return (K1, K2, K3);
def f_K(block, K): #r""" #Performs the f_K function supplied block and K. #""" left_block = LeftHalfBits(block); right_block = RightHalfBits(block);
temp_block1 = EP(right_block);
temp_block2 = XorBlock(temp_block1, K); left_temp_block2 = LeftHalfBits(temp_block2); right_temp_block2 = RightHalfBits(temp_block2);
S0_out = S0(left_temp_block2); S1_out = S1(right_temp_block2);
temp_block3 = concatenate(S0_out, S1_out);
temp_block4 = P4(temp_block3)
temp_block5 = XorBlock(temp_block4, left_block);
output_block = concatenate(temp_block5, right_block)
return output_block;
def SDESEncrypt(plaintext_block, K): #r""" #Peforms a single SDES plaintext block encryption. #(Given plaintext and key as bit lists.) #"""
(K1, K2, K3) = SDESKeySchedule(K);
temp_block1 = IP(plaintext_block); temp_block2 = f_K(temp_block1, K1);
temp_block3 = SW(temp_block2);
temp_block4 = f_K(temp_block3, K2);
output_block = IPinv(temp_block4);
return output_block;
def SDESDecrypt(ciphertext_block, K): #r""" #Performs a single SDES ciphertext block decryption. #(Given ciphertext and key as bit lists.) #"""
(K1, K2, K3) = SDESKeySchedule(K);
temp_block1 = IP(ciphertext_block); temp_block2 = f_K(temp_block1, K3);
temp_block3 = SW(temp_block2);
temp_block4 = f_K(temp_block3, K2);
temp_block5 = SW(temp_block4);
temp_block6 = f_K(temp_block5, K1);
output_block = IPinv(temp_block6);
return output_block;
def bytes_from_file(filename, chunksize=8192): with open(filename, "rb") as f: while True: chunk = f.read(chunksize) if chunk: for b in chunk: yield b else: break
def do_stuff_with(b): data = bin(b)[2:].zfill(8); #Convert readed byte to bits datal = []; for c in data: datal.append(int(c)); #Convert bits to bit list
key = [0, 0, 0, 0, 0, 0, 1, 0, 1, 0]; #Decryption key out = SDESDecrypt(datal, key);
byte = ""; #make byte from bit list for c in out: byte = byte + str(c); byte = int(byte,2);
return byte
def main():
output = []; for b in bytes_from_file('text10_cipher.txt',1): output.append(do_stuff_with(b));
decrypted = "".join(map(chr, output));
print(decrypted) if __name__=='__main__': main()
|
|
| |
Wins_Vega | Дата: Среда, 2013-11-06, 3:04 PM | Сообщение # 68 |
Админ
Группа: Администраторы
Сообщений: 204
Статус: Offline
| Нечеткий контроллер Обучается на генетическом алгоритме
Реализация SDL2 C++
Release.rar vcredist_x86.rar
Код template< class T, class U > static string GeneticApproximation(T* i_class, U i_method, string baseInput, int populationsize, int devideblocks, double mutationprobability) { IContainer* func = new Container< T, U > ( i_class, i_method);
GCP_Vector<GeneticSpecies*> *generation = new GCP_Vector<GeneticSpecies*>(); GCP_Vector<GeneticSpecies*> *next_generation; for(int i=0; i<populationsize; i++){ GeneticSpecies *specie = new GeneticSpecies(); specie->values = baseInput; generation->push_back(specie); } //Iteration int iK=0,spawn=0; double dBestResult = 500000; do { //Solve problem for (int i = 0; i < generation->size(); i++) { GeneticSpecies *obj = generation->at(i); obj->result = (int)func->Call((void*)obj->values.c_str()); if (obj->result < dBestResult) dBestResult = obj->result; } next_generation = new GCP_Vector<GeneticSpecies*>(); iK = 0; for (int i = 0; i < generation->size(); i++) { if (generation->at(i)->result <= dBestResult) { if(GCP_Math::rnd() < mutationprobability) generation->at(i)->mutation(); int iRandomElement = GCP_Math::rnd()*(generation->size()-1); GeneticSpecies *obj = new GeneticSpecies(); obj->result = generation->at(i)->result; obj->values = generation->at(i)->values; obj->breed(generation->at(iRandomElement),devideblocks); obj->result = (int)func->Call((void*)obj->values.c_str());
if(obj->result < generation->at(i)->result) next_generation->push_back(obj); else { obj->result = generation->at(i)->result; obj->values = generation->at(i)->values; next_generation->push_back(obj); } iK++; } }
if(next_generation->size() > 0) { for(int i=0; i<generation->size(); i++) delete generation->at(i); delete generation; generation = next_generation; } spawn++; } while (iK > 1 && spawn < 100);
int minResult = 500000; int minResultIndex = -1; for(int i=0; i<generation->size(); i++) { if(generation->at(i)->result<minResult){ minResult = generation->at(i)->result; minResultIndex = i; } } int iRandomElement = GCP_Math::rnd()*(generation->size()-1); if (minResultIndex != -1) return generation->at(minResultIndex)->values; return generation->at(iRandomElement)->values; } Код class GeneticSpecies { public: string values; double result; GeneticSpecies &operator=(GeneticSpecies rhs) { result = rhs.result; values = rhs.values; return *this; } void breed(GeneticSpecies *other, int devideblocks) { int len = values.size();
int rand; if(devideblocks<2) rand = devideblocks; rand = 2+ceil(GCP_Math::rnd()*(devideblocks-2)); int blocksize = len / rand;
//Какие блоки будем менять GCP_Vector<int> oursBlocks; GCP_Vector<int> othersBlocks; for(int i=0; i<rand; i++){ double probability = GCP_Math::rnd(); if(probability>0.1) { int ourBlock = GCP_Math::rnd()*(rand-1); int otherBlock = GCP_Math::rnd()*(rand-1); if(!oursBlocks.has_value(ourBlock) && !othersBlocks.has_value(otherBlock)) { oursBlocks.push_back(ourBlock); othersBlocks.push_back(otherBlock); } } }
for(int i=0; i<oursBlocks.size(); i++) { int ourBlockNum = oursBlocks.at(i); int otherBlockNum = othersBlocks.at(i);
for(int j=0; j<blocksize; j++) { char swap; swap = values[ourBlockNum*blocksize+j]; values[ourBlockNum*blocksize+j] = other->values[otherBlockNum*blocksize+j]; other->values[otherBlockNum*blocksize+j] = swap; } }
} void mutation() { int len = values.size(); int rand = GCP_Math::rnd()*10; for(int i=0; i<rand; i++) { int randV1 = GCP_Math::rnd()*(len-1); int randV2 = GCP_Math::rnd()*(len-1); char swap; swap = values[randV1]; values[randV1] = values[randV2]; values[randV2] = swap; } } };
Wins Vega Wins
|
|
| |