BouncyCastle 제품군을 사용하여 Java에서 3DES를 사용하여 일반 텍스트를 암호화하는 데 실패했습니다. 이 결과는 나중에 해독 할 예정이므로 기존 C# 구현에 의해 생성 된 결과와 일치해야합니다.CES 구현과 다른 출력을보고하는 3DES 암호화 구현
나는 Java에서 C# algo의 "동급"을 제작했다고 확신하지만 다른 결과가 계속 나타납니다. 누군가가 친절하게 두 발췌문을보고 조언 할 수 있습니까? 나는 가장 감사 할 것입니다.
는C 번호 암호화 : 나는 진수로 결과를 변환에 사용이 도우미 기능이 있습니다
public static byte[] encryptStringToBytes_3DES(string plainText, string passKey)
{
// Check arguments.
if (plainText == null || plainText.Length <= 0)
throw new ArgumentNullException("plainText");
// Declare the streams used
// to encrypt to an in memory
// array of bytes.
MemoryStream msEncrypt = null;
CryptoStream csEncrypt = null;
StreamWriter swEncrypt = null;
ASCIIEncoding ascii = new System.Text.ASCIIEncoding();
// used to encrypt the data.
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
string passphrase = passKey;
byte[] iv = ascii.GetBytes("AVREWASH");
byte[] key = ascii.GetBytes(passphrase);
try
{
// Create a TripleDES object
// with the specified key and IV.
//Console.WriteLine("Key size is " + tdes.KeySize+" and IV is "+tdes.IV+" and that of key is "+key.Length);
tdes.Key = key;
tdes.IV = iv;
tdes.Padding = PaddingMode.Zeros;
// Create a decrytor to perform the stream transform.
ICryptoTransform encryptor = tdes.CreateEncryptor(tdes.Key, tdes.IV);
// Create the streams used for encryption.
msEncrypt = new MemoryStream();
csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write);
swEncrypt = new StreamWriter(csEncrypt);
//Write all data to the stream.
swEncrypt.Write(plainText);
}
catch (Exception ex)
{
Console.WriteLine("Error is " + ex.Message);
while (true)
{
}
}
finally
{
// Clean things up.
// Close the streams.
if (swEncrypt != null)
swEncrypt.Close();
if (csEncrypt != null)
csEncrypt.Close();
if (msEncrypt != null)
msEncrypt.Close();
// Clear the TripleDES object.
if (tdes != null)
tdes.Clear();
}
// Return the encrypted bytes from the memory stream.
return msEncrypt.ToArray();
}
... 자바는 "해당"암호화를 수행 할 예정 스 니펫을
public static string ByteArrayToString(byte[] ba)
{
string hex = BitConverter.ToString(ba);
return hex.Replace("-", "");
}
도 다음 :
public void encrypt(String plaintext, String IV, String tripleDesKey){
try{
SecretKey keySpec = new SecretKeySpec(tripleDesKey.getBytes("US-ASCII"),"DESede");
IvParameterSpec iv = new IvParameterSpec(IV.getBytes("US-ASCII"));
Cipher e_cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
e_cipher.init(Cipher.ENCRYPT_MODE, keySpec, iv);
byte [] cipherText = e_cipher.doFinal(plaintext.trim().getBytes("US-ASCII"));
System.out.println("Ciphertext: " + asHex(cipherText));
}
catch(Exception exc){
ex.printStackTrace();
}
}
여기에 해당하는 16 진수입니다. function
public static String asHex (byte buf[]) {
StringBuffer strbuf = new StringBuffer(buf.length * 2);
int i;
for (i = 0; i < buf.length; i++) {
if (((int) buf[i] & 0xff) < 0x10)
strbuf.append("0");
strbuf.append(Long.toString((int) buf[i] & 0xff, 16));
}
return strbuf.toString();
}
제발 도와주세요.
문자 세트 (UTF 등)를 두 번 확인합니다. String은 때때로 이런 종류의 물건을 다룰 때, 특히 getBytes()를 호출 할 때 기본값을가집니다. 모든 javadoc을 읽으십시오. –