加密int
label27:int j這一行不符合Java的語法規范,肯定是編譯通不過。
2. c語言 數據加密
什麼是異或演算法
異或的特點是原始值經過兩次異或某一個數後會變成原來的值,所以有時利用這個特性來進行加密,加密端把數據與一個密鑰進行異或操作,生成密文。接收方收到密文後利用加密方提供的密鑰進行再次異或操作就能得到明文。
常式:
/*以DWORD為單位對文件進行加密,將每個DWORD與0xfcba0000(密鑰)做異或,寫入另一個文件*/
#include<stdio.h>
#include<stdlib.h>
#defineDWORDunsignedlong
#defineBYTEunsignedchar
#definefalse0
#definetrue1
intmain(intargc,char*argv[])
{
FILE*hSource;
FILE*hDestination;
DWORDdwKey=0xfcba0000;
char*pbBuffer;
DWORDdwBufferLen=sizeof(DWORD);
DWORDdwCount;
DWORDdwData;
if(argv[1]==0||argv[2]==0)
{
printf("missingargument!
");
returnfalse;
}
char*szSource=argv[1];
char*szDestination=argv[2];
hSource=fopen(szSource,"rb");//打開源文件.
hDestination=fopen(szDestination,"wb");//打開目標文件
if(hSource==NULL){printf("openSourceFileerror!");returnfalse;}
if(hDestination==NULL){printf("openDestinationFileerror!");returnfalse;}
//分配緩沖區
pbBuffer=(char*)malloc(dwBufferLen);
do{
//從源文件中讀出dwBlockLen個位元組
dwCount=fread(pbBuffer,1,dwBufferLen,hSource);
//加密數據
dwData=*(DWORD*)pbBuffer;//char*TOdword
dwData^=dwKey;//xoroperation
pbBuffer=(char*)&dwData;
//將加密過的數據寫入目標文件
fwrite(pbBuffer,1,dwCount,hDestination);
}while(!feof(hSource));
//關閉文件、釋放內存
fclose(hSource);
fclose(hDestination);
printf("%sisencryptedto%s
",szSource,szDestination);
return0;
}
3. C語言數字加密
#include
void
main()
{
int
a[5];
/*
存儲各位上的數字
*/
int
num,
temp,
encripy;
/*
num是要輸入的數,temp是交換時用來存儲臨時值,encripy是加密後的數據
*/
int
i;
do
{
printf("please
input
the
number:");
scanf("%d",&num);
if(!(num/10000
!=0
&&
num/100000==0))
printf("data
error!\n");
}while(!(num/10000
!=0
&&
num/100000==0));
a[0]
=
num/10000%10;
/*
求各位上的數字
*/
a[1]
=
num/1000%10;
a[2]
=
num/100%10;
/*
百位上的數字
*/
a[3]
=
num/10%10;
/*
十位上的數字
*/
a[4]
=
num%10;
/*
個位上的數字
*/
for(i
=
0;
i
<
5;
++i)
/*
開始加密
*/
a[i]
=
(a[i]
+
8)%10;
temp
=
a[0];
/*
交換位置開始
*/
a[0]
=
a[3];
a[3]
=
temp;
temp
=
a[1];
a[1]
=
a[2];
a[2]
=
temp;
/*
交換位置結束同時加密結束
*/
encripy
=
a[0]*10000
+
a[1]*1000
+
a[2]*100
+
a[3]*10
+
a[4];
/*
加密後的數據
*/
printf("\nthe
scourse
number:
%d\n",
num);
/*
輸出原數據
*/
printf("\nencripy
the
number:
%d\n\n",
encripy);
/*
輸出加密後的數據
*/
}
在vc6.0成功運行,希望對你有幫助!
4. C語言 字元串加密
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
intmain(void)
{
charc[100];
intk;
intlen,i,temp;
scanf("%s",c);
scanf("%d",&k);
len=(int)strlen(c);
k=k%26;
for(i=0;i<len;i++)
{
if(c[i]>='a'&&c[i]<='z')
{
if(c[i]+k>'z')
{
temp='z'-c[i];
temp=k-temp-1;
c[i]='a'+temp;
}
else
{
c[i]+=k;
}
}
elseif(c[i]>='A'&&c[i]<='Z')
{
if(c[i]+k>'Z')
{
temp='Z'-c[i];
temp=k-temp-1;
c[i]='A'+temp;
}
else
{
c[i]+=k;
}
}
else
{
/*donothing*/
}
}
printf("%s ",c);
return0;
}
5. java如何加密int類型數據
String加密 實際上也是對String的 byte[] 加密。
通常一種加密演算法,都針對的是位元組數組,而非String 或者int。
因為所有上述這些類型都可以用 byte[]表示,只要開發一次就可以針對所有類型加密了
把int轉化成 byte[]加密就可以了
byte[4] intbytes = new byte[4]; 然後用位移運算,得到int的每一個byte
int value = 1000 ;
intbytes[0] = (byte)(value & 0x000000FF)
intbytes[1] = (byte)((value & 0x0000FF00) >> 8)
intbytes[2] = (byte)((value & 0x00FF0000) >> 16)
......
String換算成byte就更容易了 String.getBytes("utf-8") ; 參數是字元集名字 可以不用指定,但是你就不確定它到底用的哪種字元集。
6. 如何數值加密
#include <iostream>
using namespace std;
int main()
{
char number[10] = { '\0' };
cin >> number;
while (1)
{
int length = (int)strlen(number);
int proct = 1, sum = 0;
for (int i = 0; i < length; i++)
{
proct *= (number[i] - '0');
sum += (number[i] - '0');
}
int bits_proct = 1, bits_sum = 1;
int tmp_proct = proct, tmp_sum = sum;
while (tmp_proct >= 10)
{
tmp_proct = tmp_proct / 10;
bits_proct++;
}
while (tmp_sum >= 10)
{
tmp_sum = tmp_sum / 10;
bits_sum++;
}
if (proct)
{
for (int i = 0; i < bits_proct; i++)
{
number[i] = proct / (int)pow(10, bits_proct - i - 1) % 10 + '0';
}
for (int j = bits_proct; j < 10; j++)
{
number[j] = '\0';
}
}
else
{
for (int i = 0; i < bits_sum; i++)
{
number[i] = sum / (int)pow(10, bits_sum - i - 1) % 10 + '0';
}
for (int j = bits_sum; j < 10; j++)
{
number[j] = '\0';
}
}
if (strlen(number) == 1)
{
break;
}
}
cout << number << endl;
system("pause");
return 0;
}