ras 加密

ras 加密

import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;

import javax.crypto.Cipher;

public class RsaTest {
 public static void main(String args[]) throws Exception {
  SecureRandom sr = new SecureRandom();
  KeyPairGenerator kg = KeyPairGenerator.getInstance("RSA");
  kg.initialize(512, sr);

  //产生新密钥对
  KeyPair kp = kg.generateKeyPair();
  //获得私匙
  PrivateKey prk = kp.getPrivate();
  //获得公钥
  PublicKey puk = kp.getPublic();
  
  String str = "你好吗?我在测试RSA测试^_^";
  Cipher cipher = Cipher.getInstance("RSA");
  cipher.init(Cipher.ENCRYPT_MODE, prk);
  byte[] dest = cipher.doFinal(str.getBytes());
  System.out.println(new String(dest));
  
  
  Cipher ciphers = Cipher.getInstance("RSA");
  ciphers.init(Cipher.DECRYPT_MODE,puk);  
  byte[] srcs = ciphers.doFinal(dest);
  System.out.println("解密后的数据:"+new String(srcs));
  
  
 }
}



</script>

你可能感兴趣的:(ras 加密)