加密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;
}