c語言des
❶ des解密演算法,利用c語言解密JAVA語言加密的密碼。。密鑰為12345678,加密後的密文為:26d086be3a3a62fc
// C 語言 DES用的是 ECB模式, 沒有填充
// 因此Java端要對應, 你的明文是 liubiao 嗎?
// 另外 DES已經不安全了, 如果可以改為 3DES或者 AES吧。
public class LearnDes {
public static void main(String[] args) {
try {
System.out.println(encrypt("liubiao", "12345678"));
System.out.println(decrypt("26d086be3a3a62fc", "12345678"));
} catch (Exception e) {
e.printStackTrace();
}
}
public static String encrypt(String message, String key) throws Exception {
//Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
Cipher cipher = Cipher.getInstance("DES/ECB/NOPADDING");
DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8"));
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
IvParameterSpec iv = new IvParameterSpec(key.getBytes("UTF-8"));
//cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
cipher.init(Cipher.ENCRYPT_MODE, secretKey );
return toHexString(cipher.doFinal(message.getBytes("UTF-8")));
}
public static String decrypt(String message, String key) throws Exception {
byte[] bytesrc = convertHexString(message);
//Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
Cipher cipher = Cipher.getInstance("DES/ECB/NOPADDING");
DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8"));
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
IvParameterSpec iv = new IvParameterSpec(key.getBytes("UTF-8"));
//cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
cipher.init(Cipher.DECRYPT_MODE, secretKey );
byte[] retByte = cipher.doFinal(bytesrc);
return new String(retByte);
}
public static byte[] convertHexString(String ss) {
byte digest[] = new byte[ss.length() / 2];
for (int i = 0; i < digest.length; i++) {
String byteString = ss.substring(2 * i, 2 * i + 2);
int byteValue = Integer.parseInt(byteString, 16);
digest[i] = (byte) byteValue;
}
return digest;
}
public static String toHexString(byte b[]) {
StringBuffer hexString = new StringBuffer();
for (int i = 0; i < b.length; i++) {
String plainText = Integer.toHexString(0xff & b[i]);
if (plainText.length() < 2)
plainText = "0" + plainText;
hexString.append(plainText);
}
return hexString.toString();
}
}
❷ 求C語言迷宮程序的解釋說明!!!
我看了一下,演算法應該是一樣的,下面是我以前用C++寫的
相信你能看得懂
/////////////////////////
/////////迷宮求解////////
//////作者:hacker/////
/////時間:11.10.2006/////
/////////////////////////
/*class:
Matrix:矩陣類
offsets:搜索偏移
enum directions:四個方向
struct item:搜索節點
Migong:迷宮類
1.創建一個Migong對象
2.使用用Create方法輸入數據
3.使用Solve方法進行求解
4.ShowSolve方法顯示解
5.可以重復使用Create方法
6.入口只能在左上角
7.默認出口在右下角
ShowAllPath:窮舉所有的路徑
備注:
由於演算法原因,這里的所有路徑應該是指
介於:
a.如果兩條路存在某個點不同那麼就是不同的路
b.如果在一條路中去掉一個或者一個以上的圈,那麼他們是同一條路
之間意義上的路
*/
#include <iostream>
#include <stack>
#include <vector>
using namespace std;
#ifndef MIGONG_H
#define MIGONG_H
///////////////////
///////矩陣類//////
///////////////////
class Matrix{
int* m;
int row, col;
bool iscreate;
public:
Matrix(){m=0;iscreate=false;};
~Matrix() {Release();};
bool Create(int, int);
int& operator () (int, int);
int GetRow(){return row;};
int GetCol(){return col;};
void Release();
void Show(char, char );
};
bool Matrix::Create(int r, int c)
{
if( r<=0 || c<=0) return false;
Release();
row = r;
col = c;
m = new int[row*col];
for (int i=0;i<row*col;i++)
{
*(m+i) = 0;
}
iscreate = true;
return true;
}
int& Matrix::operator ()(int r, int c)
{
return *(m+r*col+c);
}
void Matrix::Release()
{
if (iscreate)
{
row = col = 0;
if (m) delete[] m;
m = 0;
}
iscreate = false;
}
void Matrix::Show(char blk='#', char nblk=' ')
{
int i, j;
for (i=0;i<row;i++)
{
for (j=0;j<col;j++)
{
if (*(m+i*col+j) == 0)
cout<<nblk;
else
cout<<blk;
}
cout<<endl;
}
}
/////////////////////////////
////迷宮相關數據結構的定義///
/////////////////////////////
struct offsets{
int a, b;
};
enum directions{
_S = 0,
_E,
_N,
_W
};
struct item{
int row, col, dir;
};
class Migong{
static offsets move[4];
Matrix maze;
Matrix mark;
int row;
int col;
int desr;
int desc;
stack<item> stk;
bool iscreate;
int pathlength;
bool GetPath();
bool IsInPath(int, int);
public:
Migong(){issolved=false;result=0;pathlength=row=col=0;iscreate=false;};
~Migong(){Release();};
bool Create(int* , int , int , int , int );
void Solve();
void Release();
void OutputMaze();
void ShowSolve(char, char );
public:
bool issolved;
item* result;
};
offsets Migong::move[4]={ {1, 0}, {0, 1},
{-1, 0}, {0, -1}};
////////////////////////////
//迷宮數據應該是不含邊框的//
////////////////////////////
bool Migong::Create(int* m, int r, int c, int desrow=-1, int descol=-1)
{
if (r<=0 || c<=0) return false;
Release();
if (desrow==-1 || descol==-1)
{
desr = r;
desc = c;
}
else
{
desr = desrow;
desc = descol;
}
row = r;
col = c;
maze.Create(r+2, c+2);
mark.Create(r+2, c+2);
int i, j;
for (i=0;i<r+2;i++)
{
for (j=0;j<c+2;j++)
{
if (j==0 || j==c+1 || i==0 || i==r+1)
{
mark(i, j) = maze(i, j) = 1;
}else
{
mark(i, j) = 0;
maze(i, j) = m[((i-1)*col+j-1)];
}
}
}
return iscreate = true;
}
bool Migong::GetPath()
{
mark(1,1) = 1;
item temp;
temp.col = 1;
temp.row = 1;
temp.dir = _S;
stk.push(temp);
while (!stk.empty())
{
temp = stk.top();
stk.pop();
int i = temp.row;
int j = temp.col;
int d = temp.dir;
while (d<4)
{//根據當前點的狀態確定下一個搜索點
int g = i + move[d].a;
int h = j + move[d].b;
if (g==desr && h==desc)
{
return true;
}
//如果這個點不是障礙點且沒有被搜索過那麼可以對這個點進行搜索
if (maze(g, h)==0 && mark(g, h)==0)
{
mark(g, h) = 1;
temp.row = g;
temp.col = h;
temp.dir = d+1;
stk.push(temp);
i = g;
j = h;
d = _S;//對一下個點進行搜索
}
else d++;
}
}
return false;
}
void Migong::Solve()
{
issolved = GetPath();
if (issolved)
{
pathlength = stk.size();
result = new item[pathlength];
for (int i=0;i<pathlength;i++)
{
*(result+i) = stk.top();
stk.pop();
// cout<<"("<<(*(result+i)).row<<","<<(*(result+i)).col<<")"<<endl;
}
}
while (!stk.empty())
stk.pop();
}
void Migong::Release()
{
if (iscreate)
{
maze.Release();
mark.Release();
row=col=0;
if (result)
delete [] result;
result = 0;
while (!stk.empty())
stk.pop();
}
iscreate = false;
issolved = false;
pathlength = 0;
}
void Migong::OutputMaze()
{
if (!iscreate) return;
maze.Show();
}
bool Migong::IsInPath(int r, int c)
{
if (!iscreate || !issolved)
return false;
item temp;
for (int i=0;i<pathlength;i++)
{
temp = *(result+i);
if ((temp.row==r) && (temp.col==c))
return true;
}
return false;
}
void Migong::ShowSolve(char blk='#',char s='o')
{
if (!iscreate) return;
if (!issolved)
{
cout<<"無解"<<endl;
}
else
{
int i, j;
for (i=0;i<row+2;i++)
{
for (j=0;j<col+2;j++)
{
if ((i==1 && j==1) || (i==desr && j==desc))
{
cout<<s;
}
else if (maze(i, j) == 1)
{
cout<<blk;
}else
{
if (IsInPath(i, j))
cout<<s;
else
cout<<' ';
}
}
cout<<endl;
}
}
}
//////////////////////
//////窮舉所有路徑////
//////////////////////
offsets move[4]={ {1, 0}, {0, 1},
{-1, 0}, {0, -1}};
struct node
{
int row,col;
};
vector<node> path;
int count;
bool IsReachable( Matrix& maze, Matrix& mark, node beg, node des)
{
if (beg.row==des.row&&beg.col==des.col)
{//如果達到的話那麼顯示路徑
count++;
cout<<"第"<<count<<"條路徑:"<<endl;
for (int i=0;i<path.size();i++)
cout<<"("<<path[i].row<<","<<path[i].col<<")";
cout<<"("<<des.row<<","<<des.col<<")";
cout<<endl;
return false;
}
if (maze(beg.row, beg.col)==1 || mark(beg.row, beg.col)==1)
{
return false;
}
path.push_back(beg);
mark(beg.row, beg.col) = 1;
node nextnode;
for (int i=_S;i<_W+1;i++)
{
nextnode.row = beg.row + move[i].a;
nextnode.col = beg.col + move[i].b;
IsReachable(maze, mark, nextnode, des);
}
path.resize(path.size()-1);
mark(beg.row, beg.col) = 0;
return false;//如果不是窮舉的話應該根據for循環的結果重新設置返回值
}
/*
參數maze,mark為迷宮長寬均加二的矩陣
desr,desc為出口點
*/
void FindAllPath( Matrix& maze, Matrix& mark, int desr, int desc)
{
node first, last;
first.row = 1;
first.col = 1;
last.row = desr;
last.col = desc;
IsReachable(maze, mark, first, last);
path.clear();
}
/*
m迷宮矩陣數據
r,c行和列的大小
desr,desc目標位置
*/
void ShowAllPath(int* m, int r, int c, int desr=-1, int desc=-1)
{
Matrix maze, mark;
maze.Create(r+2, c+2);
mark.Create(r+2, c+2);
if (desr==-1 || desc==-1)
{
desr = r;
desc = c;
}
int i, j;
for (i=0;i<r+2;i++)
{
for (j=0;j<c+2;j++)
{
if (j==0 || j==c+1 || i==0 || i==r+1)
{
mark(i, j) = maze(i, j) = 1;
}else{
mark(i, j) = 0;
maze(i, j) = m[((i-1)*c+j-1)];
}
}
}
count = 0;
FindAllPath(maze, mark, desr, desc);
maze.Release();
mark.Release();
}
#endif
❸ C語言中handle類型是什麼意思
1、handle是句柄類型,來源於Handle-C,Handle-C是硬體描述語言。windows在創建一個系統對象的同時,把一個句柄賦值給這個實體 ,可以用這個句柄來識別或者修改這個對象, 這就是handle句柄的作用。
2、句柄(HANDLE)是資源的標識。操作系統要管理和操作這些資源,都是通過句柄來找到對應的資源。按資源的類型,又可將句柄細分成圖標句柄(HICON),游標句柄(HCURSOR),窗口句柄(HWND),應用程序實例句柄(HINSTANCE)等等各種類型的句柄。 所以HWND是一種HANDLE
❹ 求一個用c語言寫的DES加密演算法~~
using system;
using system.security.cryptography;
using system.io;
using system.text;
public class encryptstringdes {
public static void main(string);
return;
}
// 使用utf8函數加密輸入參數
utf8encoding utf8encoding = new utf8encoding();
byte.tochararray());
// 方式一:調用默認的des實現方法des_csp.
des des = des.create();
// 方式二:直接使用des_csp()實現des的實體
//des_csp des = new des_csp();
// 初始化des加密的密鑰和一個隨機的、8比特的初始化向量(iv)
byte iv = {0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef};
des.key = key;
des.iv = iv;
// 建立加密流
symmetricstreamencryptor sse = des.createencryptor();
// 使用cryptomemorystream方法獲取加密過程的輸出
cryptomemorystream cms = new cryptomemorystream();
// 將symmetricstreamencryptor流中的加密數據輸出到cryptomemorystream中
sse.setsink(cms);
// 加密完畢,將結果輸出到控制台
sse.write(inputbytearray);
sse.closestream();
// 獲取加密數據
byte);
}
console.writeline();
//上面演示了如何進行加密,下面演示如何進行解密
symmetricstreamdecryptor ssd = des.createdecryptor();
cms = new cryptomemorystream();
ssd.setsink(cms);
ssd.write(encrypteddata);
ssd.closestream();
byte decryptedchararray = utf8encoding.getchars(decrypteddata);
console.writeline("解密後數據:");
console.write(decryptedchararray);
console.writeline();
}
}
編譯:
d:\csharp>csc des_demo.cs
microsoft (r) c# compiler version 7.00.8905
right (c) microsoft corp 2000. all rights reserved.
運行實例:
d:\csharp>des_demo.exe 使用c#編寫des加密程序的framework
加密結果:
3d 22 64 c6 57 d1 c4 c3 cf 77 ce 2f d0 e1 78 2a 4d ed 7a a8 83 f9 0e 14 e1 ba 38
7b 06 41 8d b5 e9 3f 00 0d c3 28 d1 f9 6d 17 4b 6e a7 41 68 40
❺ c語言,怎麼知道某一個數據在txt的第幾個位元組
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
unsigned long file_size;
char *text_buf;
FILE *fp;
fp = fopen("aa.txt","r");//這里把aa.txt改成你要打開的文件名字
fseek(fp,0,SEEK_END);
file_size = ftell(fp);
rewind(fp);
text_buf = (char *)malloc(file_size+1);
memset(text_buf,'
