RSA暗号化方式と復号方式の実現方法(推奨)
RSAscurity.java
package com.mstf.rsa;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import javax.crypto.Cipher;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
/*RSA 。 , , 。
RSA
RSA , ( 100 ) 。
,
:
1. p,q , n=p*q;
2. e , e (p-1)*(q-1)
3. Euclid d , e*d = 1(mod(p-1)*(q-1)) ( n,d )
4: (n,e) (n,d)
RSA
* , RSA DES 100 , 。
* RSA 。 。*/
public class RSAsecurity {
public static String src = "admin";
public void priENpubDE() {
try {
//
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
//
keyPairGenerator.initialize(1024);
//
KeyPair keyPair = keyPairGenerator.generateKeyPair();
//
RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair.getPublic();
//
RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate();
// 2. , ----
//
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(rsaPrivateKey.getEncoded());
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
// Cipher , getinstance
Cipher cipher = Cipher.getInstance("RSA");
//
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
byte[] result = cipher.doFinal(src.getBytes());
System.out.println(" , ---- :" + Base64.encode(result));
// 3. , ----
//
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(rsaPublicKey.getEncoded());
keyFactory = KeyFactory.getInstance("RSA");
PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
cipher = Cipher.getInstance("RSA");
//
cipher.init(Cipher.DECRYPT_MODE, publicKey);
result = cipher.doFinal(result);
System.out.println(" , ---- :" + new String(result));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void pubENpriDE() {
try {
// 1.
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
//
keyPairGenerator.initialize(512);
//
KeyPair keyPair = keyPairGenerator.generateKeyPair();
//
RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair.getPublic();
//
RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate();
// 2. , ----
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(rsaPublicKey.getEncoded());
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
//
// Cipher , getinstance
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
//
byte[] result = cipher.doFinal(src.getBytes());
System.out.println(" , ---- :" + Base64.encode(result));
// 3. , -----
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(rsaPrivateKey.getEncoded());
keyFactory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
//
cipher.init(Cipher.DECRYPT_MODE, privateKey);
//
result = cipher.doFinal(result);
System.out.println(" , ----- :" + new String(result));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
RSA test.java
package com.mstf.rsa;
import com.mstf.rsa.RSAsecurity;
public class RSAtest {
public static void main(String[] args) {
RSAsecurity rsAsecurity = new RSAsecurity();
System.out.println(" :");
rsAsecurity.priENpubDE();
System.out.println(" :");
rsAsecurity.pubENpriDE();
}
}
以上のRSA暗号化方式と復号方式の実現方法(推奨)は、小編集が皆さんに共有した内容の全部です。参考にしていただければと思います。どうぞよろしくお願いします。