rsa加密工具
『壹』 如何使用RSA簽名給給信息加密和解密
【實例下載】本文介紹RSA2加密與解密,RSA2是RSA的加強版本,在密鑰長度上採用2048, RSA2比RSA更安全,更可靠, 本人的另一篇文章RSA已經發表,有想了解的可以點開下面的RSA文章
『貳』 java rsa私鑰加密
java rsa私鑰加密是什麼?讓我們一起來了解一下吧!
java rsa私鑰加密是一種加密演算法。私鑰加密演算法是用私鑰來進行加密與解密信息。私鑰加密也被稱作對稱加密,原因是加密與解密使用的秘鑰是同一個。
RSA加密需要注意的事項如下:
1. 首先產生公鑰與私鑰
2. 設計加密與解密的演算法
3. 私鑰加密的數據信息只能由公鑰可以解密
4. 公鑰加密的數據信息只能由私鑰可以解密
實戰演練,具體步驟如下: public class RsaCryptTools { private static final String CHARSET = "utf-8"; private static final Base64.Decoder decoder64 = Base64.getDecoder(); private static final Base64.Encoder encoder64 = Base64.getEncoder(); /** * 生成公私鑰 * @param keySize * @return * @throws NoSuchAlgorithmException */ public static SecretKey generateSecretKey(int keySize) throws NoSuchAlgorithmException { //生成密鑰對 KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA"); keyGen.initialize(keySize, new SecureRandom()); KeyPair pair = keyGen.generateKeyPair(); PrivateKey privateKey = pair.getPrivate(); PublicKey publicKey = pair.getPublic(); //這里可以將密鑰對保存到本地 return new SecretKey(encoder64.encodeToString(publicKey.getEncoded()), encoder64.encodeToString(privateKey.getEncoded())); } /** * 私鑰加密 * @param data * @param privateInfoStr * @return * @throws IOException * @throws InvalidCipherTextException */ public static String encryptData(String data, String privateInfoStr) throws IOException, InvalidKeySpecException, NoSuchAlgorithmException, InvalidKeyException, NoSuchPaddingException, BadPaddingException, IllegalBlockSizeException { Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); cipher.init(Cipher.ENCRYPT_MODE, getPrivateKey(privateInfoStr)); return encoder64.encodeToString(cipher.doFinal(data.getBytes(CHARSET))); } /** * 公鑰解密 * @param data * @param publicInfoStr * @return */ public static String decryptData(String data, String publicInfoStr) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeySpecException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, UnsupportedEncodingException { byte[] encryptDataBytes=decoder64.decode(data.getBytes(CHARSET)); //解密 Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); cipher.init(Cipher.DECRYPT_MODE, getPublicKey(publicInfoStr)); return new String(cipher.doFinal(encryptDataBytes), CHARSET); } private static PublicKey getPublicKey(String base64PublicKey) throws NoSuchAlgorithmException, InvalidKeySpecException { X509EncodedKeySpec keySpec = new X509EncodedKeySpec(Base64.getDecoder().decode(base64PublicKey.getBytes())); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); return keyFactory.generatePublic(keySpec); } private static PrivateKey getPrivateKey(String base64PrivateKey) throws NoSuchAlgorithmException, InvalidKeySpecException { PrivateKey privateKey = null; PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(Base64.getDecoder().decode(base64PrivateKey.getBytes())); KeyFactory keyFactory = null; keyFactory = KeyFactory.getInstance("RSA"); privateKey = keyFactory.generatePrivate(keySpec); return privateKey; } /** * 密鑰實體 * @author hank * @since 2020/2/28 0028 下午 16:27 */ public static class SecretKey { /** * 公鑰 */ private String publicKey; /** * 私鑰 */ private String privateKey; public SecretKey(String publicKey, String privateKey) { this.publicKey = publicKey; this.privateKey = privateKey; } public String getPublicKey() { return publicKey; } public void setPublicKey(String publicKey) { this.publicKey = publicKey; } public String getPrivateKey() { return privateKey; } public void setPrivateKey(String privateKey) { this.privateKey = privateKey; } @Override public String toString() { return "SecretKey{" + "publicKey='" + publicKey + '\'' + ", privateKey='" + privateKey + '\'' + '}'; } } private static void writeToFile(String path, byte[] key) throws IOException { File f = new File(path); f.getParentFile().mkdirs(); try(FileOutputStream fos = new FileOutputStream(f)) { fos.write(key); fos.flush(); } } public static void main(String[] args) throws NoSuchAlgorithmException, NoSuchPaddingException, IOException, BadPaddingException, IllegalBlockSizeException, InvalidKeyException, InvalidKeySpecException { SecretKey secretKey = generateSecretKey(2048); System.out.println(secretKey); String enStr = encryptData("你好測試測試", secretKey.getPrivateKey()); System.out.println(enStr); String deStr = decryptData(enStr, secretKey.getPublicKey()); System.out.println(deStr); enStr = encryptData("你好測試測試hello", secretKey.getPrivateKey()); System.out.println(enStr); deStr = decryptData(enStr, secretKey.getPublicKey()); System.out.println(deStr); } }
『叄』 jsencrypt實現前端RSA非對稱加密解密(vue項目)
最近一個vue項目要求所有密碼數據需要使用RSA非對稱加密傳輸,以為挺簡單,結果開發過程中還是遇到了些問題,簡單做個筆記。同時也希望可以幫助到遇到同樣問題的道友門。
重點來了:使用jsencrypt實現RSA非對稱加解密
因為這里直接在前端加解密,所以需要一對現成的密鑰,我們通過 密鑰在線生成器 得到:
然後在需要使用的文件中引入JSEncrypt,我是將所有工具函數都封裝在一個js文件的,我就直接在該文件中引入,我看也有人是在main.js中引入的。
到這里我們的加密解密方法就完成了,在需要的地方直接拿過來用就好了!
大功告成!這樣就完了?我以為這樣就ok了。
當然,如果沒有遇到這個bug,就可以忽略下面的內容了。
從上面截圖可以看到,重啟項目的時候報錯: navigator is not defined
而且這個bug有點奇葩,先啟動項目再引入jsencrypt就什麼問題都沒有,但是先引入jsencrypt再啟動項目就報錯。這也是我前面能順利執行的原因所在。
通過好一通折騰,用了網上的各種方法,比如在main.js引入jsencrypt、引入jsdom之類的,都沒能解決這個問題,最終還是在jsencrypt的git相關 issue 下找到了這個問題的解決方案。
到這里問題就算基本解決了,但是由於項目組不止我一個前端,我不能要求每個同事或者以後接手維護項目的同事都要在node_moles中去替換文件。
所以就採用了另外一種方案:將jsencrypt.js通過在線js壓縮器壓縮至jsencrypt.min.js中,然後把jsencrypt.min.js放到src/assets/jsencrypt文件夾中,就不用npm install的方式了。
換了種方式,jsencrypt的引用方式需要做出相應的調整:
參考鏈接: RSA非對稱加密傳輸---前端加密&解密(VUE項目)
https://github.com/travist/jsencrypt/issues/144
PS:才疏學淺,如果有考慮不周之處或者有更好的解決方案,歡迎指正探討!
『肆』 如何利用OpenSSL庫進行RSA加密和解密
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<openssl/rsa.h>
#include<openssl/engine.h>
intmain(intargc,char*argv[])
{
printf("openssl_testbegin ");
RSA*rsa=NULL;
charoriginstr[]="hello ";//這是我們需要加密的原始數據
//allocateRSAstructure,首先需要申請一個RSA結構題用於存放生成的公私鑰,這里rsa就是這個結構體的指針
rsa=RSA_new();
if(rsa==NULL)
{
printf("RSA_newfailed ");
return-1;
}
//generateRSAkeys
BIGNUM*exponent;
exponent=BN_new();//生成RSA公私鑰之前需要選擇一個奇數(oddnumber)來用於生成公私鑰
if(exponent==NULL)
{
printf("BN_newfailed ");
gotoFAIL1;
}
if(0==BN_set_word(exponent,65537))//這里選擇奇數65537
{
printf("BN_set_wordfailed ");
gotoFAIL1;
}
//這里molus的長度選擇4096,小於1024的molus長度都是不安全的,容易被破解
if(0==RSA_generate_key_ex(rsa,4096,exponent,NULL))
{
printf("RSA_generate_key_exfailed ");
gotoFAIL;
}
char*cipherstr=NULL;
//分配一段空間用於存儲加密後的數據,這個空間的大小由RSA_size函數根據rsa算出
cipherstr=malloc(RSA_size(rsa));
if(cipherstr==NULL)
{
printf("malloccipherstrbuffailed ");
gotoFAIL1;
}
//下面是實際的加密過程,最後一個參數paddingtype,有以下幾種。
/*
RSA_PKCS1_PADDINGPKCS#1v1.5padding..
RSA_PKCS1_OAEP_PADDING
EME-OAEPasdefinedinPKCS#1v2.0withSHA-1,..
RSA_SSLV23_PADDING
PKCS#1v1.5paddingwithanSSL-.
RSA_NO_PADDING
RawRSAencryption.ntheapplicationcode..
*/
//這里首先用公鑰進行加密,選擇了RSA_PKCS1_PADDING
if(RSA_size(rsa)!=RSA_public_encrypt(strlen(originstr)+1,originstr,cipherstr,rsa,RSA_PKCS1_PADDING))
{
printf("encryptionfailure ");
gotoFAIL2;
}
printf("theoriginalstringis%s ",originstr);
printf("theencryptedstringis%s ",cipherstr);
//Now,let'
//下面來用私鑰解密,首先需要一個buffer用於存儲解密後的數據,這個buffer的長度要足夠(小於RSA_size(rsa))
//這里分配一個長度為250的字元數組,應該是夠用的。
chardecrypted_str[250];
intdecrypted_len;
if(-1=(decrypted_len=RSA_private_decrypt(256,cipherstr,decrypted_str,rsa,RSA_PKCS1_PADDING)))
{
printf("decryptionfailure ");
gotoFAIL2;
}
printf("decryptedstringlengthis%d,decryped_stris%s ",decrypted_len,decrypted_str);
FAIL2:
free(cipherstr);
FAIL1:
BN_free(exponent);
FAIL:
RSA_free(rsa);
return0;
}
以上是源代碼,下面使用下面的編譯命令在源碼所在路徑下生成可執行文件
gcc *.c -o openssl_test -lcrypto -ldl -L/usr/local/ssl/lib -I/usr/local/ssl/include
其中,-lcrypto和-ldl是必須的,前者是OpenSSL中的加密演算法庫,後者是用於成功載入動態庫。
『伍』 用RSA演算法設計加密軟體 C語言或者C++
UpdateData(TRUE);
m_miwencode=_T("");
CKEY_PRODUCErsa;
intcodelenght,codenum;
codelenght=m_yuanwencode.GetLength();
codenum=codelenght/3;
CStringstrmod;
strmod.Format(_T("%d"),Model);
ModeNum=strmod.GetLength();
intCryptograph;
for(inti=0;i<codenum;i++)
{
CStringstr;
str=m_yuanwencode.Mid(3*i,3);
intj=(str[0]-'0')*100+(str[1]-'0')*10+(str[2]-'0');
inttemp=1;
for(intk=0;k<PublicKey;k++)
{
temp*=j;
if(temp>=Model)
temp%=Model;
if(!temp)
Cryptograph=temp;
}
Cryptograph=temp%Model;
str.Format(_T("%d"),Cryptograph);
intstrnum=str.GetLength();
if(strnum!=ModeNum)
{
ints=ModeNum-strnum;
if(s==1)
{
str=_T("0")+str;
}
if(s==2)
{
str=_T("00")+str;
}
if(s==3)
{
str=_T("000")+str;
}
if(s==4)
{
str=_T("0000")+str;
}
}
m_miwencode+=str;
}
UpdateData(FALSE);
m_miwencode=_T("");
vs2005編寫的C++(mfc)程序。這個可以不,可以加密字元串,要的話把分給我,發你郵箱里