如何使用RSA算法加密密码? [英] How do you encrypt a password by using the RSA Algorithm?
问题描述
我是加密技术的新手,我得到了这些说明
I am new to encryption and I got these instructions
使用RSA算法,并使用PKCS#1.5填充(不是OAEP),然后将结果添加到加密流中-这将成为加密密码."我代码中的字符串os是一个公共密钥.
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
.
I have imported some jars; org-apache-commons-codec.jar
and bcprov-jdk15-130.jar
.
由于某些原因,我在此行上收到错误 System.out.println("\ n" +"\ n" +"encryptionresult" +"\ n" +"\ n" + Base64.toBase64String(cipher.doFinal(initiatorpassword.getBytes()))+"\ n");
For some reasons I get an error on this line System.out.println("\n"+ "\n" +"encryptionresult" + "\n" + "\n" + Base64.toBase64String(cipher.doFinal(initiatorpassword.getBytes())) + "\n");
该错误仅在整个字符串上突出显示 toBase64String
.
The error highlights toBase64String
only on the whole string.
错误:找不到符号方法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;
}
}
推荐答案
您正在寻找的方法是
我不确定您从何处获得对 Base64.toBase64String
的引用,但是快速搜索显示它非常类似于类似的C#方法.
I'm not sure where you got the reference to Base64.toBase64String
, but a quick search shows that is very close to a similar C# method.
这篇关于如何使用RSA算法加密密码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!