Created
November 24, 2011 14:28
-
-
Save egtra/1391469 to your computer and use it in GitHub Desktop.
TweenEncryptString
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
' Tween - Client of Twitter | |
' Copyright (c) 2007-2011 kiri_feather (@kiri_feather) <kiri.feather@gmail.com> | |
' (c) 2008-2011 Moz (@syo68k) | |
' (c) 2008-2011 takeshik (@takeshik) <http://www.takeshik.org/> | |
' (c) 2010-2011 anis774 (@anis774) <http://d.hatena.ne.jp/anis774/> | |
' (c) 2010-2011 fantasticswallow (@f_swallow) <http://twitter.com/f_swallow> | |
' (c) 2011 Egtra (@egtra) <http://dev.activebasic.com/egtra/> | |
' All rights reserved. | |
' | |
' This file is part of Tween. | |
' | |
' This program is free software; you can redistribute it and/or modify it | |
' under the terms of the GNU General Public License as published by the Free | |
' Software Foundation; either version 3 of the License, or (at your option) | |
' any later version. | |
' | |
' This program is distributed in the hope that it will be useful, but | |
' WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | |
' or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
' for more details. | |
' | |
' You should have received a copy of the GNU General Public License along | |
' with this program. If not, see <http://www.gnu.org/licenses/>, or write to | |
' the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor, | |
' Boston, MA 02110-1301, USA. | |
Module Test | |
Public Sub Main() | |
Dim s = Console.ReadLine | |
Console.WriteLine(EncryptString(s)) | |
End Sub | |
Public Function ResizeBytesArray(ByVal bytes() As Byte, _ | |
ByVal newSize As Integer) As Byte() | |
Dim newBytes(newSize - 1) As Byte | |
If bytes.Length <= newSize Then | |
Dim i As Integer | |
For i = 0 To bytes.Length - 1 | |
newBytes(i) = bytes(i) | |
Next i | |
Else | |
Dim pos As Integer = 0 | |
Dim i As Integer | |
For i = 0 To bytes.Length - 1 | |
newBytes(pos) = newBytes(pos) Xor bytes(i) | |
pos += 1 | |
If pos >= newBytes.Length Then | |
pos = 0 | |
End If | |
Next i | |
End If | |
Return newBytes | |
End Function | |
Public Function EncryptString(ByVal str As String) As String | |
If String.IsNullOrEmpty(str) Then Return "" | |
'文字列をバイト型配列にする | |
Dim bytesIn As Byte() = System.Text.Encoding.UTF8.GetBytes(str) | |
'DESCryptoServiceProviderオブジェクトの作成 | |
Using des As New System.Security.Cryptography.DESCryptoServiceProvider | |
'共有キーと初期化ベクタを決定 | |
'パスワードをバイト配列にする | |
Dim bytesKey As Byte() = System.Text.Encoding.UTF8.GetBytes("_tween_encrypt_key_") | |
'共有キーと初期化ベクタを設定 | |
des.Key = ResizeBytesArray(bytesKey, des.Key.Length) | |
des.IV = ResizeBytesArray(bytesKey, des.IV.Length) | |
'暗号化されたデータを書き出すためのMemoryStream | |
Using msOut As New System.IO.MemoryStream | |
'DES暗号化オブジェクトの作成 | |
Using desdecrypt As System.Security.Cryptography.ICryptoTransform = _ | |
des.CreateEncryptor() | |
'書き込むためのCryptoStreamの作成 | |
Using cryptStream As New System.Security.Cryptography.CryptoStream( _ | |
msOut, desdecrypt, _ | |
System.Security.Cryptography.CryptoStreamMode.Write) | |
'書き込む | |
cryptStream.Write(bytesIn, 0, bytesIn.Length) | |
cryptStream.FlushFinalBlock() | |
'暗号化されたデータを取得 | |
Dim bytesOut As Byte() = msOut.ToArray() | |
'閉じる | |
cryptStream.Close() | |
msOut.Close() | |
'Base64で文字列に変更して結果を返す | |
Return System.Convert.ToBase64String(bytesOut) | |
End Using | |
End Using | |
End Using | |
End Using | |
End Function | |
End Module |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment