當前位置:首頁 » 密碼管理 » 加密字元

加密字元

發布時間: 2022-08-12 11:00:26

❶ C語言字元串加密

問題不小,你表面用的是C
但是,好多地方不符合C的語法
,,比如:
for
(int
i=0;
str[i]
!=
'\0';
i++)
還有,就是你好像沒有弄清楚
,你要做什麼似的,有好多無用的東西,
就像你的函數里的,key
,雖然你提到key了,但是你根本沒有使用key,你只是使用45來進行加密,,還有就是一個文件
的大小,是不確定的,你用一個100個字元的字元串來存,有點那個不安全了,,如果稍長一點就會出問題,產生運行時錯誤。其實你這個加密和解密是一個可逆過程,用一個函數,就可以了,具體你想要的也不是太明白,就給你弄了一個簡單一點加密和解密程序
,輸入輸出不是同一個文件
,不知道是不是你想要的。
#include

#include

#include

void
Decrypt()
{
char
fname[FILENAME_MAX];
char
fname2[FILENAME_MAX];
FILE*
fp;
FILE*
fp1;
int
key;
char
c;
printf("輸入要加/解密文件的路徑:\n");
scanf("%s",
fname);
printf("請輸入密鑰:\n");
scanf("%d",&key);
strcpy(fname2,fname);
strcat(fname2,".txt");
if(
(fp
=
fopen(fname,"r+"))
==
NULL)
{
printf("error");
exit(1);
}
if(
(fp1
=
fopen(fname2,"w+"))
==
NULL)
{
printf("error");
exit(1);
}
while(
(c
=
fgetc(fp))
!=
EOF)
{
c
=
c^key;
fputc(c,fp1);
}
fcloseall();
}
int
main()
{
Decrypt();
return
0;
}
如果想看一些好一點的加密演算法
,我這里有一些,聯系我發給你
,,

❷ C語言對字元進行加密

if(e>='A'&&e<='W')//
e+=3;
elseif(e>='X'&&e<='Z')
e-=23;
else
{
...//donotencryptordosomethingelse
}

❸ 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;
}

❹ 加密字元串加密成字母

#include
#include
void replace(char *src, int n)
{
char *s = src;
n = n % 26;
while(*s != 0)
{
*s = *s + n;
if(*s > 'Z') *s -= 26;
s++;
}
}
void disorder(char *s, char *key)
{
int len = 0 , i = 0;
char *tmp;
while(s[len++] != 0);
tmp = (char*)malloc(sizeof(char) * (len));
while(i < len) tmp[i] = s[i++];
for(i = 0; i < len - 1; i++)
{
s[i] = tmp[key[i] - '0' - 1];
}
free(tmp);
}
void main()
{
int n;
char str[100], key[100];
printf("input : ");
//請合法輸入eg. AXZ 2 231
scanf("%s%d%s", str, &n, key);
replace(str, n);
printf("str = %s\n", str);
disorder(str, key);
printf("str = %s\n", str);
}

❺ 如何用java實現字元串簡單加密解密

java加密字元串可以使用des加密演算法,實例如下:
package test;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.security.*;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
/**
* 加密解密
*
* @author shy.qiu
* @since http://blog.csdn.net/qiushyfm
*/
public class CryptTest {
/**
* 進行MD5加密
*
* @param info
* 要加密的信息
* @return String 加密後的字元串
*/
public String encryptToMD5(String info) {
byte[] digesta = null;
try {
// 得到一個md5的消息摘要
MessageDigest alga = MessageDigest.getInstance("MD5");
// 添加要進行計算摘要的信息
alga.update(info.getBytes());
// 得到該摘要
digesta = alga.digest();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
// 將摘要轉為字元串
String rs = byte2hex(digesta);
return rs;
}
/**
* 進行SHA加密
*
* @param info
* 要加密的信息
* @return String 加密後的字元串
*/
public String encryptToSHA(String info) {
byte[] digesta = null;
try {
// 得到一個SHA-1的消息摘要
MessageDigest alga = MessageDigest.getInstance("SHA-1");
// 添加要進行計算摘要的信息
alga.update(info.getBytes());
// 得到該摘要
digesta = alga.digest();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
// 將摘要轉為字元串
String rs = byte2hex(digesta);
return rs;
}
// //////////////////////////////////////////////////////////////////////////
/**
* 創建密匙
*
* @param algorithm
* 加密演算法,可用 DES,DESede,Blowfish
* @return SecretKey 秘密(對稱)密鑰
*/
public SecretKey createSecretKey(String algorithm) {
// 聲明KeyGenerator對象
KeyGenerator keygen;
// 聲明 密鑰對象
SecretKey deskey = null;
try {
// 返回生成指定演算法的秘密密鑰的 KeyGenerator 對象
keygen = KeyGenerator.getInstance(algorithm);
// 生成一個密鑰
deskey = keygen.generateKey();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
// 返回密匙
return deskey;
}
/**
* 根據密匙進行DES加密
*
* @param key
* 密匙
* @param info
* 要加密的信息
* @return String 加密後的信息
*/
public String encryptToDES(SecretKey key, String info) {
// 定義 加密演算法,可用 DES,DESede,Blowfish
String Algorithm = "DES";
// 加密隨機數生成器 (RNG),(可以不寫)
SecureRandom sr = new SecureRandom();
// 定義要生成的密文
byte[] cipherByte = null;
try {
// 得到加密/解密器
Cipher c1 = Cipher.getInstance(Algorithm);
// 用指定的密鑰和模式初始化Cipher對象
// 參數:(ENCRYPT_MODE, DECRYPT_MODE, WRAP_MODE,UNWRAP_MODE)
c1.init(Cipher.ENCRYPT_MODE, key, sr);
// 對要加密的內容進行編碼處理,
cipherByte = c1.doFinal(info.getBytes());
} catch (Exception e) {
e.printStackTrace();
}
// 返回密文的十六進制形式
return byte2hex(cipherByte);
}
/**
* 根據密匙進行DES解密
*
* @param key
* 密匙
* @param sInfo
* 要解密的密文
* @return String 返回解密後信息
*/
public String decryptByDES(SecretKey key, String sInfo) {
// 定義 加密演算法,
String Algorithm = "DES";
// 加密隨機數生成器 (RNG)
SecureRandom sr = new SecureRandom();
byte[] cipherByte = null;
try {
// 得到加密/解密器
Cipher c1 = Cipher.getInstance(Algorithm);
// 用指定的密鑰和模式初始化Cipher對象
c1.init(Cipher.DECRYPT_MODE, key, sr);
// 對要解密的內容進行編碼處理
cipherByte = c1.doFinal(hex2byte(sInfo));
} catch (Exception e) {
e.printStackTrace();
}
// return byte2hex(cipherByte);
return new String(cipherByte);
}
// /////////////////////////////////////////////////////////////////////////////
/**
* 創建密匙組,並將公匙,私匙放入到指定文件中
*
* 默認放入mykeys.bat文件中
*/
public void createPairKey() {
try {
// 根據特定的演算法一個密鑰對生成器
KeyPairGenerator keygen = KeyPairGenerator.getInstance("DSA");
// 加密隨機數生成器 (RNG)
SecureRandom random = new SecureRandom();
// 重新設置此隨機對象的種子
random.setSeed(1000);
// 使用給定的隨機源(和默認的參數集合)初始化確定密鑰大小的密鑰對生成器
keygen.initialize(512, random);// keygen.initialize(512);
// 生成密鑰組
KeyPair keys = keygen.generateKeyPair();
// 得到公匙
PublicKey pubkey = keys.getPublic();
// 得到私匙
PrivateKey prikey = keys.getPrivate();
// 將公匙私匙寫入到文件當中
doObjToFile("mykeys.bat", new Object[] { prikey, pubkey });
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
/**
* 利用私匙對信息進行簽名 把簽名後的信息放入到指定的文件中
*
* @param info
* 要簽名的信息
* @param signfile
* 存入的文件
*/
public void signToInfo(String info, String signfile) {
// 從文件當中讀取私匙
PrivateKey myprikey = (PrivateKey) getObjFromFile("mykeys.bat", 1);
// 從文件中讀取公匙
PublicKey mypubkey = (PublicKey) getObjFromFile("mykeys.bat", 2);
try {
// Signature 對象可用來生成和驗證數字簽名
Signature signet = Signature.getInstance("DSA");
// 初始化簽署簽名的私鑰
signet.initSign(myprikey);
// 更新要由位元組簽名或驗證的數據
signet.update(info.getBytes());
// 簽署或驗證所有更新位元組的簽名,返回簽名
byte[] signed = signet.sign();
// 將數字簽名,公匙,信息放入文件中
doObjToFile(signfile, new Object[] { signed, mypubkey, info });
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 讀取數字簽名文件 根據公匙,簽名,信息驗證信息的合法性
*
* @return true 驗證成功 false 驗證失敗
*/
public boolean validateSign(String signfile) {
// 讀取公匙
PublicKey mypubkey = (PublicKey) getObjFromFile(signfile, 2);
// 讀取簽名
byte[] signed = (byte[]) getObjFromFile(signfile, 1);
// 讀取信息
String info = (String) getObjFromFile(signfile, 3);
try {
// 初始一個Signature對象,並用公鑰和簽名進行驗證
Signature signetcheck = Signature.getInstance("DSA");
// 初始化驗證簽名的公鑰
signetcheck.initVerify(mypubkey);
// 使用指定的 byte 數組更新要簽名或驗證的數據
signetcheck.update(info.getBytes());
System.out.println(info);
// 驗證傳入的簽名
return signetcheck.verify(signed);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 將二進制轉化為16進制字元串
*
* @param b
* 二進制位元組數組
* @return String
*/
public 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;
}
}
return hs.toUpperCase();
}
/**
* 十六進制字元串轉化為2進制
*
* @param hex
* @return
*/
public byte[] hex2byte(String hex) {
byte[] ret = new byte[8];
byte[] tmp = hex.getBytes();
for (int i = 0; i < 8; i++) {
ret[i] = uniteBytes(tmp[i * 2], tmp[i * 2 + 1]);
}
return ret;
}
/**
* 將兩個ASCII字元合成一個位元組; 如:"EF"--> 0xEF
*
* @param src0
* byte
* @param src1
* byte
* @return byte
*/
public static byte uniteBytes(byte src0, byte src1) {
byte _b0 = Byte.decode("0x" + new String(new byte[] { src0 }))
.byteValue();
_b0 = (byte) (_b0 << 4);
byte _b1 = Byte.decode("0x" + new String(new byte[] { src1 }))
.byteValue();
byte ret = (byte) (_b0 ^ _b1);
return ret;
}
/**
* 將指定的對象寫入指定的文件
*
* @param file
* 指定寫入的文件
* @param objs
* 要寫入的對象
*/
public void doObjToFile(String file, Object[] objs) {
ObjectOutputStream oos = null;
try {
FileOutputStream fos = new FileOutputStream(file);
oos = new ObjectOutputStream(fos);
for (int i = 0; i < objs.length; i++) {
oos.writeObject(objs[i]);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 返回在文件中指定位置的對象
*
* @param file
* 指定的文件
* @param i
* 從1開始
* @return
*/
public Object getObjFromFile(String file, int i) {
ObjectInputStream ois = null;
Object obj = null;
try {
FileInputStream fis = new FileInputStream(file);
ois = new ObjectInputStream(fis);
for (int j = 0; j < i; j++) {
obj = ois.readObject();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return obj;
}
/**
* 測試
*
* @param args
*/
public static void main(String[] args) {
CryptTest jiami = new CryptTest();
// 執行MD5加密"Hello world!"
System.out.println("Hello經過MD5:" + jiami.encryptToMD5("Hello"));
// 生成一個DES演算法的密匙
SecretKey key = jiami.createSecretKey("DES");
// 用密匙加密信息"Hello world!"
String str1 = jiami.encryptToDES(key, "Hello");
System.out.println("使用des加密信息Hello為:" + str1);
// 使用這個密匙解密
String str2 = jiami.decryptByDES(key, str1);
System.out.println("解密後為:" + str2);
// 創建公匙和私匙
jiami.createPairKey();
// 對Hello world!使用私匙進行簽名
jiami.signToInfo("Hello", "mysign.bat");
// 利用公匙對簽名進行驗證。
if (jiami.validateSign("mysign.bat")) {
System.out.println("Success!");
} else {
System.out.println("Fail!");
}
}
}

❻ 文字加密方式有哪些

1. WPA-PSK/WPA2-PSK

WPA-PSK/WPA2-PSK安全類型其實是WPA/WPA2的一種簡化版本,它是基於共享密鑰的WPA模式,安全性很高,設置也比較簡單,適合普通家庭用戶和小型企業使用.

認證類型: 該項用來選擇系統採用的安全模式,即自動、WPA-PSK、WPA2-PSK.

自動:若選擇該項,路由器會根據主機請求自動選擇WPA-PSK或WPA2-PSK安全模式.

加密演算法: 該項用來選擇對無線數據進行加密的安全演算法,選項有自動、TKIP、AES.默認選項為自動,選擇該項後,路由器將根據實際需要自動選擇TKIP或AES加密方式.注意11N模式不支持TKIP演算法.

PSK密碼: 該項是WPA-PSK/WPA2-PSK的初始設置密鑰,設置時,要求為8-63個ASCII字

符或8-64個十六進制字元.

組密鑰更新周期:該項設置廣播和組播密鑰的定時更新周期,以秒為單位,最小值為30,若該值為0,則表示不進行更新.

2. WPA/WPA2

WPA/WPA2是一種比WEP強大的加密演算法,選擇這種安全類型,路由器將採用Radius伺服器進行身份認證並得到密鑰的WPA或WPA2安全模式.由於要架設一台專用的認證伺服器,代價比較昂貴且維護也很復雜,所以不推薦普通用戶使用此安全類型.

3. WEP

WEP是Wired Equivalent Privacy的縮寫,它是一種基本的加密方法,其安全性不如另外兩種安全類型高.選擇WEP安全類型,路由器將使用802.11基本的WEP安全模式.這里需要注意的是因為802.11N不支持此加密方式,如果您選擇此加密方式,路由器可能會工作在較低的傳輸速率上.

這里特別需要說明的是,三種無線加密方式對無線網路傳輸速率的影響也不盡相同.由於IEEE 802.11n標准不支持以WEP加密或TKIP加密演算法的高吞吐率,所以如果用戶選擇了WEP加密方式或WPA-PSK/WPA2-PSK加密方式的TKIP演算法,無線傳輸速率將會自動限制在11g水平(理論值54Mbps,實際測試成績在20Mbps左右).

也就是說,如果用戶使用的是符合IEEE 802.11n標準的無線產品(理論速率150M或300M),那麼無線加密方式只能選擇WPA-PSK/WPA2-PSK的AES演算法加密,否則無線傳輸速率將會明顯降低.而如果用戶使用的是符合IEEE 802.11g標準的無線產品,那麼三種加密方式都可以很好的兼容,不過仍然不建議大家選擇WEP這種較老且已經被破解的加密方式,最好可以升級一下無線路由。

❼ 在c++中如何加密一個字元串

定義一個map<char,char>類型的對象dict,表示字典
map的key是待加密的字元,value是加密後的字元
for(int i = 0;i<string2.size();i++)
{
string2[i] = dict[string2[i]] //dict[string2[i]]表示key 為 string[2] 的 value

}

❽ C語言怎麼加密字元

#include<stdio.h>
#include<string.h>
intmain()
{
charstr[]="00000",str2[]="00000",*p=str,*p2=str2;
printf("輸入5個字母:");
while(*p!=0)
{
scanf("%c",p);
if(*p==' ')
continue;
if(*p<'A'||(*p>'Z'&&*p<'a')||*p>'z')//輸入驗證,必須是字母
{
printf("只能輸入字母,請重新輸入 ");
p=str;
p2=str2;
fflush(stdin);//輸入有錯重新輸入前清空緩沖區。fflush屬於c擴展函數,正常使用沒問題,如需在linuxggc上使用,考慮多次調用getchar函數來清空
}
else
{
*p2=(*p)+4;
if(*p2>90&&*p2<97)//大寫字母加4,最大位不超出
*p2='A'+(*p2-90)-1;
if(*p2>122)//小寫字母加4,最大位不超出
*p2='a'+(*p2-122)-1;
p2++;
p++;
}
}

printf("原字元串為:%s 加密後的字元串為:%s ",str,str2);
return0;
}

❾ C語言如何進行字元加密

進行字元加密是很多種的。
數據加密的基本過程就是對原來為明文的文件或數據按某種演算法進行處理,使其成為不可讀的一段代碼為「密文」,使其只能在輸入相應的密鑰之後才能顯示出原容,通過這樣的途徑來達到保護數據不被非法人竊取、閱讀的目的。 該過程的逆過程為解密,即將該編碼信息轉化為其原來數據的過程。

熱點內容
php怎麼訪問地址 發布:2025-05-18 01:29:43 瀏覽:320
fbe加密 發布:2025-05-18 01:16:34 瀏覽:250
求中點編程 發布:2025-05-18 01:03:14 瀏覽:840
安卓pay是什麼 發布:2025-05-18 01:02:27 瀏覽:747
免費手游掛機腳本 發布:2025-05-18 00:55:43 瀏覽:354
sd卡手機存儲系統存儲 發布:2025-05-18 00:55:28 瀏覽:637
pythonlistintstr 發布:2025-05-18 00:48:18 瀏覽:604
輕應用緩存 發布:2025-05-18 00:31:02 瀏覽:252
鳥存儲空氣 發布:2025-05-18 00:20:24 瀏覽:201
linux刻錄iso 發布:2025-05-18 00:16:15 瀏覽:663