RAS加解密

/**
* 创建公钥和私钥
* @return
*/
public static Pair<String,String> createPubPrivateKey() throws Exception {
   // 公钥私钥构建器
   KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
   // 初始化大小,64的整数倍
   keyPairGenerator.initialize(512);
   // 生成公钥和私钥
   KeyPair keyPair = keyPairGenerator.generateKeyPair();
   PublicKey aPublic = keyPair.getPublic();
   PrivateKey aPrivate = keyPair.getPrivate();

   MutablePair<String, String> pair = new MutablePair<>();
   pair.setLeft(Base64.encodeBase64String(aPrivate.getEncoded()));
   pair.setRight(Base64.encodeBase64String(aPublic.getEncoded()));
   System.out.println("private key: "+pair.getLeft());
   System.out.println("public key: "+pair.getRight());
   System.out.println("--------------------------------------------------------------------------------");
   return pair;
}

/**
* rsa 加密
* @param plaintext 明文
* @param pubKey 公钥
* @return
* @throws Exception
*/
public static String encrypt(String plaintext, String pubKey) throws Exception {
   X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(Base64.decodeBase64(pubKey));
   KeyFactory keyFactory = KeyFactory.getInstance("RSA");
   PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);

   Cipher cipher = Cipher.getInstance("RSA");
   cipher.init(Cipher.ENCRYPT_MODE, publicKey);
   byte[] result = cipher.doFinal(plaintext.getBytes("UTF-8"));
   return Base64.encodeBase64String(result);
}

/**
* rsa 解密
* @param ciphertext 密文
* @param prikey 私钥
* @return
* @throws Exception
*/
public static String decrypt(String ciphertext, String prikey) throws Exception {
   PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(Base64.decodeBase64(prikey));
   KeyFactory keyFactory = KeyFactory.getInstance("RSA");
   PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
   Cipher cipher = Cipher.getInstance("RSA");
   cipher.init(Cipher.DECRYPT_MODE, privateKey);
   byte[] result = cipher.doFinal(Base64.decodeBase64(ciphertext));
   return new String(result);
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
RAS解密算法是一种非对称密算法,常用于数据密和数字签名。以下是RAS解密算法的C语言实现示例: ``` #include <stdio.h> #include <stdlib.h> #include <string.h> #include <openssl/rsa.h> #include <openssl/pem.h> int main() { RSA *rsa = NULL; BIO *pubkeybio = NULL; BIO *prikeybio = NULL; unsigned char *msg = "Hello, world!"; unsigned char *encrypted = NULL; unsigned char *decrypted = NULL; int encryptedlen, decryptedlen; // 生成RSA密钥对 rsa = RSA_generate_key(2048, RSA_F4, NULL, NULL); if (rsa == NULL) { printf("RSA key generation failed!\n"); exit(EXIT_FAILURE); } // 生成公钥PEM文件 pubkeybio = BIO_new_file("public.pem", "w+"); if (pubkeybio == NULL) { printf("Unable to open public key file!\n"); exit(EXIT_FAILURE); } PEM_write_bio_RSAPublicKey(pubkeybio, rsa); BIO_free(pubkeybio); // 生成私钥PEM文件 prikeybio = BIO_new_file("private.pem", "w+"); if (prikeybio == NULL) { printf("Unable to open private key file!\n"); exit(EXIT_FAILURE); } PEM_write_bio_RSAPrivateKey(prikeybio, rsa, NULL, NULL, 0, NULL, NULL); BIO_free(prikeybio); // 密 encrypted = (unsigned char *)malloc(RSA_size(rsa)); encryptedlen = RSA_public_encrypt(strlen(msg) + 1, msg, encrypted, rsa, RSA_PKCS1_PADDING); if (encryptedlen == -1) { printf("Encryption failed!\n"); exit(EXIT_FAILURE); } printf("Encrypted message: %s\n", encrypted); // 解密 decrypted = (unsigned char *)malloc(RSA_size(rsa)); decryptedlen = RSA_private_decrypt(encryptedlen, encrypted, decrypted, rsa, RSA_PKCS1_PADDING); if (decryptedlen == -1) { printf("Decryption failed!\n"); exit(EXIT_FAILURE); } printf("Decrypted message: %s\n", decrypted); RSA_free(rsa); free(encrypted); free(decrypted); return 0; } ``` 以上代码使用OpenSSL库实现了RSA密钥对的生成、公钥和私钥的输出、密和解密操作。在实际使用中,需要根据具体需求进行修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值