1
0
Fork 0

Rsa로직 추가

This commit is contained in:
Sangbum Kim 2016-11-27 23:50:02 +09:00
parent 0128f2b843
commit b8d24b42ef
3 changed files with 126 additions and 3 deletions

View File

@ -12,5 +12,9 @@ repositories {
}
dependencies {
compile group: 'org.bouncycastle:bcprov-jdk15on:1.54'
compile 'org.bouncycastle:bcprov-jdk15on:1.54'
compile 'commons-io:commons-io:2.4'
// compile 'org.whispersystems:curve25519-android:0.2.5'
// compile 'org.whispersystems:curve25519-java:0.2.5'
}

View File

@ -1,4 +1,4 @@
#Mon Aug 01 04:55:26 KST 2016
#Sun Nov 27 22:11:34 KST 2016
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME

View File

@ -0,0 +1,119 @@
import javax.crypto.Cipher;
import java.security.*;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
/**
* RSA Helper Encryption Class
* Created by brianplummer on 4/29/14.
*/
public class RSACryptors {
//key length
public static final int KEY_LENGTH = 4096;
//main family of rsa
public static final String RSA = "RSA";
/**
* generate a 2048 bit RSA key
*
* @return a 2048 bit rsa key
*/
public static KeyPair generateRSAKey() {
KeyPairGenerator kpg = null;
try {
//get an RSA key generator
kpg = KeyPairGenerator.getInstance(RSA);
} catch (NoSuchAlgorithmException e) {
System.out.printf("%s\n", e.getMessage());
throw new RuntimeException(e);
}
//initialize the key to 2048 bits
kpg.initialize(KEY_LENGTH);
//return the generated key pair
return kpg.genKeyPair();
}
/**
* main RSA encrypt method
*
* @param plain plain text you want to encrypt
* @param publicKey public key to encrypt with
* @return encrypted text
*/
public static byte[] encryptRSA(byte[] plain, PublicKey publicKey) {
byte[] enc = null;
try {
Cipher cipher = Cipher.getInstance(RSA);
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
enc = cipher.doFinal(plain);
}
//no need to catch 4 different exceptions
catch (Exception e) {
System.out.printf("%s\n", e.getMessage());
throw new RuntimeException(e);
}
return enc;
}
/**
* main RSA decrypt method
*
* @param enc encrypted text you want to dcrypt
* @param privateKey private key to use for decryption
* @return plain text
*/
public static byte[] decryptRSA(byte[] enc, PrivateKey privateKey) {
byte[] plain = null;
try {
Cipher cipher = Cipher.getInstance(RSA);
cipher.init(Cipher.DECRYPT_MODE, privateKey);
plain = cipher.doFinal(enc);
}
//no need to catch 4 different exceptions
catch (Exception e) {
System.out.printf("%s\n", e.getMessage());
throw new RuntimeException(e);
}
return plain;
}
public static void main(String[] args) throws NoSuchAlgorithmException, InvalidKeySpecException {
final StringBuffer log = new StringBuffer();
// KeyPair pair = RSACryptors.generateRSAKey();
// System.out.println(new String(Base64.encode(pair.getPrivate().getEncoded())));
// System.out.println("\n\n");
// System.out.println(new String(Base64.encode(pair.getPublic().getEncoded())));
final String priKey = "MIIJQgIBADANBgkqhkiG9w0BAQEFAASCCSwwggkoAgEAAoICAQC0deqTP1T8yOAvGu8UXxx2ERDeofF6RHuqkJ1dkbaCU59m/dH1L5+tNEKWNyqZIbfF/o5/UTjRA/rP2oQ2jA9IFtbBTQVMf1VDE3mnCZMzU3ATX7sWU0ej2WuHpgBV5DdroyUWVZGmpcvF1PnCLWIH2rLFA0SEoAspth8vqVrsY7w/nPOsUmwB10W+KRKhJa1UhGBUPjtaq2Y3vXnDkwqaT326K5DpjFF4wEWoYshmkkAtBetU0tPCkbqkNLGBj7qZiHdcr3wCOroa28kpYRgmD6nezVdkWcVdRNZr/HHxdtAHNtxH+6CAfU36kVd7GdYW5DUnoHXt4q2hZ3M0zl9UP4ZZk5MClwu4W9ALb6jLxY5YBcKkLuAXs94lESELhEJnarcTPoMfhtr6YvVuJBOajHHylV2J+8JQ18NI+EITJxh/JeWrPtQGH/dYS9v0MqgXFMu67UAOOot67+8e1D2Iq5bMX/Six1uwVCMW/CRpPOfM5qi/H89GkjSG4h2B79E+6W0Y+bOnTNoSSgjQf3AhpGudhKmsidR/6h2F1RhgTMMU4i3KZmU66if3dJi3GstmQdkve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wEAAQ==";
final X509EncodedKeySpec pubSpec = new X509EncodedKeySpec(Base64.decode(pubKey));
final PKCS8EncodedKeySpec priSpec = new PKCS8EncodedKeySpec(Base64.decode(priKey));
final KeyFactory kf = KeyFactory.getInstance("RSA");
final PublicKey pubKeyObj=kf.generatePublic(pubSpec);
final PrivateKey priKeyObj=kf.generatePrivate(priSpec);
final String source="encryptRSA";
log.append(" source : ");
log.append(source);
log.append('\n');
final byte[] encrypted=RSACryptors.encryptRSA(source.getBytes(),pubKeyObj);
log.append(" {");
for (int j = 0; j < encrypted.length; j++) {
log.append(String.valueOf(encrypted[j]) + ",");
}
log.append(" }\n");
final byte[] decrypted=RSACryptors.decryptRSA(encrypted,priKeyObj);
log.append(" trans : ");
log.append(new String(decrypted));
log.append('\n');
System.out.println(log.toString());
}
}