1. 求一個加密解密演算法,密鑰為不限,要求密文為數字和字母組成!
下面的是C#md5加密演算法的實例
using System.Security.Cryptography;
using System.Text;
#region 加密密碼,UserMd5(string str1)
protected string UserMd5(string str1)
{
string cl1 = str1;
string pwd = "";
MD5 md5 = MD5.Create();
// 加密後是一個位元組類型的數組
byte[] s=md5.ComputeHash(Encoding.Unicode.GetBytes(cl1));
// 通過使用循環,將位元組類型的數組轉換為字元串,此字元串是常規字元格式化所得
for(int i = 0 ; i < s.Length; i++)
{
// 將得到的字元串使用十六進制類型格式。格式後的字元是小寫的字母,如果使用大寫(X)則格式後的字元是大寫字元
pwd = pwd + s[i].ToString("x");
}
return pwd;
}
#endregion
asp.net2003 c#的
2. 用c語言設計一個簡單地加密算,解密演算法,並說明其中的原理
恰巧這兩天剛看的一種思路,很簡單的加密解密演算法,我說一下吧。
演算法原理很簡單,假設你的原密碼是A,用A與數B按位異或後得到C,C就是加密後的密碼,用C再與數B按位異或後能得回A。即(A異或B)異或B=A。用C實現很簡單的。
這就相當於,你用原密碼A和特定數字B產生加密密碼C,別人拿到這個加密的密碼C,如果不知道特定的數字B,他是無法解密得到原密碼A的。
對於密碼是數字的情況可以用下面的代碼:
#include <stdio.h>
#define BIRTHDAY 19880314
int main()
{
long a, b;
scanf("%ld", &a);
printf("原密碼:%ld\n", a);
b = BIRTHDAY;
a ^= b;
printf("加密密碼:%ld\n", a);
a ^= b; printf("解密密碼:%ld\n", a);
return 0;
}
如果密碼是字元串的話,最簡單的加密演算法就是對每個字元重新映射,只要加密解密雙方共同遵守同一個映射規則就行啦。
3. java加密解密代碼
package com.cube.limail.util;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;/**
* 加密解密類
*/
public class Eryptogram
{
private static String Algorithm ="DES";
private String key="CB7A92E3D3491964";
//定義 加密演算法,可用 DES,DESede,Blowfish
static boolean debug = false ;
/**
* 構造子註解.
*/
public Eryptogram ()
{
} /**
* 生成密鑰
* @return byte[] 返回生成的密鑰
* @throws exception 扔出異常.
*/
public static byte [] getSecretKey () throws Exception
{
KeyGenerator keygen = KeyGenerator.getInstance (Algorithm );
SecretKey deskey = keygen.generateKey ();
System.out.println ("生成密鑰:"+bytesToHexString (deskey.getEncoded ()));
if (debug ) System.out.println ("生成密鑰:"+bytesToHexString (deskey.getEncoded ()));
return deskey.getEncoded ();
} /**
* 將指定的數據根據提供的密鑰進行加密
* @param input 需要加密的數據
* @param key 密鑰
* @return byte[] 加密後的數據
* @throws Exception
*/
public static byte [] encryptData (byte [] input ,byte [] key ) throws Exception
{
SecretKey deskey = new javax.crypto.spec.SecretKeySpec (key ,Algorithm );
if (debug )
{
System.out.println ("加密前的二進串:"+byte2hex (input ));
System.out.println ("加密前的字元串:"+new String (input ));
} Cipher c1 = Cipher.getInstance (Algorithm );
c1.init (Cipher.ENCRYPT_MODE ,deskey );
byte [] cipherByte =c1.doFinal (input );
if (debug ) System.out.println ("加密後的二進串:"+byte2hex (cipherByte ));
return cipherByte ;
} /**
* 將給定的已加密的數據通過指定的密鑰進行解密
* @param input 待解密的數據
* @param key 密鑰
* @return byte[] 解密後的數據
* @throws Exception
*/
public static byte [] decryptData (byte [] input ,byte [] key ) throws Exception
{
SecretKey deskey = new javax.crypto.spec.SecretKeySpec (key ,Algorithm );
if (debug ) System.out.println ("解密前的信息:"+byte2hex (input ));
Cipher c1 = Cipher.getInstance (Algorithm );
c1.init (Cipher.DECRYPT_MODE ,deskey );
byte [] clearByte =c1.doFinal (input );
if (debug )
{
System.out.println ("解密後的二進串:"+byte2hex (clearByte ));
System.out.println ("解密後的字元串:"+(new String (clearByte )));
} return clearByte ;
} /**
* 位元組碼轉換成16進制字元串
* @param byte[] b 輸入要轉換的位元組碼
* @return String 返回轉換後的16進制字元串
*/
public static String byte2hex (byte [] b )
{
String hs ="";
String stmp ="";
for (int n =0 ;n <b.length ;n ++)
{
stmp =(java.lang.Integer.toHexString (b [n ] & 0XFF ));
if (stmp.length ()==1 ) hs =hs +"0"+stmp ;
else hs =hs +stmp ;
if (n <b.length -1 ) hs =hs +":";
} return hs.toUpperCase ();
}
/**
* 字元串轉成位元組數組.
* @param hex 要轉化的字元串.
* @return byte[] 返回轉化後的字元串.
*/
public static byte[] hexStringToByte(String hex) {
int len = (hex.length() / 2);
byte[] result = new byte[len];
char[] achar = hex.toCharArray();
for (int i = 0; i < len; i++) {
int pos = i * 2;
result[i] = (byte) (toByte(achar[pos]) << 4 | toByte(achar[pos + 1]));
}
return result;
}
private static byte toByte(char c) {
byte b = (byte) "0123456789ABCDEF".indexOf(c);
return b;
}
/**
* 位元組數組轉成字元串.
* @param String 要轉化的字元串.
* @return 返回轉化後的位元組數組.
*/
public static final String bytesToHexString(byte[] bArray) {
StringBuffer sb = new StringBuffer(bArray.length);
String sTemp;
for (int i = 0; i < bArray.length; i++) {
sTemp = Integer.toHexString(0xFF & bArray[i]);
if (sTemp.length() < 2)
sb.append(0);
sb.append(sTemp.toUpperCase());
}
return sb.toString();
}
/**
* 從資料庫中獲取密鑰.
* @param deptid 企業id.
* @return 要返回的位元組數組.
* @throws Exception 可能拋出的異常.
*/
public static byte[] getSecretKey(long deptid) throws Exception {
byte[] key=null;
String value=null;
//CommDao =new CommDao();
// List list=.getRecordList("from Key k where k.deptid="+deptid);
//if(list.size()>0){
//value=((com.csc.sale.bean.Key)list.get(0)).getKey();
value = "CB7A92E3D3491964";
key=hexStringToByte(value);
//}
if (debug)
System.out.println("密鑰:" + value);
return key;
}
public String encryptData2(String data) {
String en = null;
try {
byte[] key=hexStringToByte(this.key);
en = bytesToHexString(encryptData(data.getBytes(),key));
} catch (Exception e) {
e.printStackTrace();
}
return en;
}
public String decryptData2(String data) {
String de = null;
try {
byte[] key=hexStringToByte(this.key);
de = new String(decryptData(hexStringToByte(data),key));
} catch (Exception e) {
e.printStackTrace();
}
return de;
}
} 加密使用: byte[] key=Eryptogram.getSecretKey(deptid); //獲得鑰匙(位元組數組)
byte[] tmp=Eryptogram.encryptData(password.getBytes(), key); //傳入密碼和鑰匙,獲得加密後的位元組數組的密碼
password=Eryptogram.bytesToHexString(tmp); //將位元組數組轉化為字元串,獲得加密後的字元串密碼解密與之差不多
4. c#加密演算法,解密演算法,輸入一串字元,a變成d,b變成e,c--f等等,用vs編寫 在button按鈕裡面寫程序代碼
加密的
stringoStr,nStr;
oStr=textBox1.Text;
varbuff=oStr.ToCharArray();
for(inti=0;i<buff.Length;i++){
buff[i]=(char)((int)buff[i]+3);
if(buff[i]>'z'){
buff[i]=(char)((int)buff[i]-26);
}
}
nStr=newstring(buff);
textBox2.Text=nStr;
解密
stringoStr,nStr;
oStr=textBox3.Text;
varbuff=oStr.ToCharArray();
for(inti=0;i<buff.Length;i++){
buff[i]=(char)((int)buff[i]-3);
if(buff[i]<'a'){
buff[i]=(char)((int)buff[i]+26);
}
}
nStr=newstring(buff);
textBox4.Text=nStr;
5. 給你一段加密明文的代碼,高手來寫個解密的代碼!
幫你看了一下,很明顯這個加密是不可逆的....原因從這段開始:
For
i
=
1
To
Len(strPass)
strTmp
=
Asc(Mid(strPass,
i,
1))
'取每個字元的ascii碼值
theStr
=
theStr
&
Abs(strTmp)
'把每個字元的ascii碼值取絕對值再串在一起組成一個新的字元串
Next
這樣處理之後,只能得到一串無規律的數字組成的字元,從這里開始,你想算回去已經不可能了,所以你也不用求什麼解密演算法了...
而且這個加密演算法沒有唯一性,比如:你可以試試加密:中國,密文為:105441CE26
然後你再試試加密這個:i,\
它的密文也是:105441CE26
具體原因就不仔細說了...還是出在上面的那段代碼里...
6. C++文件的加密和解密代碼
最簡單的兩種加解密方式
單位元組操作 上下文無關
供參考
#include<stdio.h>
voiddo_0(FILE*fin,FILE*fout)
{
#undefKEY
#defineKEY0x37
intc;
while((c=fgetc(fin))!=EOF)
{
c^=KEY;
fputc(c,fout);
}
}
voiddo_1_0(FILE*fin,FILE*fout)
{
#undefKEY
#defineKEY0x06
intc;
while((c=fgetc(fin))!=EOF)
{
c+=KEY;
c%=256;
fputc(c,fout);
}
}
voiddo_1_1(FILE*fin,FILE*fout)
{
#undefKEY
#defineKEY0x06
intc;
while((c=fgetc(fin))!=EOF)
{
c+=256-KEY;
c%=256;
fputc(c,fout);
}
}
intmain()
{
intmode,type;
charin_file[128],out_file[128];
FILE*fp_in,*fp_out;
do
{
printf("selectrunmode:0->encrypt1->decrypt ");
scanf("%d",&mode);
}while(mode!=0&&mode!=1);
do
{
printf("selecttype:0/1 ");
scanf("%d",&type);
}while(type!=0&&type!=1);
getchar();
do
{
printf("inputfilename: ");
gets(in_file);
fp_in=fopen(in_file,"rb");
if(fp_in==NULL)
printf("cannotreadfile%s ",in_file);
}while(fp_in==NULL);
do
{
printf("outputfilename: ");
gets(out_file);
fp_out=fopen(out_file,"wb");
if(fp_out==NULL)
printf("cannotwritefile%s ",out_file);
}while(fp_out==NULL);
if(type==0)
do_0(fp_in,fp_out);
elseif(mode==0)
do_1_0(fp_in,fp_out);
elsedo_1_1(fp_in,fp_out);
fclose(fp_in);
fclose(fp_out);
return0;
}
7. c語言加密解密演算法
這里使用的是按位加密,按ASCII碼進行加密的演算法自己寫個,很容易的。
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
void dofile(char *in_fname,char *pwd,char *out_fname);/*對文件進行加密的具體函數*/
void usage(char *name);
void main(int argc,char *argv[])/*定義main()函數的命令行參數*/
{
char in_fname[30];/*用戶輸入的要加密的文件名*/
char out_fname[30];
char pwd[10];/*用來保存密碼*/
if(argc!=4)
{/*容錯處理*/
usage(argv[0]);
printf("\nIn-fname:\n");
gets(in_fname);/*得到要加密的文件名*/
while(*in_fname==NULL)
{
printf("\nIn-fname:\n");
gets(in_fname);
}
printf("Password 6-8:\n");
gets(pwd);/*得到密碼*/
while(*pwd==NULL || strlen(pwd)>8 || strlen(pwd)<6)
{
printf("Password 6-8:\n");
gets(pwd);
}
printf("Out-file:\n");
gets(out_fname);/*得到加密後你要的文件名*/
while(*in_fname==NULL)
{
printf("Out-file:\n");
gets(out_fname);
}
while(!strcmp(in_fname,out_fname))
{
printf("文件名不能和源文件相同\n");
printf("Out-file:\n");
gets(out_fname);
}
dofile(in_fname,pwd,out_fname);
printf("加密成功,解密請再次運行程序\n");
}
else
{/*如果命令行參數正確,便直接運行程序*/
strcpy(in_fname,argv[1]);
strcpy(pwd,argv[2]);
strcpy(out_fname,argv[3]);
while(*pwd==NULL || strlen(pwd)>8 || strlen(pwd)<6)
{
printf("Password faied!\n");
printf("Password 6-8:\n");
gets(pwd);
}
while(!strcmp(in_fname,out_fname))
{
printf("文件名不能和源文件相同\n");
printf("Out-file:\n");
gets(out_fname);
while(*in_fname==NULL)
{
printf("Out-file:\n");
gets(out_fname);
}
}
dofile(in_fname,pwd,out_fname);
printf("加密成功,解密請再次運行程序\n");
}
}
/*加密子函數開始*/
void dofile(char *in_fname,char *pwd,char *out_file)
{
FILE *fp1,*fp2;
register char ch;
int j=0;
int j0=strlen(pwd);
fp1=fopen(in_fname,"rb");
if(fp1==NULL)
{
printf("cannot open in-file.\n");
exit(1);/*如果不能打開要加密的文件,便退出程序*/
}
fp2=fopen(out_file,"wb");
if(fp2==NULL)
{
printf("cannot open or create out-file.\n");
exit(1);/*如果不能建立加密後的文件,便退出*/
}
/*加密演算法開始*/
while(j0>=0)
{
ch=fgetc(fp1);
while(!feof(fp1))
{
fputc(ch^pwd[j>=j0?j=0:j++],fp2);/*異或後寫入fp2文件*/
ch=fgetc(fp1);
}
j0--;
}
fclose(fp1);/*關閉源文件*/
fclose(fp2);/*關閉目標文件*/
}
void usage(char *name)
{
printf("\t=======================File encryption======================\n");
printf("\tusage: %s In-fname password out_fname\n",name);
printf("\tExample: %s file1.txt 12345678 file2.txt\n",name);
}
8. C++實現RSA加密解密演算法
#include <iostream>
using namespace std;
template <class HugeInt>
HugeInt Power( const HugeInt & x, const HugeInt & n, // 求x^n mod p
const HugeInt & p )
{
if( n == 0 )
return 1;
HugeInt tmp = Power( ( x * x ) % p, n / 2, p );
if( n % 2 != 0 )
tmp = ( tmp * x ) % p;
return tmp;
}
template <class HugeInt>
void fullGcd( const HugeInt & a, const HugeInt & b, //
HugeInt & x, HugeInt & y )
{
HugeInt x1, y1;
if( b == 0 )
{
x = 1;
y = 0;
}
else
{
fullGcd( b, a % b, x1, y1 );
x = y1;
y = x1 - ( a / b ) * y1;
}
}
template <class HugeInt>
HugeInt inverse( const HugeInt & p, const HugeInt & q, // 求d
const HugeInt & e )
{
int fyn = ( 1 - p ) * ( 1 - q );
HugeInt x, y;
fullGcd( fyn, e, x, y );
return x > 0 ? x : x + e;
}
int main( )
{
cout << "Please input the plaintext: " << endl;
int m;
cin >> m;
cout << "Please input p,q and e: " << endl;
int p, q, e;
cin >> p >> q >> e;
int n = p * q;
int d = inverse( p, q, e );
int C = Power( m, e, n );
cout << "The ciphertext is: " << C << endl;
cout << "\n\nPlease input the ciphertext: " << endl;
cin >> C;
cout << "\n\nPlease input p, q and d: " << endl;
cin >> p >> q >> d;
n = p * q;
m = Power( C, d, n );
cout <<"The plaintext is: " << m << endl << endl;
system( "pause" );
return 0;
}
這就是RSA加密解密演算法
9. 求java中3des加密解密示例
在java中調用sun公司提供的3DES加密解密演算法時,需要使用到$JAVA_HOME/jre/lib/目錄下如下的4個jar包:
jce.jar
security/US_export_policy.jar
security/local_policy.jar
ext/sunjce_provider.jar
Java運行時會自動載入這些包,因此對於帶main函數的應用程序不需要設置到CLASSPATH環境變數中。對於WEB應用,不需要把這些包加到WEB-INF/lib目錄下。
以下是java中調用sun公司提供的3DES加密解密演算法的樣本代碼:
加密解密代碼
import java.security.Security;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
/*字元串 DESede(3DES) 加密*/
public class ThreeDes {
/**
* @param args在java中調用sun公司提供的3DES加密解密演算法時,需要使
* 用到$JAVA_HOME/jre/lib/目錄下如下的4個jar包:
*jce.jar
*security/US_export_policy.jar
*security/local_policy.jar
*ext/sunjce_provider.jar
*/
private static final String Algorithm ="DESede"; //定義加密演算法,可用 DES,DESede,Blowfish
//keybyte為加密密鑰,長度為24位元組
//src為被加密的數據緩沖區(源)
public static byte[] encryptMode(byte[] keybyte,byte[] src){
try {
//生成密鑰
SecretKey deskey = new SecretKeySpec(keybyte, Algorithm);
//加密
Cipher c1 = Cipher.getInstance(Algorithm);
c1.init(Cipher.ENCRYPT_MODE, deskey);
return c1.doFinal(src);//在單一方面的加密或解密
} catch (java.security.NoSuchAlgorithmException e1) {
// TODO: handle exception
e1.printStackTrace();
}catch(javax.crypto.NoSuchPaddingException e2){
e2.printStackTrace();
}catch(java.lang.Exception e3){
e3.printStackTrace();
}
return null;
}
//keybyte為加密密鑰,長度為24位元組
//src為加密後的緩沖區
public static byte[] decryptMode(byte[] keybyte,byte[] src){
try {
//生成密鑰
SecretKey deskey = new SecretKeySpec(keybyte, Algorithm);
//解密
Cipher c1 = Cipher.getInstance(Algorithm);
c1.init(Cipher.DECRYPT_MODE, deskey);
return c1.doFinal(src);
} catch (java.security.NoSuchAlgorithmException e1) {
// TODO: handle exception
e1.printStackTrace();
}catch(javax.crypto.NoSuchPaddingException e2){
e2.printStackTrace();
}catch(java.lang.Exception e3){
e3.printStackTrace();
}
return null;
}
//轉換成十六進制字元串
public static String byte2Hex(byte[] b){
String hs="";
String stmp="";
for(int n=0; n<b.length; n++){
stmp = (java.lang.Integer.toHexString(b[n]& 0XFF));
if(stmp.length()==1){
hs = hs + "0" + stmp;
}else{
hs = hs + stmp;
}
if(n<b.length-1)hs=hs+":";
}
return hs.toUpperCase();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
//添加新安全演算法,如果用JCE就要把它添加進去
Security.addProvider(new com.sun.crypto.provider.SunJCE());
final byte[] keyBytes = {0x11, 0x22, 0x4F, 0x58,
(byte)0x88, 0x10, 0x40, 0x38, 0x28, 0x25, 0x79,0x51,
(byte)0xCB,
(byte)0xDD, 0x55, 0x66, 0x77, 0x29, 0x74,
(byte)0x98, 0x30, 0x40, 0x36,
(byte)0xE2
}; //24位元組的密鑰
String szSrc = "This is a 3DES test. 測試";
System.out.println("加密前的字元串:" + szSrc);
byte[] encoded = encryptMode(keyBytes,szSrc.getBytes());
System.out.println("加密後的字元串:" + new String(encoded));
byte[] srcBytes = decryptMode(keyBytes,encoded);
System.out.println("解密後的字元串:" + (new String(srcBytes)));
}
}
10. 多表式密碼對密文加密解密演算法的c語言代碼
#include<stdio.h>
#include<string.h>
void MtoC(char m[50],char k[10],char c[50])
{
int m1[50],k1[10],c1[50],i,j;
for(i=0;i<strlen(k);i++)
k1[i]=k[i]-'a';
for(j=0;j<strlen(m);j++)
{
m1[j]=m[j]-'a';
c1[j]=(m1[j]+k1[j%strlen(k)])%26;
c[j]=c1[j]+'a';
printf("%c------%c\n",m[j],c[j]);
}
}
void CtoM(char c[50],char k[10],char m[50])
{
int m1[50],k1[10],c1[50],i,j;
for(i=0;i<strlen(k);i++)
k1[i]=k[i]-'a';
for(j=0;j<strlen(m);j++)
{
c1[j]=c[j]-'a';
m1[j]=(c1[j]-k1[j%strlen(k)]+26)%26;
m[j]=m1[j]+'a';
printf("%c------%c\n",c[j],m[j]);
}
}
int main(void)
{
int i,j;
char m[50], k[10], c[50],t[50];
printf("輸入明文:");
gets(t);
j=0;
for(i=0;t[i]!='\0';i++){
if(t[i]<='Z'&&t[i]>='A'){
m[j]=t[i]+32;
j++;
}
else if(t[i]<='z'&&t[i]>='a'){
m[j]=t[i];
j++;
}
}
m[j]='\0';
printf("輸入密鑰:");
scanf("%s",k);
printf("明文轉換為密文:n");
MtoC(m,k,c);
printf("密文轉換為明文:n");
CtoM(c,k,m);
}