我是新來加密,我得到了這些指令如何使用RSA算法加密密碼?
Use the RSA algorithm, and use PKCS #1.5 padding (not OAEP), and add the result to the encrypted stream –this becomes the encrypted password." String os from my code is a public key.
我已經進口了一些罐子; org-apache-commons-codec.jar
和bcprov-jdk15-130.jar
。
對於一些原因,我得到一個錯誤,在這條線System.out.println("\n"+ "\n" +"encryptionresult" + "\n" + "\n" + Base64.toBase64String(cipher.doFinal(initiatorpassword.getBytes())) + "\n");
僅在整個字符串錯誤凸顯toBase64String
。
ERROR: "cannot find symbol method toBase64String."
import java.io.IOException;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.X509EncodedKeySpec;
import javax.crypto.Cipher;
import org.bouncycastle.util.encoders.Base64;
import sun.misc.BASE64Decoder;
public class encrypt {
public static void main(String[] args) {
encrypt obj = new encrypt();
obj.process();
}
public void process() {
try {
String initiatorpassword = "giddibon!";
String os = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqLcFdVcV7HdEOotsNLoMPhD74CX1ejzcgfNuiJNy9pTySxbszRBCWxmok3Unul4rX/zyVD/6vDb9nbqRywZIgR46UOn+tR3vGXXPX6igxgS6DYTaQV8W858yOGLuoYwRi5xeQJfczAMU4o+sCxlBbMCqYs4nzW81fi8iF2OEUdrfJcbamhSnksdgfD/nomWy9MESAz1QufrOBnaRX2N0CKsi8SNmzsghpfP15VLiIVV8YXPFKtd9sY37FpY28OKGjKG5wdije/bzFL8qEcPDhqYGuVaGkhX1bkI0iH+UcFtYYrZv/Fyb5jRHXmNLiq4mMG0fMH8ENxNACFtRZTDIIQIDAQAB";
PublicKey publickeyos = rpos(os);
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.ENCRYPT_MODE, publickeyos);
//error on this line
System.out.println("\n"+ "\n" +"encryptionresult" + "\n" + "\n" + Base64.toBase64String(cipher.doFinal(initiatorpassword.getBytes())) + "\n");
} catch (Exception e) {
e.printStackTrace();
}
}
public PublicKey rpos(String file) throws InvalidKeySpecException, NoSuchAlgorithmException, IOException {
byte[] keyBytes = file.getBytes();
String pubKey = new String(keyBytes);
BASE64Decoder decoder = new BASE64Decoder();
keyBytes = decoder.decodeBuffer(pubKey);
// generate public key
X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey publicKey = keyFactory.generatePublic(spec);
return publicKey;
}
}
JDK正在使用哪個版本,是編譯時錯誤?你是否遇到運行時錯誤,是堆棧跟蹤的錯誤部分? –
@RamachandranGA顯然我無法運行,因爲它有突出顯示的錯誤。錯誤:「無法找到符號方法toBase64String。」 java版本「1.7.0_101」 –
您不需要任何外部JAR文件。您可以在標準JRE內完成所有操作。 – EJP