java rsa私钥加密是什么?让我们一起来了解一下吧!
java rsa私钥加密是一种加密算法。私钥加密算法是用私钥来进行加密与解密信息。私钥加密也被称作对称加密,原因是加密与解密使用的秘钥是同一个。
RSA加密需要注意的事项如下:
1 首先产生公钥与私钥
2 设计加密与解密的算法
3 私钥加密的数据信息只能由公钥可以解密
4 公钥加密的数据信息只能由私钥可以解密
实战演练,具体步骤如下: public class RsaCryptTools { private static final String CHARSET = "utf-8"; private static final Base64Decoder decoder64 = Base64getDecoder(); private static final Base64Encoder encoder64 = Base64getEncoder(); / 生成公私钥 @param keySize @return @throws NoSuchAlgorithmException / public static SecretKey generateSecretKey(int keySize) throws NoSuchAlgorithmException { //生成密钥对 KeyPairGenerator keyGen = KeyPairGeneratorgetInstance("RSA"); keyGeninitialize(keySize, new SecureRandom()); KeyPair pair = keyGengenerateKeyPair(); PrivateKey privateKey = pairgetPrivate(); PublicKey publicKey = pairgetPublic(); //这里可以将密钥对保存到本地 return new SecretKey(encoder64encodeToString(publicKeygetEncoded()), encoder64encodeToString(privateKeygetEncoded())); } / 私钥加密 @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 = CiphergetInstance("RSA/ECB/PKCS1Padding"); cipherinit(CipherENCRYPT_MODE, getPrivateKey(privateInfoStr)); return encoder64encodeToString(cipherdoFinal(datagetBytes(CHARSET))); } / 公钥解密 @param data @param publicInfoStr @return / public static String decryptData(String data, String publicInfoStr) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeySpecException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, UnsupportedEncodingException { byte[] encryptDataBytes=decoder64decode(datagetBytes(CHARSET)); //解密 Cipher cipher = CiphergetInstance("RSA/ECB/PKCS1Padding"); cipherinit(CipherDECRYPT_MODE, getPublicKey(publicInfoStr)); return new String(cipherdoFinal(encryptDataBytes), CHARSET); } private static PublicKey getPublicKey(String base64PublicKey) throws NoSuchAlgorithmException, InvalidKeySpecException { X509EncodedKeySpec keySpec = new X509EncodedKeySpec(Base64getDecoder()decode(base64PublicKeygetBytes())); KeyFactory keyFactory = KeyFactorygetInstance("RSA"); return keyFactorygeneratePublic(keySpec); } private static PrivateKey getPrivateKey(String base64PrivateKey) throws NoSuchAlgorithmException, InvalidKeySpecException { PrivateKey privateKey = null; PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(Base64getDecoder()decode(base64PrivateKeygetBytes())); KeyFactory keyFactory = null; keyFactory = KeyFactorygetInstance("RSA"); privateKey = keyFactorygeneratePrivate(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) { thispublicKey = publicKey; thisprivateKey = privateKey; } public String getPublicKey() { return publicKey; } public void setPublicKey(String publicKey) { thispublicKey = publicKey; } public String getPrivateKey() { return privateKey; } public void setPrivateKey(String privateKey) { thisprivateKey = 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); fgetParentFile()mkdirs(); try(FileOutputStream fos = new FileOutputStream(f)) { foswrite(key); fosflush(); } } public static void main(String[] args) throws NoSuchAlgorithmException, NoSuchPaddingException, IOException, BadPaddingException, IllegalBlockSizeException, InvalidKeyException, InvalidKeySpecException { SecretKey secretKey = generateSecretKey(2048); Systemoutprintln(secretKey); String enStr = encryptData("你好测试测试", secretKeygetPrivateKey()); Systemoutprintln(enStr); String deStr = decryptData(enStr, secretKeygetPublicKey()); Systemoutprintln(deStr); enStr = encryptData("你好测试测试hello", secretKeygetPrivateKey()); Systemoutprintln(enStr); deStr = decryptData(enStr, secretKeygetPublicKey()); Systemoutprintln(deStr); } }
RSA 是一种非对称加密算法,很多表单的密码都采用 RSA 加密。
使用 RSA 一般需要先产生一对公钥和私钥,当采用公钥加密时,使用私钥解密;采用私钥加密时,使用公钥解密。
执行结果如下:
在实际应用中,我们可以先执行 genKeyPair 先生成一对密钥,将该对密钥保存在配置文件中,然后在加密时,调用 encrypt(str, publicKey) 方法使用公钥对文本进行加密,在解密时,调用 decrypt(strEn, privateKey) 方法使用私钥对文本进行解密,即可。
/数据只能是大写字母组成的字符串。
加密的时候,输入Y,然后输入要加密的文本(大写字母)
解密的时候,输入N,然后输入一个整数n表示密文的个数,然后n个整数表示加密时候得到的密文。
/
/RSA algorithm /
#include <stdioh>
#include <stringh>
#include <stdlibh>
#define MM 7081
#define KK 1789
#define PHIM 6912
#define PP 85
typedef char strtype[10000];
int len;
long nume[10000];
int change[126];
char antichange[37];
void initialize()
{ int i;
char c;
for (i = 11, c = 'A'; c <= 'Z'; c ++, i ++)
{ change[c] = i;
antichange[i] = c;
}
}
void changetonum(strtype str)
{ int l = strlen(str), i;
len = 0;
memset(nume, 0, sizeof(nume));
for (i = 0; i < l; i ++)
{ nume[len] = nume[len] 100 + change[str[i]];
if (i % 2 == 1) len ++;
}
if (i % 2 != 0) len ++;
}
long binamod(long numb, long k)
{ if (k == 0) return 1;
long curr = binamod (numb, k / 2);
if (k % 2 == 0)
return curr curr % MM;
else return (curr curr) % MM numb % MM;
}
long encode(long numb)
{ return binamod(numb, KK);
}
long decode(long numb)
{ return binamod(numb, PP);
}
main()
{ strtype str;
int i, a1, a2;
long curr;
initialize();
puts("Input 'Y' if encoding, otherwise input 'N':");
gets(str);
if (str[0] == 'Y')
{ gets(str);
changetonum(str);
printf("encoded: ");
for (i = 0; i < len; i ++)
{ if (i) putchar('-');
printf(" %ld ", encode(nume[i]));
}
putchar('\n');
}
else
{ scanf("%d", &len);
for (i = 0; i < len; i ++)
{ scanf("%ld", &curr);
curr = decode(curr);
a1 = curr / 100;
a2 = curr % 100;
printf("decoded: ");
if (a1 != 0) putchar(antichange[a1]);
if (a2 != 0) putchar(antichange[a2]);
}
putchar('\n');
}
putchar('\n');
system("PAUSE");
return 0;
}
/
测试:
输入:
Y
FERMAT
输出:
encoded: 5192 - 2604 - 4222
输入
N
3 5192 2604 4222
输出
decoded: FERMAT
/
以上就是关于java rsa私钥加密全部的内容,包括:java rsa私钥加密、RSA 加密解密、RSA加密算法怎样用C语言实现 急急急!!!等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)